List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT
int CPE_PROJECT
To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT.
Click Source Link
From source file:org.eclipse.che.jdt.internal.core.ClasspathEntry.java
License:Open Source License
/** * Returns a printable representation of this classpath entry. *///from ww w . j a va 2 s. c o m public String toString() { StringBuffer buffer = new StringBuffer(); // Object target = JavaModel.getTarget(getPath(), true); // if (target instanceof File) buffer.append(getPath().toOSString()); // else // buffer.append(String.valueOf(getPath())); buffer.append('['); switch (getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: buffer.append("CPE_LIBRARY"); //$NON-NLS-1$ break; case IClasspathEntry.CPE_PROJECT: buffer.append("CPE_PROJECT"); //$NON-NLS-1$ break; case IClasspathEntry.CPE_SOURCE: buffer.append("CPE_SOURCE"); //$NON-NLS-1$ break; case IClasspathEntry.CPE_VARIABLE: buffer.append("CPE_VARIABLE"); //$NON-NLS-1$ break; case IClasspathEntry.CPE_CONTAINER: buffer.append("CPE_CONTAINER"); //$NON-NLS-1$ break; } buffer.append("]["); //$NON-NLS-1$ switch (getContentKind()) { case IPackageFragmentRoot.K_BINARY: buffer.append("K_BINARY"); //$NON-NLS-1$ break; case IPackageFragmentRoot.K_SOURCE: buffer.append("K_SOURCE"); //$NON-NLS-1$ break; case ClasspathEntry.K_OUTPUT: buffer.append("K_OUTPUT"); //$NON-NLS-1$ break; } buffer.append(']'); if (getSourceAttachmentPath() != null) { buffer.append("[sourcePath:"); //$NON-NLS-1$ buffer.append(getSourceAttachmentPath()); buffer.append(']'); } if (getSourceAttachmentRootPath() != null) { buffer.append("[rootPath:"); //$NON-NLS-1$ buffer.append(getSourceAttachmentRootPath()); buffer.append(']'); } buffer.append("[isExported:"); //$NON-NLS-1$ buffer.append(this.isExported); buffer.append(']'); IPath[] patterns = this.inclusionPatterns; int length; if ((length = patterns == null ? 0 : patterns.length) > 0) { buffer.append("[including:"); //$NON-NLS-1$ for (int i = 0; i < length; i++) { buffer.append(patterns[i]); if (i != length - 1) { buffer.append('|'); } } buffer.append(']'); } patterns = this.exclusionPatterns; if ((length = patterns == null ? 0 : patterns.length) > 0) { buffer.append("[excluding:"); //$NON-NLS-1$ for (int i = 0; i < length; i++) { buffer.append(patterns[i]); if (i != length - 1) { buffer.append('|'); } } buffer.append(']'); } if (this.accessRuleSet != null) { buffer.append('['); buffer.append(this.accessRuleSet.toString(false/*on one line*/)); buffer.append(']'); } if (this.entryKind == CPE_PROJECT) { buffer.append("[combine access rules:"); //$NON-NLS-1$ buffer.append(this.combineAccessRules); buffer.append(']'); } if (getOutputLocation() != null) { buffer.append("[output:"); //$NON-NLS-1$ buffer.append(getOutputLocation()); buffer.append(']'); } if ((length = this.extraAttributes == null ? 0 : this.extraAttributes.length) > 0) { buffer.append("[attributes:"); //$NON-NLS-1$ for (int i = 0; i < length; i++) { buffer.append(this.extraAttributes[i]); if (i != length - 1) { buffer.append(','); } } buffer.append(']'); } return buffer.toString(); }
From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java
License:Open Source License
/** * Returns the package fragment root represented by the resource, or * the package fragment the given resource is located in, or <code>null</code> * if the given resource is not on the classpath of the given project. *//*w ww .ja v a 2 s . co m*/ public static IJavaElement determineIfOnClasspath(File resource, JavaProject project) { IPath resourcePath = new Path(resource.getAbsolutePath()); boolean isExternal = false; //ExternalFoldersManager.isInternalPathForExternalFolder(resourcePath); // if (isExternal) // resourcePath = resource.getLocation(); try { JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) ((JavaProject) project).manager .getInfo(project); JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache; HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null : projectCache.allPkgFragmentsCache; boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment()); IClasspathEntry[] entries = isJavaLike ? project.getRawClasspath() // JAVA file can only live inside SRC folder (on the raw path) : ((JavaProject) project).getResolvedClasspath(); int length = entries.length; if (length > 0) { String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); for (int i = 0; i < length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) continue; IPath rootPath = entry.getPath(); if (rootPath.equals(resourcePath)) { if (isJavaLike) return null; return project.getPackageFragmentRoot(resource); } else if (rootPath.isPrefixOf(resourcePath)) { // allow creation of package fragment if it contains a .java file that is included if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) { // given we have a resource child of the root, it cannot be a JAR pkg root PackageFragmentRoot root = // isExternal ? // new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) : (PackageFragmentRoot) ((JavaProject) project) .getFolderPackageFragmentRoot(rootPath); if (root == null) return null; IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount()); if (resource.isFile()) { // if the resource is a file, then remove the last segment which // is the file name in the package pkgPath = pkgPath.removeLastSegments(1); } String[] pkgName = pkgPath.segments(); // if package name is in the cache, then it has already been validated // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141) if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName)) return root.getPackageFragment(pkgName); if (pkgName.length != 0 && JavaConventions .validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel), sourceLevel, complianceLevel) .getSeverity() == IStatus.ERROR) { return null; } return root.getPackageFragment(pkgName); } } } } } catch (JavaModelException npe) { return null; } return null; }
From source file:org.eclipse.che.jdt.internal.core.JavaProject.java
License:Open Source License
/** * Returns the package fragment roots identified by the given entry. In case it refers to * a project, it will follow its classpath so as to find exported roots as well. * Only works with resolved entry/*from ww w . j a va 2s .co m*/ * * @param resolvedEntry * IClasspathEntry * @param accumulatedRoots * ObjectVector * @param rootIDs * HashSet * @param referringEntry * the CP entry (project) referring to this entry, or null if initial project * @param retrieveExportedRoots * boolean * @throws JavaModelException */ public void computePackageFragmentRoots(IClasspathEntry resolvedEntry, ObjectVector accumulatedRoots, HashSet rootIDs, IClasspathEntry referringEntry, boolean retrieveExportedRoots, Map rootToResolvedEntries) throws JavaModelException { String rootID = ((ClasspathEntry) resolvedEntry).rootID(); if (rootIDs.contains(rootID)) return; IPath projectPath = this.getFullPath(); IPath entryPath = resolvedEntry.getPath(); // IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); IPackageFragmentRoot root = null; switch (resolvedEntry.getEntryKind()) { // source folder case IClasspathEntry.CPE_SOURCE: // if (projectPath.isPrefixOf(entryPath)) { Object target1 = JavaModelManager.getTarget(entryPath, true/*check existency*/); if (target1 == null) return; if (target1 instanceof File && ((File) target1).isDirectory()) { root = getPackageFragmentRoot((File) target1); } // } break; // internal/external JAR or folder case IClasspathEntry.CPE_LIBRARY: if (referringEntry != null && !resolvedEntry.isExported()) return; Object target = JavaModelManager.getTarget(entryPath, true/*check existency*/); if (target == null) return; // if (target instanceof IResource){ // // internal target // } else if (target instanceof File) { // external target if (JavaModelManager.isFile(target)) { root = new JarPackageFragmentRoot((File) target, this, manager); } else if (((File) target).isDirectory()) { root = getPackageFragmentRoot((File) target, entryPath); // root = new ExternalPackageFragmentRoot(entryPath, this); } } break; // recurse into required project case IClasspathEntry.CPE_PROJECT: if (!retrieveExportedRoots) return; if (referringEntry != null && !resolvedEntry.isExported()) return; //todo multiproject // IResource member = workspaceRoot.findMember(entryPath); // if (member != null && member.getType() == IResource.PROJECT) {// double check if bound to project (23977) // IProject requiredProjectRsc = (IProject)member; // if (JavaProject.hasJavaNature(requiredProjectRsc)) { // special builder binary output // rootIDs.add(rootID); // JavaProject requiredProject = (JavaProject)JavaCore.create(requiredProjectRsc); // requiredProject.computePackageFragmentRoots( // requiredProject.getResolvedClasspath(), // accumulatedRoots, // rootIDs, // rootToResolvedEntries == null ? resolvedEntry // : ((org.eclipse.jdt.internal.core.ClasspathEntry)resolvedEntry) // .combineWith((org.eclipse.jdt.internal.core.ClasspathEntry)referringEntry), // // only combine if need to build the reverse map // retrieveExportedRoots, // rootToResolvedEntries); // } // break; // } } if (root != null) { accumulatedRoots.add(root); rootIDs.add(rootID); if (rootToResolvedEntries != null) rootToResolvedEntries.put(root, ((ClasspathEntry) resolvedEntry).combineWith((ClasspathEntry) referringEntry)); } }
From source file:org.eclipse.che.jdt.internal.core.search.IndexSelector.java
License:Open Source License
private static int canSeeFocus(IJavaElement focus, JavaProject javaProject, char[][][] focusQualifiedNames) { try {/* www . ja v a 2 s. co m*/ if (focus == null) return PROJECT_CAN_NOT_SEE_FOCUS; if (focus.equals(javaProject)) return PROJECT_CAN_SEE_FOCUS; if (focus instanceof JarPackageFragmentRoot) { // focus is part of a jar IPath focusPath = focus.getPath(); IClasspathEntry[] entries = javaProject.getExpandedClasspath(); for (int i = 0, length = entries.length; i < length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(focusPath)) return PROJECT_CAN_SEE_FOCUS; } return PROJECT_CAN_NOT_SEE_FOCUS; } // look for dependent projects IPath focusPath = ((JavaProject) focus).getProject().getFullPath(); IClasspathEntry[] entries = javaProject.getExpandedClasspath(); for (int i = 0, length = entries.length; i < length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT && entry.getPath().equals(focusPath)) { if (focusQualifiedNames != null) { // builder state is usable, hence use it to try to reduce project which can see the focus... State projectState = (State) JavaModelManager.getJavaModelManager() .getLastBuiltState(javaProject.getProject(), null); if (projectState != null) { Object[] values = projectState.getReferences().valueTable; int vLength = values.length; for (int j = 0; j < vLength; j++) { if (values[j] == null) continue; ReferenceCollection references = (ReferenceCollection) values[j]; if (references.includes(focusQualifiedNames, null, null)) { return PROJECT_CAN_SEE_FOCUS; } } return PROJECT_SOURCE_CAN_NOT_SEE_FOCUS; } } return PROJECT_CAN_SEE_FOCUS; } } return PROJECT_CAN_NOT_SEE_FOCUS; } catch (JavaModelException e) { return PROJECT_CAN_NOT_SEE_FOCUS; } }
From source file:org.eclipse.che.jdt.internal.core.search.JavaSearchScope.java
License:Open Source License
/** * Add a path to current java search scope or all project fragment roots if null. * Use project resolved classpath to retrieve and store access restriction on each classpath entry. * Recurse if dependent projects are found. * * @param javaProject//from w w w . j ava 2s. com * Project used to get resolved classpath entries * @param pathToAdd * Path to add in case of single element or null if user want to add all project package fragment roots * @param includeMask * Mask to apply on classpath entries * @param projectsToBeAdded * Set to avoid infinite recursion * @param visitedProjects * Set to avoid adding twice the same project * @param referringEntry * Project raw entry in referring project classpath * @throws org.eclipse.jdt.core.JavaModelException * May happen while getting java model info */ void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException { // IProject project = javaProject.getProject(); // if (!project.isAccessible() || !visitedProjects.add(project)) return; IPath projectPath = javaProject.getFullPath(); String projectPathString = projectPath.toString(); addEnclosingProjectOrJar(projectPath); IClasspathEntry[] entries = javaProject.getResolvedClasspath(); // IJavaModel model = javaProject.getJavaModel(); // JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo(); for (int i = 0, length = entries.length; i < length; i++) { IClasspathEntry entry = entries[i]; AccessRuleSet access = null; ClasspathEntry cpEntry = (ClasspathEntry) entry; if (referringEntry != null) { // Add only exported entries. // Source folder are implicitly exported. if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) { continue; } cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry); // cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry); } access = cpEntry.getAccessRuleSet(); switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: IClasspathEntry rawEntry = null; // Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries; // if (rootPathToRawEntries != null) { // rawEntry = (IClasspathEntry)rootPathToRawEntries.get(entry.getPath()); // } // if (rawEntry == null) break; rawKind: switch (cpEntry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: case IClasspathEntry.CPE_VARIABLE: if ((includeMask & APPLICATION_LIBRARIES) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { // Object target = JavaModel.getTarget(path, false/*don't check existence*/); // if (target instanceof IFolder) // case of an external folder // path = ((IFolder)target).getFullPath(); String pathToString = path.getDevice() == null ? path.toString() : path.toOSString(); add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$ addEnclosingProjectOrJar(entry.getPath()); } } break; case IClasspathEntry.CPE_CONTAINER: IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject); if (container == null) break; switch (container.getKind()) { case IClasspathContainer.K_APPLICATION: if ((includeMask & APPLICATION_LIBRARIES) == 0) break rawKind; break; case IClasspathContainer.K_SYSTEM: case IClasspathContainer.K_DEFAULT_SYSTEM: if ((includeMask & SYSTEM_LIBRARIES) == 0) break rawKind; break; default: break rawKind; } IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { Object target = JavaModel.getTarget(path, false/*don't check existence*/); if (target instanceof IFolder) // case of an external folder path = ((IFolder) target).getFullPath(); String pathToString = path.getDevice() == null ? path.toString() : path.toOSString(); add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$ addEnclosingProjectOrJar(entry.getPath()); } break; } break; case IClasspathEntry.CPE_PROJECT: // if ((includeMask & REFERENCED_PROJECTS) != 0) { // IPath path = entry.getPath(); // if (pathToAdd == null || pathToAdd.equals(path)) { // JavaProject referencedProject = (JavaProject)model.getJavaProject(path.lastSegment()); // if (!projectsToBeAdded // .contains(referencedProject)) { // do not recurse if depending project was used to create the scope // add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry); // } // } // } break; case IClasspathEntry.CPE_SOURCE: if ((includeMask & SOURCES) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { add(projectPath.toString(), path.toOSString() .substring(projectPath.toString().length() + 1)/*Util.relativePath(path, 1*//*remove project segment*//*)*/, projectPathString, false/*not a package*/, access); } } break; } } }
From source file:org.eclipse.che.plugin.java.plain.server.rest.ClasspathUpdaterService.java
License:Open Source License
private IClasspathEntry[] createModifiedEntry(List<ClasspathEntryDto> entries) { List<IClasspathEntry> coreClasspathEntries = new ArrayList<>(entries.size()); for (ClasspathEntryDto entry : entries) { IPath path = fromOSString(entry.getPath()); int entryKind = entry.getEntryKind(); if (IClasspathEntry.CPE_LIBRARY == entryKind) { coreClasspathEntries.add(newLibraryEntry(path, null, null)); } else if (IClasspathEntry.CPE_SOURCE == entryKind) { coreClasspathEntries.add(newSourceEntry(path)); } else if (IClasspathEntry.CPE_VARIABLE == entryKind) { coreClasspathEntries.add(newVariableEntry(path, null, null)); } else if (IClasspathEntry.CPE_CONTAINER == entryKind) { coreClasspathEntries.add(newContainerEntry(path)); } else if (IClasspathEntry.CPE_PROJECT == entryKind) { coreClasspathEntries.add(newProjectEntry(path)); }//www. jav a2s . co m } return coreClasspathEntries.toArray(new IClasspathEntry[coreClasspathEntries.size()]); }
From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProvider.java
License:Open Source License
/** * Builds classpath for the java project. * * @param javaProject java project/*from ww w.ja v a2s .co m*/ * @return set of resources which are included to the classpath */ public Set<String> getProjectClassPath(IJavaProject javaProject) { try { IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false); Set<String> result = new HashSet<>(); for (IClasspathEntry classpathEntry : resolvedClasspath) { switch (classpathEntry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: IPath path = classpathEntry.getPath(); result.add(path.toOSString()); break; case IClasspathEntry.CPE_SOURCE: IPath outputLocation = classpathEntry.getOutputLocation(); if (outputLocation != null) { result.add(workspacePath + outputLocation.toOSString()); } break; case IClasspathEntry.CPE_PROJECT: IPath projectPath = classpathEntry.getPath(); JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel(); IJavaProject project = javaModel.getJavaProject(projectPath.toOSString()); result.addAll(getProjectClassPath(project)); break; } } return result; } catch (JavaModelException e) { LOG.debug(e.getMessage(), e); } return Collections.emptySet(); }
From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java
License:Open Source License
@Test public void classpathProviderShouldProvideClasspathPathsWithAnotherProject() throws Exception { JavaModel model = mock(JavaModel.class); Field javaModel = JavaModelManager.class.getDeclaredField("javaModel"); javaModel.setAccessible(true);/*from w w w .jav a 2 s .c om*/ javaModel.set(JavaModelManager.getJavaModelManager(), model); IClasspathEntry entry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "/anotherProject/src", "/anotherProject/target/classes"); IJavaProject anotherProject = mock(IJavaProject.class); when(anotherProject.getResolvedClasspath(false)).thenReturn(new IClasspathEntry[] { entry }); when(model.getJavaProject("/anotherProject")).thenReturn(anotherProject); IClasspathEntry classpathEntry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "", "/testProject/target/classes"); IClasspathEntry jarClasspathEntry = mockClasspathEntry(IClasspathEntry.CPE_LIBRARY, "/absolute/path/to/jar.file", null); IClasspathEntry projectEntry = mockClasspathEntry(IClasspathEntry.CPE_PROJECT, "/anotherProject", null); IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry, projectEntry }; when(javaProject.getResolvedClasspath(false)).thenReturn(entries); Set<String> classPath = classpathProvider.getProjectClassPath(javaProject); assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes", "/absolute/path/to/jar.file", PROJECTS_PATH + "/anotherProject/target/classes"); }
From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathEntryHelper.java
License:Open Source License
public IClasspathEntry toClasspathEntry() { Map<String, String> attributes = new HashMap<String, String>(this.attributes); if (artifactKey != null) { attributes.put(ClasspathManager.GROUP_ID_ATTRIBUTE, artifactKey.getGroupId()); attributes.put(ClasspathManager.ARTIFACT_ID_ATTRIBUTE, artifactKey.getArtifactId()); attributes.put(ClasspathManager.VERSION_ATTRIBUTE, artifactKey.getVersion()); attributes.put(ClasspathManager.PACKAGING_ATTRIBUTE, artifactKey.getPackaging()); if (artifactKey.getClassifier() != null) { attributes.put(ClasspathManager.CLASSIFIER_ATTRIBUTE, artifactKey.getClassifier()); }// w w w . j a va 2 s .com } IClasspathAttribute[] attributesArray = new IClasspathAttribute[attributes.size()]; int attributeIndex = 0; for (Map.Entry<String, String> attribute : attributes.entrySet()) { attributesArray[attributeIndex++] = JavaCore.newClasspathAttribute(attribute.getKey(), attribute.getValue()); } IAccessRule[] accessRulesArray = accessRules.toArray(new IAccessRule[accessRules.size()]); IClasspathEntry entry; switch (kind) { case IClasspathEntry.CPE_CONTAINER: entry = JavaCore.newContainerEntry(path, // accessRulesArray, // attributesArray, // exported); break; case IClasspathEntry.CPE_LIBRARY: entry = JavaCore.newLibraryEntry(path, // sourcePath, // sourceRootPath, // accessRulesArray, // attributesArray, // exported); break; case IClasspathEntry.CPE_SOURCE: entry = JavaCore.newSourceEntry(path, // getInclusionPatterns(), // getExclusionPatterns(), // outputLocation, // attributesArray); break; case IClasspathEntry.CPE_PROJECT: entry = JavaCore.newProjectEntry(path, // accessRulesArray, // combineAccessRules, // attributesArray, // exported); break; case IClasspathEntry.CPE_VARIABLE: entry = JavaCore.newVariableEntry(path, // sourcePath, // sourceRootPath, // accessRulesArray, // attributesArray, // exported); break; default: throw new IllegalArgumentException("Unsupported IClasspathEntry kind=" + kind); //$NON-NLS-1$ } return entry; }
From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathHelper.java
License:Open Source License
public ClasspathEntryHelper addProjectEntry(IPath projectPath) { ClasspathEntryHelper helper = new ClasspathEntryHelper(projectPath, IClasspathEntry.CPE_PROJECT); addEntryHelper(helper);/*from w w w . jav a2 s . co m*/ return helper; }