private IClasspathEntry[] computePluginEntries() { List<IClasspathEntry> entries = new ArrayList<>(); IResolvedPlugin resolvedPlugin = JPFPluginModelManager.instance().getResolvedPlugin(fModel); if( resolvedPlugin == null ) { return new IClasspathEntry[0]; } Set<IModel> seen = new HashSet<>(); IResolvedPlugin hostPlugin = resolvedPlugin.getHostPlugin(); if( hostPlugin != null ) { addImports(hostPlugin, entries, true, true, false, seen); } addImports(resolvedPlugin, entries, true, false, true, seen); return entries.toArray(new IClasspathEntry[entries.size()]); }
public static void removeFromProject(IJavaProject javaProject) { try { Set<IClasspathEntry> entries = new LinkedHashSet<>(); entries.addAll(Arrays.asList(javaProject.getRawClasspath())); if( entries.remove(JavaCore.newContainerEntry(JPFClasspathPlugin.CONTAINER_PATH)) ) { javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null); } } catch( JavaModelException e ) { JPFClasspathLog.logError(e); } }
@Override public List<IClasspathEntry> createClasspathEntries() { IPath srcJar = null; if( underlyingResource.getFileExtension().equals("jar") ) { String name = underlyingResource.getName(); IFile srcJarFile = underlyingResource.getProject().getFile( "lib-src/" + name.substring(0, name.length() - 4) + "-sources.jar"); if( srcJarFile.exists() ) { srcJar = srcJarFile.getFullPath(); } } return Arrays.asList(JavaCore.newLibraryEntry(underlyingResource.getFullPath(), srcJar, null)); }
public static void getProjectClassPath(IJavaProject project, List<File> dst) throws Exception { IRuntimeClasspathEntry [] rentries = JavaRuntime.computeUnresolvedRuntimeClasspath(project); for (IRuntimeClasspathEntry entry : rentries) { switch (entry.getType()) { case IClasspathEntry.CPE_SOURCE: break; case IClasspathEntry.CPE_PROJECT: break; case IClasspathEntry.CPE_LIBRARY: break; case IClasspathEntry.CPE_VARIABLE: // JRE like entries IRuntimeClasspathEntry [] variableEntries = JavaRuntime.resolveRuntimeClasspathEntry(entry, project); break; case IClasspathEntry.CPE_CONTAINER: IRuntimeClasspathEntry [] containerEntries = JavaRuntime.resolveRuntimeClasspathEntry(entry, project); for (IRuntimeClasspathEntry containerentry : containerEntries) { dst.add(new File (containerentry.getLocation())); } break; default: throw new Exception("unsupported classpath entry "+entry); } } }
/** * Add GraphWalker libraries to the passed project * * @param project * @throws JavaModelException */ public static void addGW4EClassPathContainer(IProject project) throws JavaModelException { if (hasGW4EClassPathContainer(project)) { return; } IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; System.arraycopy(entries, 0, newEntries, 0, entries.length); Path lcp = new Path(GW4ELibrariesContainer.ID); IClasspathEntry libEntry = JavaCore.newContainerEntry(lcp, true); newEntries[entries.length] = JavaCore.newContainerEntry(libEntry.getPath(), true); javaProject.setRawClasspath(newEntries, null); addJunit4Libraries(project); }
/** * Add JUnit libraries to the passed project * * @param project * @throws JavaModelException */ private static void addJunit4Libraries(IProject project) throws JavaModelException { IClasspathEntry entry = JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH); IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] entries = javaProject.getRawClasspath(); boolean junitFound = false; String s = entry.getPath().toString(); for (int i = 0; i < entries.length; i++) { if (entries[i].getPath().toString().indexOf(s) != -1) { junitFound = true; break; } } if (!junitFound) { IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; System.arraycopy(entries, 0, newEntries, 0, entries.length); newEntries[entries.length] = entry; javaProject.setRawClasspath(newEntries, null); } }
/** * Remove the passed folder from ClassPath * * @param project * @param folderPath * @param monitor * @throws JavaModelException */ public static void removeFolderFromClasspath(IProject project, String folderPath, IProgressMonitor monitor) throws JavaModelException { IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] entries = javaProject.getRawClasspath(); List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(); IPath folder = project.getFolder(folderPath).getFullPath(); for (int i = 0; i < entries.length; i++) { if (!entries[i].getPath().equals(folder)) { newEntries.add(entries[i]); } } entries = new IClasspathEntry[newEntries.size()]; newEntries.toArray(entries); javaProject.setRawClasspath(entries, monitor); }
/** * @param projectName * @return the direct child folders of the project * @throws CoreException */ public static List<String> getFilteredSourceFolders(String projectName, String[] excludes) throws CoreException { List<String> ret = new ArrayList<String>(); IJavaProject javaProject = (IJavaProject) JavaCore.create(getProject(projectName)); IClasspathEntry[] entries = javaProject.getRawClasspath(); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath().makeRelativeTo(new Path(projectName)); boolean isExcluded = false; for (int j = 0; j < excludes.length; j++) { if (excludes[j].equalsIgnoreCase(path.toString())) { isExcluded = true; break; } } if (!isExcluded) { String p = path.toString(); ret.add(p); } } } return ret; }
private void copyExternalLibAndAddToClassPath(String source,IFolder destinationFolder, List<IClasspathEntry> entries) throws CoreException{ File sourceFileLocation = new File(source); File[] listFiles = sourceFileLocation.listFiles(); if(listFiles != null){ for(File sourceFile : listFiles){ IFile destinationFile = destinationFolder.getFile(sourceFile.getName()); try(InputStream fileInputStream = new FileInputStream(sourceFile)) { if(!destinationFile.exists()){ //used while importing a project destinationFile.create(fileInputStream, true, null); } entries.add(JavaCore.newLibraryEntry(new Path(destinationFile.getLocation().toOSString()), null, null)); } catch (IOException | CoreException exception) { logger.debug("Copy external library files operation failed", exception); throw new CoreException(new MultiStatus(Activator.PLUGIN_ID, 101, "Copy external library files operation failed", exception)); } } } }
public static void setClasspath(IClasspathEntry[] classpath, IJavaProject javaProject, IProgressMonitor monitor) throws JavaModelException { // backup .classpath File classPathFileBak = javaProject.getProject().getFile(".classpath.bak").getLocation().toFile(); if (!classPathFileBak.exists()) { File classPathFile = javaProject.getProject().getFile(".classpath").getLocation().toFile(); try { Files.copy(Paths.get(classPathFile.getAbsolutePath()), Paths.get(classPathFileBak.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { // can't back up file but we should continue anyway Activator.log("Failed to backup classfile [" + classPathFile.getAbsolutePath() + "]"); } } javaProject.setRawClasspath(classpath, monitor); }
@Override public IClasspathEntry getSelection() { IPath path = new Path(Initializer.APGAS_CONTAINER_ID); final int index = this.mProjectsCombo.getSelectionIndex(); if (index != -1) { final String selectedProjectName = this.mProjectsCombo.getItem(index); if (this.mOwnerProject == null || !selectedProjectName.equals(this.mOwnerProject.getName())) { path = path.append(selectedProjectName); } } return JavaCore.newContainerEntry(path); }
public DerbyClasspathContainer() { List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(); Bundle bundle = Platform.getBundle(CommonNames.CORE_PATH); Enumeration en = bundle.findEntries("/", "*.jar", true); String rootPath = null; try { rootPath = FileLocator.resolve(FileLocator.find(bundle, new Path("/"), null)).getPath(); } catch(IOException e) { Logger.log(e.getMessage(), IStatus.ERROR); } while(en.hasMoreElements()) { IClasspathEntry cpe = JavaCore.newLibraryEntry(new Path(rootPath+'/'+((URL)en.nextElement()).getFile()), null, null); entries.add(cpe); } IClasspathEntry[] cpes = new IClasspathEntry[entries.size()]; _entries = (IClasspathEntry[])entries.toArray(cpes); }
private static void createClasspath(IPath root, List<String> paths, IJavaProject javaProject, int javaLanguageLevel) throws CoreException { String name = root.lastSegment(); IFolder base = javaProject.getProject().getFolder(name); if (!base.isLinked()) { base.createLink(root, IResource.NONE, null); } List<IClasspathEntry> list = new LinkedList<>(); for (String path : paths) { IPath workspacePath = base.getFullPath().append(path); list.add(JavaCore.newSourceEntry(workspacePath)); } list.add(JavaCore.newContainerEntry(new Path(BazelClasspathContainer.CONTAINER_NAME))); list.add( JavaCore.newContainerEntry(new Path(STANDARD_VM_CONTAINER_PREFIX + javaLanguageLevel))); IClasspathEntry[] newClasspath = list.toArray(new IClasspathEntry[0]); javaProject.setRawClasspath(newClasspath, null); }
private boolean isSourcePath(String path) throws JavaModelException, BackingStoreException { Path pp = new File(instance.getWorkspaceRoot().toString() + File.separator + path).toPath(); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); for (IClasspathEntry entry : project.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IResource res = root.findMember(entry.getPath()); if (res != null) { String file = res.getLocation().toOSString(); if (!file.isEmpty() && pp.startsWith(file)) { IPath[] inclusionPatterns = entry.getInclusionPatterns(); if (!matchPatterns(pp, entry.getExclusionPatterns()) && (inclusionPatterns == null || inclusionPatterns.length == 0 || matchPatterns(pp, inclusionPatterns))) { return true; } } } } } return false; }
/** * Remove all JRE runtime entries from the given set * * @param scope * set to filter * @return filtered set without JRE runtime entries */ public static Set<IPackageFragmentRoot> filterJREEntries( Collection<IPackageFragmentRoot> scope) throws JavaModelException { final Set<IPackageFragmentRoot> filtered = new HashSet<IPackageFragmentRoot>(); for (final IPackageFragmentRoot root : scope) { final IClasspathEntry entry = root.getRawClasspathEntry(); switch (entry.getEntryKind()) { case IClasspathEntry.CPE_SOURCE: case IClasspathEntry.CPE_LIBRARY: case IClasspathEntry.CPE_VARIABLE: filtered.add(root); break; case IClasspathEntry.CPE_CONTAINER: IClasspathContainer container = JavaCore.getClasspathContainer( entry.getPath(), root.getJavaProject()); if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) { filtered.add(root); } break; } } return filtered; }
/** * Return the set of Java source paths for the given project, relative to the project. Return an * empty list if not a Java project. */ public static List<IPath> getJavaSourcePaths(IProject project) { IJavaProject javaProject = JavaCore.create(project); if (!javaProject.exists()) { return Collections.emptyList(); } try { IClasspathEntry[] classpathEntries = javaProject.getRawClasspath(); List<IPath> paths = new ArrayList<>(); for (IClasspathEntry entry : classpathEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { // source paths are absolute to the root folder of the project paths.add(entry.getPath().removeFirstSegments(1)); } } return paths; } catch (JavaModelException ex) { return Collections.emptyList(); } }
private void addDependenciesToClasspath(IProject project, IFolder folder, IProgressMonitor monitor) throws CoreException { List<IClasspathEntry> newEntries = new ArrayList<>(); IClasspathAttribute[] nonDependencyAttribute = new IClasspathAttribute[] {UpdateClasspathAttributeUtil.createNonDependencyAttribute()}; // Add all the jars under lib folder to the classpath File libFolder = folder.getLocation().toFile(); for (File file : libFolder.listFiles()) { IPath path = Path.fromOSString(file.toPath().toString()); newEntries.add(JavaCore.newLibraryEntry(path, null, null, new IAccessRule[0], nonDependencyAttribute, false /* isExported */)); } ClasspathUtil.addClasspathEntries(project, newEntries, monitor); }
@Override public IClasspathEntry[] resolveClasspathContainer(IProject project, IRuntime runtime) { if (project != null && MavenUtils.hasMavenNature(project)) { // Maven handles its own classpath return NO_CLASSPATH_ENTRIES; } // Runtime is expected to provide Servlet and JSP APIs IProjectFacetVersion webFacetVersion = DEFAULT_DYNAMIC_WEB_VERSION; try { IFacetedProject facetedProject = ProjectFacetsManager.create(project); webFacetVersion = facetedProject.getInstalledVersion(WebFacetUtils.WEB_FACET); } catch (CoreException ex) { logger.log(Level.WARNING, "Unable to obtain jst.web facet version", ex); } return doResolveClasspathContainer(webFacetVersion); }
static IClasspathEntry[] getAppEngineServerRuntimeClasspathEntries(IProject project) { IJavaProject javaProject = JavaCore.create(project); IPath containerPath = new Path( "org.eclipse.jst.server.core.container/com.google.cloud.tools.eclipse.appengine.standard.runtimeClasspathProvider"); try { for (IClasspathEntry entry : javaProject.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && containerPath.isPrefixOf(entry.getPath())) { // resolve and return the entries IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject); return container.getClasspathEntries(); } } } catch (JavaModelException ex) { fail(ex.toString()); /* NOTREACHED */ } fail("AppEngine Server Runtime classpath container not found"); return null; }
@Test public void testInitialize_ifArtifactJarPathIsInvalidContainerResolvedFromScratch() throws CoreException, IOException { assertFalse(new File(NON_EXISTENT_FILE).exists()); IClasspathEntry entry = mock(IClasspathEntry.class); when(entry.getPath()).thenReturn(new Path(NON_EXISTENT_FILE)); IClasspathEntry[] entries = new IClasspathEntry[]{ entry }; LibraryClasspathContainer container = mock(LibraryClasspathContainer.class); when(container.getClasspathEntries()).thenReturn(entries); when(serializer.loadContainer(any(IJavaProject.class), any(IPath.class))).thenReturn(container); LibraryClasspathContainerInitializer containerInitializer = new LibraryClasspathContainerInitializer(TEST_CONTAINER_PATH, serializer, resolverService); containerInitializer.initialize(new Path(TEST_LIBRARY_PATH), testProject.getJavaProject()); verifyContainerResolvedFromScratch(); }
@Test public void testInitialize_ifSourceArtifactJarPathInvalidContainerResolvedFromScratch() throws CoreException, IOException { File artifactFile = temporaryFolder.newFile(); assertFalse(new File(NON_EXISTENT_FILE).exists()); IClasspathEntry entry = mock(IClasspathEntry.class); when(entry.getPath()).thenReturn(new Path(artifactFile.getAbsolutePath())); when(entry.getSourceAttachmentPath()).thenReturn(new Path(NON_EXISTENT_FILE)); IClasspathEntry[] entries = new IClasspathEntry[]{ entry }; LibraryClasspathContainer container = mock(LibraryClasspathContainer.class); when(container.getClasspathEntries()).thenReturn(entries); when(serializer.loadContainer(any(IJavaProject.class), any(IPath.class))).thenReturn(container); LibraryClasspathContainerInitializer containerInitializer = new LibraryClasspathContainerInitializer(TEST_CONTAINER_PATH, serializer, resolverService); containerInitializer.initialize(new Path(TEST_LIBRARY_PATH), testProject.getJavaProject()); verifyContainerResolvedFromScratch(); }
public static void addToClassPath(IResource res, int type, IJavaProject javaProject, IProgressMonitor monitor) throws JavaModelException { Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>(Arrays.asList(javaProject.getRawClasspath())); IClasspathEntry entry = null; switch (type) { case IClasspathEntry.CPE_SOURCE: entry = JavaCore.newSourceEntry(res.getFullPath()); break; case IClasspathEntry.CPE_LIBRARY: entry = JavaCore.newLibraryEntry(res.getFullPath(), null, null, true); break; case IClasspathEntry.CPE_PROJECT: entry = JavaCore.newProjectEntry(res.getFullPath(), true); break; } entries.add(entry); setClasspath(entries.toArray(new IClasspathEntry[entries.size()]), javaProject, monitor); }
/** * * @param javaProject * @param jar * @return * @throws JavaModelException */ private boolean isClasspathEntryForJar(IJavaProject javaProject, IResource jar) throws JavaModelException { IClasspathEntry[] classPathEntries = javaProject.getRawClasspath(); if (classPathEntries != null) { for (IClasspathEntry classpathEntry : classPathEntries) { // fix jar files if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { if (classpathEntry.getPath().equals(jar.getFullPath())) { return true; } } } } return false; }
/** * We need to add all hybris projects to the build path of each project so that all configured extensions will be on the Eclipse classpath and the extensions will be loaded correctly * @return */ private void addAllHybrisProjectsToBuildPathForCustomProjects(IProgressMonitor monitor) throws JavaModelException { monitor.setTaskName("Fixing build path"); Set<IProject> customProjects = FixProjectsUtils.getAllCustomerProjects(); monitor.beginTask("Fixing build path", customProjects.size()); int i = 0; for (IProject sourceProject : customProjects) { if (sourceProject.isOpen()) { IJavaProject javaSourceProject = JavaCore.create(sourceProject); List<IClasspathEntry> entries = new LinkedList<IClasspathEntry>( Arrays.asList(javaSourceProject.getRawClasspath())); for (IProject targetProject : FixProjectsUtils.getAllPlatformProjects()) { if (!sourceProject.equals(targetProject)) { IClasspathEntry entry = JavaCore.newProjectEntry(targetProject.getFullPath(), true); addToListIfNotExisting(entries, entry); } } FixProjectsUtils.setClasspath(entries.toArray(new IClasspathEntry[entries.size()]), javaSourceProject, monitor); monitor.internalWorked(i++); } } }
@VisibleForTesting static boolean removeGpeClasspathEntries(IProject project, IProgressMonitor monitor) { boolean foundGpeEntries = false; try { IJavaProject javaProject = JavaCore.create(project); List<IClasspathEntry> newEntries = new ArrayList<>(); for (IClasspathEntry entry : javaProject.getRawClasspath()) { if (!isGpeClasspath(entry)) { newEntries.add(entry); } else { foundGpeEntries = true; } } IClasspathEntry[] rawEntries = newEntries.toArray(new IClasspathEntry[0]); javaProject.setRawClasspath(rawEntries, monitor); javaProject.save(monitor, true); } catch (JavaModelException ex) { logger.log(Level.WARNING, "Failed to remove GPE classpath entries.", ex); } return foundGpeEntries; }
public static void addNativeLibrary(IJavaProject javaProject, List<Library> libraries, IProgressMonitor monitor) throws CoreException { if (libraries.isEmpty()) { return; } AnalyticsLibraryPingHelper.sendLibrarySelectionPing(AnalyticsEvents.NATIVE_PROJECT, libraries); SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.getString("adding.app.engine.libraries"), //$NON-NLS-1$ 25); Library masterLibrary = collectLibraryFiles(javaProject, libraries, subMonitor.newChild(8)); IClasspathEntry masterEntry = computeEntry(javaProject, masterLibrary, subMonitor.newChild(8)); saveLibraryList(javaProject, libraries, subMonitor.newChild(1)); if (masterEntry != null) { ClasspathUtil.addClasspathEntry(javaProject.getProject(), masterEntry, subMonitor.newChild(8)); } runContainerResolverJob(javaProject); }
LibraryClasspathContainer toLibraryClasspathContainer(IJavaProject javaProject, IPath baseDirectory, IPath sourceBaseDirectory) { List<IClasspathEntry> classpathEntries = new ArrayList<>(); for (SerializableClasspathEntry entry : entries) { classpathEntries.add(entry.toClasspathEntry(baseDirectory, sourceBaseDirectory)); } Library masterLibrary = new Library(CloudLibraries.MASTER_CONTAINER_ID); masterLibrary.setName(Messages.getString("google.cloud.platform.libraries")); //$NON-NLS-1$ if (libraryFiles == null) { // we deserialized an old version libraryFiles = new ArrayList<>(); } masterLibrary.setLibraryFiles(libraryFiles); return new LibraryClasspathContainer(new Path(path), description, classpathEntries, libraryFiles); }
@Override public IStatus resolveAll(IJavaProject javaProject, IProgressMonitor monitor) { try { MultiStatus status = StatusUtil.multi(this, Messages.getString("TaskResolveLibrariesError")); //$NON-NLS-1$ IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.getString("TaskResolveLibraries"), //$NON-NLS-1$ getTotalWork(rawClasspath)); for (IClasspathEntry classpathEntry : rawClasspath) { if (classpathEntry.getPath().segment(0) .equals(LibraryClasspathContainer.CONTAINER_PATH_PREFIX)) { IStatus resolveContainerStatus = resolveContainer(javaProject, classpathEntry.getPath(), subMonitor.newChild(1)); status.add(resolveContainerStatus); } } // rewrite if OK as otherwise Progress View shows the resolving error message return StatusUtil.filter(status); } catch (CoreException ex) { return StatusUtil.error(this, Messages.getString("TaskResolveLibrariesError"), ex); //$NON-NLS-1$ } }
private LibraryClasspathContainer resolveLibraryFiles(IJavaProject javaProject, IPath containerPath, Library library, List<Job> sourceAttacherJobs, IProgressMonitor monitor) throws CoreException { List<LibraryFile> libraryFiles = library.getAllDependencies(); SubMonitor subMonitor = SubMonitor.convert(monitor, libraryFiles.size()); subMonitor.subTask(Messages.getString("TaskResolveArtifacts", getLibraryDescription(library))); SubMonitor child = subMonitor.newChild(libraryFiles.size()); List<IClasspathEntry> entries = new ArrayList<>(); for (LibraryFile libraryFile : libraryFiles) { IClasspathEntry newLibraryEntry = resolveLibraryFileAttachSourceAsync(javaProject, containerPath, libraryFile, sourceAttacherJobs, monitor); entries.add(newLibraryEntry); child.worked(1); } monitor.done(); LibraryClasspathContainer container = new LibraryClasspathContainer( containerPath, getLibraryDescription(library), entries, libraryFiles); return container; }
private IClasspathEntry resolveLibraryFileAttachSourceAsync(IJavaProject javaProject, IPath containerPath, LibraryFile libraryFile, List<Job> sourceAttacherJobs, IProgressMonitor monitor) throws CoreException { Artifact artifact = repositoryService.resolveArtifact(libraryFile, monitor); IPath artifactPath = new Path(artifact.getFile().getAbsolutePath()); Job job = createSourceAttacherJob(javaProject, containerPath, libraryFile, monitor, artifact, artifactPath); sourceAttacherJobs.add(job); IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(artifactPath, null /* sourceAttachmentPath */, null /* sourceAttachmentRootPath */, getAccessRules(libraryFile.getFilters()), getClasspathAttributes(libraryFile, artifact), true /* isExported */); return newLibraryEntry; }
private IClasspathEntry resolveLibraryFileAttachSourceSync(LibraryFile libraryFile) throws CoreException { Artifact artifact = repositoryService.resolveArtifact(libraryFile, new NullProgressMonitor()); IPath libraryPath = new Path(artifact.getFile().getAbsolutePath()); // Not all artifacts have sources; need to work if no source artifact is available // e.g. appengine-api-sdk doesn't IPath sourceAttachmentPath = null; try { sourceAttachmentPath = repositoryService.resolveSourceArtifact(libraryFile, artifact.getVersion(), new NullProgressMonitor()); } catch (CoreException ex) { // continue without source } IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(libraryPath, sourceAttachmentPath, null /* sourceAttachmentRootPath */, getAccessRules(libraryFile.getFilters()), getClasspathAttributes(libraryFile, artifact), true /* isExported */); return newLibraryEntry; }
@VisibleForTesting LibraryClasspathContainer attachSource(IClasspathContainer container) throws Exception { if (!(container instanceof LibraryClasspathContainer)) { logger.log(Level.FINE, Messages.getString("ContainerClassUnexpected", container.getClass().getName(), LibraryClasspathContainer.class.getName())); return null; } LibraryClasspathContainer libraryClasspathContainer = (LibraryClasspathContainer) container; IPath sourceArtifactPath = sourceArtifactPathProvider.call(); List<IClasspathEntry> newClasspathEntries = new ArrayList<>(); for (IClasspathEntry entry : libraryClasspathContainer.getClasspathEntries()) { if (!entry.getPath().equals(libraryPath)) { newClasspathEntries.add(entry); } else { newClasspathEntries.add(JavaCore.newLibraryEntry( entry.getPath(), sourceArtifactPath, null /* sourceAttachmentRootPath */, entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported())); } } return libraryClasspathContainer.copyWithNewEntries(newClasspathEntries); }
private void addMavenClasspathEntries(Set<IRuntimeClasspathEntry> resolved, IRuntimeClasspathEntry runtimeClasspathEntry, ILaunchConfiguration configuration, int scope, IProgressMonitor monitor) throws CoreException { IJavaProject javaProject = JavaRuntime.getJavaProject(configuration); MavenJdtPlugin plugin = MavenJdtPlugin.getDefault(); IClasspathManager buildpathManager = plugin.getBuildpathManager(); IClasspathEntry[] cp = buildpathManager.getClasspath(javaProject.getProject(), scope, false, monitor); for(IClasspathEntry entry : cp) { switch(entry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: addProjectEntries(resolved, entry.getPath(), scope, getArtifactClassifier(entry), configuration, monitor); break; case IClasspathEntry.CPE_LIBRARY: resolved.add(JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath())); break; // case IClasspathEntry.CPE_SOURCE: // resolved.add(newSourceClasspathEntry(javaProject, cp[i])); // break; } } }
/** * Sometimes the project configuration is corrupt and a Java runtime is not on the classpath * @param monitor * @param javaProject * @throws JavaModelException */ private void fixMissingJavaRuntime(IProgressMonitor monitor, IJavaProject javaProject) throws JavaModelException { if (!javaProject.getProject().getName().equals("config")) { IClasspathEntry[] classPathEntries = javaProject.getRawClasspath(); boolean found = false; for (IClasspathEntry classpathEntry : classPathEntries) { // fix missing runtime if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { if (classpathEntry.getPath().toString().startsWith("org.eclipse.jdt.launching.JRE_CONTAINER")) { found = true; break; } } } if (!found) { IClasspathEntry entry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER"), false); Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>(Arrays.asList(classPathEntries)); entries.add(entry); FixProjectsUtils.setClasspath(entries.toArray(new IClasspathEntry[entries.size()]), javaProject, monitor); } } }
private void addJarFilesNotInClasspath(IProgressMonitor monitor, IProject project, IJavaProject javaProject) throws CoreException { addMembersOfFolderToClasspath("/lib", monitor, javaProject); addMembersOfFolderToClasspath("/web/webroot/WEB-INF/lib", monitor, javaProject); // check if this is a backoffice extension IFolder backofficeFolder = javaProject.getProject().getFolder("/resources/backoffice"); if (backofficeFolder != null && backofficeFolder.exists()) { IResource backofficeJar = backofficeFolder.findMember(javaProject.getProject().getName() + "_bof.jar"); if (backofficeJar != null && backofficeJar.exists()) { if (!isClasspathEntryForJar(javaProject, backofficeJar)) { Activator.log("Adding library [" + backofficeJar.getFullPath() + "] to classpath for project [" + javaProject.getProject().getName() + "]"); FixProjectsUtils.addToClassPath(backofficeJar, IClasspathEntry.CPE_LIBRARY, javaProject, monitor); } } } // add db drivers for platform/lib/dbdriver directory if (project.getName().equalsIgnoreCase("platform")) { addMembersOfFolderToClasspath("/lib/dbdriver", monitor, javaProject); } }
private void addMembersOfFolderToClasspath(String path, IProgressMonitor monitor, IJavaProject javaProject) throws CoreException, JavaModelException { IFolder folder = javaProject.getProject().getFolder(path); if (folder != null && folder.exists()) { for (IResource res : folder.members()) { if (res.getFileExtension() != null && res.getFileExtension().equals("jar") && res.exists()) { // check if this Resource is on the classpath if (!javaProject.isOnClasspath(res)) { if (debug) Activator.log("Adding library [" + res.getFullPath() + "] to classpath for project [" + javaProject.getProject().getName() + "]"); FixProjectsUtils.addToClassPath(res, IClasspathEntry.CPE_LIBRARY, javaProject, monitor); } } } } }
@Override public IClasspathEntry[] getClasspathEntries() { if( fModel == null ) { return new IClasspathEntry[0]; } if( fEntries == null ) { fEntries = computePluginEntries(); } return fEntries; }
private void addImports(IResolvedPlugin resolvedPlugin, List<IClasspathEntry> entries, boolean allImports, boolean includeSelf, boolean includeFragments, Set<IModel> seen) { IPluginModel model = resolvedPlugin.getPluginModel(); if( model == null ) { return; } if( !seen.add(model) ) { return; } seen.add(model); if( includeSelf ) { entries.addAll(model.createClasspathEntries()); } List<ResolvedImport> imports = resolvedPlugin.getImports(); for( ResolvedImport resolvedImport : imports ) { IResolvedPlugin resolvedImportPlugin = resolvedImport.getResolved(); if( allImports || resolvedImport.getPrerequisite().isExported() ) { addImports(resolvedImportPlugin, entries, false, true, true, seen); } } if( includeFragments ) { for( IResolvedPlugin fragment : resolvedPlugin.getFragments() ) { addImports(fragment, entries, allImports, true, false, seen); } } }
public static void addToProject(IJavaProject javaProject) { try { Set<IClasspathEntry> entries = new LinkedHashSet<>(); entries.addAll(Arrays.asList(javaProject.getRawClasspath())); entries.add(JavaCore.newContainerEntry(JPFClasspathPlugin.CONTAINER_PATH)); javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null); } catch( JavaModelException e ) { JPFClasspathLog.logError(e); } }
@Override public IClasspathEntry[] getDefaultClasspathEntries() { List<IClasspathEntry> entries = new ArrayList<>(); entries.addAll(Arrays.asList(PreferenceConstants.getDefaultJRELibrary())); entries.add(JavaCore.newContainerEntry(JPFClasspathPlugin.CONTAINER_PATH)); return entries.toArray(new IClasspathEntry[entries.size()]); }