private void addExclusionPatterns(IClasspathEntry newEntry, List<IClasspathEntry> existing, Set<IClasspathEntry> modifiedEntries) { IPath entryPath= newEntry.getPath(); for (int i= 0; i < existing.size(); i++) { IClasspathEntry curr= existing.get(i); IPath currPath= curr.getPath(); if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE && currPath.isPrefixOf(entryPath)) { IPath[] exclusionFilters= curr.getExclusionPatterns(); if (!JavaModelUtil.isExcludedPath(entryPath, exclusionFilters)) { IPath pathToExclude= entryPath.removeFirstSegments(currPath.segmentCount()).addTrailingSeparator(); IPath[] newExclusionFilters= new IPath[exclusionFilters.length + 1]; System.arraycopy(exclusionFilters, 0, newExclusionFilters, 0, exclusionFilters.length); newExclusionFilters[exclusionFilters.length]= pathToExclude; IClasspathEntry updated= JavaCore.newSourceEntry(currPath, newExclusionFilters, curr.getOutputLocation()); existing.set(i, updated); modifiedEntries.add(updated); } } } }
private void updateClasspath(IPath newPath, IProgressMonitor monitor) throws JavaModelException { IClasspathEntry[] classpath= fProject.getRawClasspath(); IPath jreContainerPath= new Path(JavaRuntime.JRE_CONTAINER); for (int i= 0; i < classpath.length; i++) { IClasspathEntry curr= classpath[i]; if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && curr.getPath().matchingFirstSegments(jreContainerPath) > 0) { classpath[i]= JavaCore.newContainerEntry(newPath, curr.getAccessRules(), curr.getExtraAttributes(), curr.isExported()); } } fProject.setRawClasspath(classpath, monitor); }
private static void updateProjectClasspath(IJavaProject javaProject, IClasspathEntry newEntry, IProgressMonitor monitor) throws JavaModelException { List<IClasspathEntry> newEntries = updateElements(javaProject.getRawClasspath(), newEntry, (entry) -> { return entry.getEntryKind() == newEntry.getEntryKind() && entry.getPath().equals(newEntry.getPath()); }); JavaLanguageServerPlugin.logInfo("Update source attachment " + (newEntry.getSourceAttachmentPath() == null ? null : newEntry.getSourceAttachmentPath().toOSString()) + " to the file " + newEntry.getPath().toOSString()); javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), monitor); }
private void setDeletedEntryProperties(IClasspathEntry entry){ fEntryKind= entry.getEntryKind(); fPath= entry.getPath(); fSourceAttachmentPath= entry.getSourceAttachmentPath(); fSourceAttachmentRootPath= entry.getSourceAttachmentRootPath(); }
@Override public Image getImage(Object obj) { IClasspathEntry entry = (IClasspathEntry) obj; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_PROJECT) return projectImage; else if (kind == IClasspathEntry.CPE_LIBRARY) { IPath sourceAtt = entry.getSourceAttachmentPath(); return sourceAtt != null ? slibraryImage : libraryImage; } return null; }
private IClasspathEntry addAttributes(IClasspathEntry entry, IClasspathAttribute[] extraAttributes) { switch (entry.getEntryKind()) { case IClasspathEntry.CPE_CONTAINER: return JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), extraAttributes, entry.isExported()); case IClasspathEntry.CPE_LIBRARY: return JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), extraAttributes, entry.isExported()); case IClasspathEntry.CPE_PROJECT: return JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(), entry.combineAccessRules(), extraAttributes, entry.isExported()); default: return entry; // other kinds are not handled } }
private static IPath determinePackageName(IPath path) { if (path != null) { final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0)); try { if (project != null && project.hasNature(JavaCore.NATURE_ID)) { final IJavaProject javaProject = JavaCore.create(project); for (final IClasspathEntry entry : javaProject.getRawClasspath()) { if (entry.getPath().isPrefixOf(path)) { return path.removeFirstSegments(entry.getPath().segmentCount()); } } } } catch (Exception e) { // Ignore the exceptions since they are not useful (hopefully) } } return null; }
private boolean isOnClasspathEntry(IPath elementPath, boolean isFolderPath, boolean isPackageFragmentRoot, IClasspathEntry entry) { IPath entryPath = entry.getPath(); if (isPackageFragmentRoot) { // package fragment roots must match exactly entry pathes (no exclusion there) if (entryPath.equals(elementPath)) return true; } else { if (entryPath.isPrefixOf(elementPath) && !Util.isExcluded(elementPath, ((ClasspathEntry)entry).fullInclusionPatternChars(), ((ClasspathEntry)entry).fullExclusionPatternChars(), isFolderPath)) return true; } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=276373 if (entryPath.isAbsolute() && entryPath.equals(ResourcesPlugin.getWorkspace().getRoot().getLocation().append(elementPath))) { return true; } return false; }
public static List<IFolder> getFoldersOfProject(IProject project, IPath path) throws CoreException { if (!project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID)) { return Collections.emptyList(); } IJavaProject jp = JavaCore.create(project); if (jp == null || !jp.exists()) { return Collections.emptyList(); } IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); // check runtime dir IClasspathEntry[] clEntries = jp.getRawClasspath(); List<IFolder> folders = new ArrayList<>(); for (IClasspathEntry entry : clEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath toCheck = entry.getPath().append(path); IFolder folder = root.getFolder(toCheck); if (folder != null && folder.exists()) { folders.add(folder); } } } // check path relative to project IFolder foundFolder = project.getFolder(path); if (foundFolder != null && foundFolder.exists()) { folders.add(foundFolder); } return folders; }
private IPackageFragmentRoot[] getRootsForOutputLocation(IJavaProject otherJavaProject, IResource outputLocation) throws JavaModelException { IPath outputPath = outputLocation.getFullPath(); List<IPackageFragmentRoot> result = new ArrayList<>(); if (outputPath.equals(otherJavaProject.getOutputLocation())) { // collect roots reporting to the default output location: for (IClasspathEntry classpathEntry : otherJavaProject.getRawClasspath()) { if (classpathEntry.getOutputLocation() == null) { for (IPackageFragmentRoot root : otherJavaProject.findPackageFragmentRoots(classpathEntry)) { IResource rootResource = root.getResource(); if (rootResource == null || !rootResource.getProject().equals(otherJavaProject.getProject())) continue; // outside this project result.add(root); } } } } if (!result.isEmpty()) return result.toArray(new IPackageFragmentRoot[result.size()]); // search an entry that specifically (and exclusively) reports to the output location: for (IClasspathEntry classpathEntry : otherJavaProject.getRawClasspath()) { if (outputPath.equals(classpathEntry.getOutputLocation())) return otherJavaProject.findPackageFragmentRoots(classpathEntry); } return null; }
private static IFile getSourceFile(IJavaProject javaProject, String baseName) throws JavaModelException { IClasspathEntry[] classpathEntries = javaProject.getRawClasspath(); for (int i = 0; i < classpathEntries.length; i++) { if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = classpathEntries[i].getPath().append( getFilePath(baseName)).removeFirstSegments(1); path = javaProject.getProject().getFullPath().append(path); IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile( path); if (file.exists()) { return file; } } } return null; }
public static class ClasspathResolutionBreakpointListener { public void breakpoint(int bp) { // override in listener implementation } }
private void saveClasspathEntry(IClasspathEntry entry) throws IOException { if (saveNewId(entry, this.classpathEntryIds)) { saveInt(entry.getContentKind()); saveInt(entry.getEntryKind()); savePath(entry.getPath()); savePaths(entry.getInclusionPatterns()); savePaths(entry.getExclusionPatterns()); savePath(entry.getSourceAttachmentPath()); savePath(entry.getSourceAttachmentRootPath()); savePath(entry.getOutputLocation()); this.out.writeBoolean(entry.isExported()); saveAccessRules(entry.getAccessRules()); this.out.writeBoolean(entry.combineAccessRules()); saveAttributes(entry.getExtraAttributes()); } }
public static boolean isOnSourcePath(IPath sourcePath, IJavaProject project) throws JavaModelException { for (IClasspathEntry entry : project.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().equals(sourcePath)) { return true; } } return false; }
private static IClasspathEntry getClasspathEntryForMarker(final IMarker marker, final String cpEntryPath) throws CoreException { final IProject proj = marker.getResource().getProject(); if (proj != null && proj.hasNature(JavaCore.NATURE_ID)) { final IJavaProject jProject = JavaCore.create(proj); if (cpEntryPath != null) { IClasspathEntry[] rawCp = jProject.getRawClasspath(); for (int i = 0; i < rawCp.length; i++) { if (rawCp[i].getPath().toString().equals(cpEntryPath)) { return rawCp[i]; } } } } return null; }
private static boolean isRootAt(IPackageFragmentRoot root, IPath entry) { try { IClasspathEntry cpe= root.getRawClasspathEntry(); if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath outputLocation= cpe.getOutputLocation(); if (outputLocation == null) outputLocation= root.getJavaProject().getOutputLocation(); IPath location= ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation(); if (entry.equals(location)) return true; } } catch (JavaModelException e) { JavaPlugin.log(e); } IResource resource= root.getResource(); if (resource != null && entry.equals(resource.getLocation())) return true; IPath path= root.getPath(); if (path != null && entry.equals(path)) return true; return false; }
private String[] getOutputFolders() { IProject project = getElement().getAdapter(IProject.class); ArrayList<String> list = new ArrayList<>(); try { if (project.hasNature(JavaCore.NATURE_ID)) { IJavaProject jProject = JavaCore.create(project); list.add(jProject.getOutputLocation().toString()); IClasspathEntry[] entries = jProject.getRawClasspath(); for (IClasspathEntry entry : entries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) { IPath path = entry.getOutputLocation(); if (path != null) list.add(path.toString()); } } } } catch (JavaModelException e) { } catch (CoreException e) { } return list.toArray(new String[list.size()]); }
private boolean isTestSource(IJavaProject project, ICompilationUnit cu) { try { IClasspathEntry[] resolvedClasspath= project.getResolvedClasspath(true); final IPath resourcePath= cu.getResource().getFullPath(); for (IClasspathEntry e : resolvedClasspath) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (e.isTest()) { if (e.getPath().isPrefixOf(resourcePath)) { return true; } } } } } catch (JavaModelException e) { return false; } return false; }
protected void setIgnoreOptionalProblems(String entryPath, IProgressMonitor monitor) throws JavaModelException { final IJavaProject jaxWsProject = getJaxWsProject(); final IClasspathEntry[] rawClasspathEntries = jaxWsProject.getRawClasspath(); final List<IClasspathEntry> newEntries = new ArrayList<>(rawClasspathEntries.length); final org.eclipse.core.runtime.Path entryPathToSearch = new org.eclipse.core.runtime.Path('/' + jaxWsProject.getElementName() + '/' + entryPath); for (IClasspathEntry entry : rawClasspathEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().equals(entryPathToSearch)) { IClasspathAttribute[] origAttributes = entry.getExtraAttributes(); List<IClasspathAttribute> newAttributes = new ArrayList<>(origAttributes.length + 1); for (IClasspathAttribute attrib : origAttributes) { if (!IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS.equals(attrib.getName())) { newAttributes.add(attrib); } } newAttributes.add(JavaCore.newClasspathAttribute(IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS, Boolean.TRUE.toString())); newEntries.add(JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(), entry.getExclusionPatterns(), entry.getOutputLocation(), newAttributes.toArray(new IClasspathAttribute[newAttributes.size()]))); } else { newEntries.add(entry); } } jaxWsProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[newEntries.size()]), monitor); }
public static boolean isProjectSourceFolder(CPListElement[] existing, IJavaProject project) { IPath projPath= project.getProject().getFullPath(); for (int i= 0; i < existing.length; i++) { IClasspathEntry curr= existing[i].getClasspathEntry(); if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) { if (projPath.equals(curr.getPath())) { return true; } } } return false; }