Java Code Examples for org.eclipse.jdt.core.IJavaProject#getRawClasspath()
The following examples show how to use
org.eclipse.jdt.core.IJavaProject#getRawClasspath() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResetAllAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private boolean hasChange(IJavaProject project) throws JavaModelException { if (!project.getOutputLocation().equals(fOutputLocation)) return true; IClasspathEntry[] currentEntries= project.getRawClasspath(); if (currentEntries.length != fEntries.size()) return true; int i= 0; for (Iterator<CPListElement> iterator= fEntries.iterator(); iterator.hasNext();) { CPListElement oldEntrie= iterator.next(); if (!oldEntrie.getClasspathEntry().equals(currentEntries[i])) return true; i++; } return false; }
Example 2
Source File: GpeMigrator.java From google-cloud-eclipse with Apache License 2.0 | 6 votes |
@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; }
Example 3
Source File: CloudToolsEclipseProjectUpdater.java From google-cloud-eclipse with Apache License 2.0 | 6 votes |
/** * Return true if this projects uses the old-style per-library container. */ public static boolean hasOldContainers(IProject project) { try { if (!project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID)) { return false; } IJavaProject javaProject = JavaCore.create(project); for (IClasspathEntry entry : javaProject.getRawClasspath()) { IPath containerPath = entry.getPath(); if (LibraryClasspathContainer.isEntry(entry) && !CloudLibraries.MASTER_CONTAINER_ID.equals(containerPath.segment(1))) { return true; } } } catch (CoreException ex) { logger.log(Level.WARNING, "Skipping project: " + project.getName(), ex); //$NON-NLS-1$ } return false; }
Example 4
Source File: NewMavenBasedAppEngineProjectWizardTest.java From google-cloud-eclipse with Apache License 2.0 | 6 votes |
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; }
Example 5
Source File: Importer.java From hybris-commerce-eclipse-plugin with Apache License 2.0 | 6 votes |
/** * * @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; }
Example 6
Source File: BinaryRefactoringHistoryWizard.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Configures the classpath of the project before refactoring. * * @param project * the java project * @param root * the package fragment root to refactor * @param folder * the temporary source folder * @param monitor * the progress monitor to use * @throws IllegalStateException * if the plugin state location does not exist * @throws CoreException * if an error occurs while configuring the class path */ private static void configureClasspath(final IJavaProject project, final IPackageFragmentRoot root, final IFolder folder, final IProgressMonitor monitor) throws IllegalStateException, CoreException { try { monitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, 200); final IClasspathEntry entry= root.getRawClasspathEntry(); final IClasspathEntry[] entries= project.getRawClasspath(); final List<IClasspathEntry> list= new ArrayList<IClasspathEntry>(); list.addAll(Arrays.asList(entries)); final IFileStore store= EFS.getLocalFileSystem().getStore(JavaPlugin.getDefault().getStateLocation().append(STUB_FOLDER).append(project.getElementName())); if (store.fetchInfo(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)).exists()) store.delete(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); store.mkdir(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); folder.createLink(store.toURI(), IResource.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); addExclusionPatterns(list, folder.getFullPath()); for (int index= 0; index < entries.length; index++) { if (entries[index].equals(entry)) list.add(index, JavaCore.newSourceEntry(folder.getFullPath())); } project.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), false, new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); } finally { monitor.done(); } }
Example 7
Source File: ClasspathUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 6 votes |
/** * Replaces a project's classpath container entry with a new one or appends it * to the classpath if none were found. * * @param javaProject The target project * @param containerId the identifier of the classpath container type * @param newContainerPath the path for the new classpath. Note: this path * should be partial, not including the initial segment which should * in all cases be the value in containerId * @throws JavaModelException thrown by the call to getRawClasspath() */ public static void replaceOrAppendContainer(IJavaProject javaProject, String containerId, IPath newContainerPath, IProgressMonitor monitor) throws JavaModelException { IClasspathEntry[] classpathEntries = javaProject.getRawClasspath(); int indexOfClasspathEntryContainer = ClasspathUtilities.indexOfClasspathEntryContainer( classpathEntries, containerId); IClasspathEntry newContainer = JavaCore.newContainerEntry(new Path( containerId).append(newContainerPath)); List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>( Arrays.asList(classpathEntries)); if (indexOfClasspathEntryContainer >= 0) { // Replace the entry newClasspathEntries.set(indexOfClasspathEntryContainer, newContainer); } else { // Append the entry newClasspathEntries.add(newContainer); } javaProject.setRawClasspath( newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), monitor); }
Example 8
Source File: RefactoringAvailabilityTester.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static boolean isInferTypeArgumentsAvailable(final IJavaElement element) throws JavaModelException { if (!Checks.isAvailable(element)) { return false; } else if (element instanceof IJavaProject) { IJavaProject project = (IJavaProject) element; IClasspathEntry[] classpathEntries = project.getRawClasspath(); for (int i = 0; i < classpathEntries.length; i++) { if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { return true; } } return false; } else if (element instanceof IPackageFragmentRoot) { return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE; } else if (element instanceof IPackageFragment) { return ((IPackageFragment) element).getKind() == IPackageFragmentRoot.K_SOURCE; } else if (element instanceof ICompilationUnit) { return true; } else if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null) { return true; } else { return false; } }
Example 9
Source File: ProblemsLabelDecorator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private boolean isIgnoringOptionalProblems(IJavaProject project) throws JavaModelException { IPath projectPath= project.getPath(); IClasspathEntry[] rawClasspath= project.getRawClasspath(); for (IClasspathEntry entry : rawClasspath) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && projectPath.equals(entry.getPath()) && isIgnoringOptionalProblems(entry)) return true; } return false; }
Example 10
Source File: RenameJavaProjectChange.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void modifyClassPath(IJavaProject referencingProject, IProgressMonitor pm) throws JavaModelException { pm.beginTask("", 1); //$NON-NLS-1$ IClasspathEntry[] oldEntries= referencingProject.getRawClasspath(); IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length]; for (int i= 0; i < newEntries.length; i++) { if (isOurEntry(oldEntries[i])) newEntries[i]= createModifiedEntry(oldEntries[i]); else newEntries[i]= oldEntries[i]; } referencingProject.setRawClasspath(newEntries, pm); pm.done(); }
Example 11
Source File: JavaProjectHelper.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
public static void addToClasspath(IJavaProject jproject, IClasspathEntry cpe) throws JavaModelException { IClasspathEntry[] oldEntries = jproject.getRawClasspath(); for (int i = 0; i < oldEntries.length; i++) { if (oldEntries[i].equals(cpe)) { return; } } int nEntries = oldEntries.length; IClasspathEntry[] newEntries = new IClasspathEntry[nEntries + 1]; System.arraycopy(oldEntries, 0, newEntries, 0, nEntries); newEntries[nEntries] = cpe; jproject.setRawClasspath(newEntries, null); }
Example 12
Source File: SourceAttachmentCommandTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testUpdateSourceAttachmentFromExternalJar() throws Exception { File file = copyFiles("eclipse/external/foo-sources.jar", false); IPath sourcePath = Path.fromOSString(file.getAbsolutePath()); SourceAttachmentAttribute attributes = new SourceAttachmentAttribute(null, sourcePath.toOSString(), "UTF-8"); SourceAttachmentRequest request = new SourceAttachmentRequest(classFileUri, attributes); String arguments = new Gson().toJson(request, SourceAttachmentRequest.class); SourceAttachmentResult updateResult = SourceAttachmentCommand.updateSourceAttachment(Arrays.asList(arguments), new NullProgressMonitor()); assertNotNull(updateResult); assertNull(updateResult.errorMessage); // Verify the source is attached to the classfile. IClassFile classfile = JDTUtils.resolveClassFile(classFileUri); IBuffer buffer = classfile.getBuffer(); assertNotNull(buffer); assertTrue(buffer.getContents().indexOf("return sum;") >= 0); // Verify whether external jar attachment is saved with absolute path. IJavaProject javaProject = JavaCore.create(project); IPath relativePath = project.findMember("foo-sources.jar").getFullPath(); IPath absolutePath = sourcePath; for (IClasspathEntry entry : javaProject.getRawClasspath()) { if (Objects.equals("foo.jar", entry.getPath().lastSegment())) { assertNotNull(entry.getSourceAttachmentPath()); assertEquals(absolutePath, entry.getSourceAttachmentPath()); assertNotEquals(relativePath, entry.getSourceAttachmentPath()); break; } } }
Example 13
Source File: OutputFolderFilter.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Returns the result of this filter, when applied to the * given element. * * @param viewer the viewer * @param parent the parent * @param element the element to test * @return <code>true</code> if element should be included * @since 3.0 */ @Override public boolean select(Viewer viewer, Object parent, Object element) { if (element instanceof IFolder) { IFolder folder= (IFolder)element; IProject proj= folder.getProject(); try { if (!proj.hasNature(JavaCore.NATURE_ID)) return true; IJavaProject jProject= JavaCore.create(folder.getProject()); if (jProject == null || !jProject.exists()) return true; // Check default output location IPath defaultOutputLocation= jProject.getOutputLocation(); IPath folderPath= folder.getFullPath(); if (defaultOutputLocation != null && defaultOutputLocation.equals(folderPath)) return false; // Check output location for each class path entry IClasspathEntry[] cpEntries= jProject.getRawClasspath(); for (int i= 0, length= cpEntries.length; i < length; i++) { IPath outputLocation= cpEntries[i].getOutputLocation(); if (outputLocation != null && outputLocation.equals(folderPath)) return false; } } catch (CoreException ex) { return true; } } return true; }
Example 14
Source File: JavaProjectHelper.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static void addToClasspath(IJavaProject jproject, IClasspathEntry cpe) throws JavaModelException { IClasspathEntry[] oldEntries = jproject.getRawClasspath(); for (int i = 0; i < oldEntries.length; i++) { if (oldEntries[i].equals(cpe)) { return; } } int nEntries = oldEntries.length; IClasspathEntry[] newEntries = new IClasspathEntry[nEntries + 1]; System.arraycopy(oldEntries, 0, newEntries, 0, nEntries); newEntries[nEntries] = cpe; jproject.setRawClasspath(newEntries, null); }
Example 15
Source File: BuildPath.java From google-cloud-eclipse with Apache License 2.0 | 5 votes |
/** Find the master container in the given project or {@code null} if none. */ public static IClasspathEntry findMasterContainer(IJavaProject javaProject) throws JavaModelException { IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); return Stream.of(rawClasspath) .filter(LibraryClasspathContainer::isEntry) .filter(entry -> MASTER_CONTAINER_PATH.equals(entry.getPath())) .findAny() .orElse(null); }
Example 16
Source File: Importer.java From hybris-commerce-eclipse-plugin with Apache License 2.0 | 5 votes |
private void removeJarFilesThatDontExist(IProgressMonitor monitor, IProject project, IJavaProject javaProject) throws JavaModelException { IClasspathEntry[] classPathEntries = javaProject.getRawClasspath(); List<IClasspathEntry> newClassPathEntries = new LinkedList<IClasspathEntry>(); boolean changedClassPath = false; for (IClasspathEntry classpathEntry : classPathEntries) { // fix jar files if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { classpathEntry.getPath(); File classpathEntryFile = classpathEntry.getPath().toFile(); // remove JAR if it doesn't exist, only do this if the jar file is located in this project, we leave jars references from different projects if (classpathEntryFile.getPath().endsWith(".jar") && classpathEntryFile.getPath().startsWith("/" + project.getName() + "/") && !project.getFile(classpathEntryFile.getPath().replace("/" + project.getName() + "/", "/")).exists()) { changedClassPath = true; if (DEBUG) Activator.log("libary [" + classpathEntry.getPath() + "] not found for project [ " + project.getName() + "]"); } else { newClassPathEntries.add(classpathEntry); } } else { newClassPathEntries.add(classpathEntry); } } // we have a change to the classpath so now push the change if (changedClassPath) { if (newClassPathEntries.isEmpty()) { FixProjectsUtils.setClasspath(new IClasspathEntry[0], javaProject, monitor); } else { FixProjectsUtils.setClasspath(newClassPathEntries.toArray(new IClasspathEntry[newClassPathEntries.size()]), javaProject, monitor); } } }
Example 17
Source File: JavaDocLocations.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
final static void updateClasspathEntries(Map<IPath, String> oldLocationMap, IProgressMonitor monitor) throws JavaModelException { IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); IJavaProject[] javaProjects= JavaCore.create(root).getJavaProjects(); try { monitor.beginTask(CorextMessages.JavaDocLocations_migrate_operation, javaProjects.length); for (int i= 0; i < javaProjects.length; i++) { IJavaProject project= javaProjects[i]; String projectJavadoc= oldLocationMap.get(project.getPath()); if (projectJavadoc != null) { try { setProjectJavadocLocation(project, projectJavadoc); } catch (CoreException e) { // ignore } } IClasspathEntry[] rawClasspath= project.getRawClasspath(); boolean hasChange= false; for (int k= 0; k < rawClasspath.length; k++) { IClasspathEntry updated= getConvertedEntry(rawClasspath[k], project, oldLocationMap); if (updated != null) { rawClasspath[k]= updated; hasChange= true; } } if (hasChange) { project.setRawClasspath(rawClasspath, new SubProgressMonitor(monitor, 1)); } else { monitor.worked(1); } } } finally { monitor.done(); } }
Example 18
Source File: GWTProjectUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
/** * Returns the GWT-applicable source folder paths from a project (note: this * will not traverse into the project's dependencies, for this behavior, see * {@link #getGWTSourceFolderPathsFromProjectAndDependencies(IJavaProject, boolean)} * ). * * @param javaProject Reference to the project * @param sourceEntries The list to be filled with the entries corresponding * to the source folder paths * @param includeTestSourceEntries Whether to include the entries for test * source * @throws SdkException */ private static void fillGWTSourceFolderPathsFromProject( IJavaProject javaProject, Collection<? super IRuntimeClasspathEntry> sourceEntries, boolean includeTestSourceEntries) throws SdkException { assert (javaProject != null); if (GWTProjectsRuntime.isGWTRuntimeProject(javaProject)) { // TODO: Do we still need to handle this here since Sdk's report their // own runtime classpath entries? sourceEntries.addAll(GWTProjectsRuntime.getGWTRuntimeProjectSourceEntries( javaProject, includeTestSourceEntries)); } else { try { for (IClasspathEntry curClasspathEntry : javaProject.getRawClasspath()) { if (curClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath sourcePath = curClasspathEntry.getPath(); // If including tests, include all source, or if not including tests, ensure // it is not a test path if (includeTestSourceEntries || !GWTProjectUtilities.isTestPath(sourcePath)) { if (!isOptional(curClasspathEntry) || exists(sourcePath)) { sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(sourcePath)); } } } } IFolder folder = javaProject.getProject().getFolder("super"); if (folder.exists()) { sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(folder.getFullPath())); } } catch (JavaModelException jme) { GWTPluginLog.logError(jme, "Unable to retrieve raw classpath for project " + javaProject.getProject().getName()); } } }
Example 19
Source File: ProjectUtils.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static boolean addSourcePath(IPath sourcePath, IPath[] exclusionPaths, IJavaProject project) throws CoreException { IClasspathEntry[] existingEntries = project.getRawClasspath(); List<IPath> parentSrcPaths = new ArrayList<>(); List<IPath> exclusionPatterns = new ArrayList<>(); for (IClasspathEntry entry : existingEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (entry.getPath().equals(sourcePath)) { return false; } else if (entry.getPath().isPrefixOf(sourcePath)) { parentSrcPaths.add(entry.getPath()); } else if (sourcePath.isPrefixOf(entry.getPath())) { exclusionPatterns.add(entry.getPath().makeRelativeTo(sourcePath).addTrailingSeparator()); } } } if (!parentSrcPaths.isEmpty()) { throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, Messages .format("Cannot add the folder ''{0}'' to the source path because it''s parent folder is already in the source path of the project ''{1}''.", new String[] { sourcePath.toOSString(), project.getProject().getName() }))); } if (exclusionPaths != null) { for (IPath exclusion : exclusionPaths) { if (sourcePath.isPrefixOf(exclusion) && !sourcePath.equals(exclusion)) { exclusionPatterns.add(exclusion.makeRelativeTo(sourcePath).addTrailingSeparator()); } } } IClasspathEntry[] newEntries = new IClasspathEntry[existingEntries.length + 1]; System.arraycopy(existingEntries, 0, newEntries, 0, existingEntries.length); newEntries[newEntries.length - 1] = JavaCore.newSourceEntry(sourcePath, exclusionPatterns.toArray(new IPath[0])); project.setRawClasspath(newEntries, project.getOutputLocation(), null); return true; }
Example 20
Source File: JREContainerProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public static IClasspathEntry getJREContainerEntry(IJavaProject javaProject) throws JavaModelException { IClasspathEntry defaultJREContainerEntry = getDefaultJREContainerEntry(); IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); for (IClasspathEntry classpathEntry : rawClasspath) { int entryKind = classpathEntry.getEntryKind(); if (entryKind == IClasspathEntry.CPE_CONTAINER && defaultJREContainerEntry.getPath().isPrefixOf(classpathEntry.getPath())) { return classpathEntry; } } return null; }