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.ajdt.core.AspectJCorePreferences.java
License:Open Source License
/** * Remove all occurrences of an attribute * @param javaProject//from ww w .java 2s . c om * @param attribute */ private static void removeAttribute(IJavaProject javaProject, IClasspathAttribute attribute) { try { IClasspathEntry[] cp = javaProject.getRawClasspath(); boolean changed = false; for (int i = 0; i < cp.length; i++) { IClasspathAttribute[] attributes = cp[i].getExtraAttributes(); boolean found = false; for (int j = 0; !found && (j < attributes.length); j++) { if (attributes[j].getName().equals(attribute.getName())) { found = true; } } if (found) { changed = true; IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length - 1]; int count = 0; for (int j = 0; j < attributes.length; j++) { if (!attributes[j].getName().equals(attribute.getName())) { newattrib[count++] = attributes[j]; } } switch (cp[i].getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: cp[i] = JavaCore.newLibraryEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(), cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib, cp[i].isExported()); break; case IClasspathEntry.CPE_VARIABLE: cp[i] = JavaCore.newVariableEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(), cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib, cp[i].isExported()); break; case IClasspathEntry.CPE_CONTAINER: cp[i] = JavaCore.newContainerEntry(cp[i].getPath(), cp[i].getAccessRules(), newattrib, cp[i].isExported()); break; case IClasspathEntry.CPE_PROJECT: cp[i] = JavaCore.newProjectEntry(cp[i].getPath(), cp[i].getAccessRules(), true, newattrib, cp[i].isExported()); break; } } } if (changed) { javaProject.setRawClasspath(cp, null); } } catch (JavaModelException e) { } }
From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java
License:Open Source License
private static void addEntryToJavaBuildPath(IJavaProject jp, IClasspathAttribute attribute, String path, int eKind) { IClasspathAttribute[] attributes = new IClasspathAttribute[] { attribute }; try {//from w w w. j a v a 2 s .c om IClasspathEntry[] originalCP = jp.getRawClasspath(); IClasspathEntry[] newCP = new IClasspathEntry[originalCP.length + 1]; IClasspathEntry cp = null; if (eKind == IClasspathEntry.CPE_LIBRARY) { cp = JavaCore.newLibraryEntry(new Path(path), null, null, new IAccessRule[0], attributes, false); } else if (eKind == IClasspathEntry.CPE_VARIABLE) { cp = JavaCore.newVariableEntry(new Path(path), null, null, new IAccessRule[0], attributes, false); } else if (eKind == IClasspathEntry.CPE_CONTAINER) { cp = JavaCore.newContainerEntry(new Path(path), null, attributes, false); } else if (eKind == IClasspathEntry.CPE_PROJECT) { cp = JavaCore.newProjectEntry(new Path(path), null, true, attributes, false); } // Update the raw classpath with the new entry. if (cp != null) { System.arraycopy(originalCP, 0, newCP, 0, originalCP.length); newCP[originalCP.length] = cp; jp.setRawClasspath(newCP, new NullProgressMonitor()); } } catch (JavaModelException e) { } catch (NumberFormatException e) { } }
From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java
License:Open Source License
private static String toEntryKind(String entryStr) { int entry = 0; if (entryStr.equals("SOURCE")) { //$NON-NLS-1$ entry = IClasspathEntry.CPE_SOURCE; } else if (entryStr.equals("LIBRARY")) { //$NON-NLS-1$ entry = IClasspathEntry.CPE_LIBRARY; } else if (entryStr.equals("PROJECT")) { //$NON-NLS-1$ entry = IClasspathEntry.CPE_PROJECT; } else if (entryStr.equals("VARIABLE")) { //$NON-NLS-1$ entry = IClasspathEntry.CPE_VARIABLE; } else if (entryStr.equals("CONTAINER")) { //$NON-NLS-1$ entry = IClasspathEntry.CPE_CONTAINER; }/*from w w w. ja v a 2 s. c om*/ return new Integer(entry).toString(); }
From source file:org.eclipse.ajdt.core.builder.AJBuilder.java
License:Open Source License
/** * returns a list of fully qualified names of entries on the classpath * that have been rebuilt since last build * @return// ww w. j a v a 2s. c o m */ private List /*String*/ getChangedRequiredProjects(long lastBuildTimestamp) { try { // first find all the projects that have changed since last build IProject[] projectsOnClasspath = getRequiredProjects(getProject(), true); List /*IProject*/ changedProjects = new ArrayList(); for (int i = 0; i < projectsOnClasspath.length; i++) { IProject project = projectsOnClasspath[i]; // get timestamp of last build for this project long otherTimestamp = -1; if (AspectJPlugin.isAJProject(project)) { AjCompiler compiler = AspectJPlugin.getDefault().getCompilerFactory() .getCompilerForProject(project); otherTimestamp = getLastBuildTimeStamp(compiler); } else if (project.hasNature(JavaCore.NATURE_ID)) { Object s = JavaModelManager.getJavaModelManager().getLastBuiltState(project, null); if (s != null && s instanceof State) { State state = (State) s; // need to use reflection to get at the last build time otherTimestamp = getLastBuildTime(state); } } else { otherTimestamp = -1; } if (lastBuildTimestamp <= otherTimestamp) { changedProjects.add(project); } } List /*String*/ changedEntries = new ArrayList(); Set /*String*/ noDups = new HashSet(); // used to ensure there are no dups // now that we have all the projects, need to find out what they contribute to // this project's path. could be itself, a jar, or a class folder if (changedProjects.size() > 0) { IClasspathEntry[] thisClasspath = JavaCore.create(getProject()).getResolvedClasspath(true); for (Iterator projIter = changedProjects.iterator(); projIter.hasNext();) { IProject changedProject = (IProject) projIter.next(); for (int i = 0; i < thisClasspath.length; i++) { IClasspathEntry classpathEntry = thisClasspath[i]; switch (classpathEntry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: if (changedProject.getFullPath().equals(classpathEntry.getPath())) { // resolve project and add all entries List toAdd = listOfClassPathEntriesToListOfString(AspectJCorePreferences .resolveDependentProjectClasspath(classpathEntry, changedProject)); for (Iterator pathIter = toAdd.iterator(); pathIter.hasNext();) { String pathStr = (String) pathIter.next(); if (!noDups.contains(pathStr)) { changedEntries.add(pathStr); noDups.add(pathStr); } } } break; case IClasspathEntry.CPE_LIBRARY: if (changedProject.getFullPath().isPrefixOf(classpathEntry.getPath())) { // only add if this path exists IWorkspaceRoot root = getProject().getWorkspace().getRoot(); IFile onPath = root.getFile(classpathEntry.getPath()); if (onPath.exists() || root.getFolder(onPath.getFullPath()).exists()) { // may be a folder String pathStr = onPath.getLocation().toPortableString(); if (!noDups.contains(pathStr)) { changedEntries.add(pathStr); noDups.add(pathStr); } } } } } } } // if all else went well, also add the inpath to the list of changed projects. // Adding the inpath always is just a conservative estimate of what has changed. // // For Java projects, we only know the last structural build time. Usually this is // fine, but if the Java project is on the inpath, then we care about the last // build of any kind, which we can't be sure of. // (Actually, we need to know this for Aspect path projects, but aspectj can give us // precise time of the last build. // // So, as a conservative estimate, put all inpath entries onto the list. Set inPathFiles = CoreCompilerConfiguration.getCompilerConfigurationForProject(getProject()) .getInpath(); if (inPathFiles != null) { for (Iterator fileIter = inPathFiles.iterator(); fileIter.hasNext();) { File inpathFile = (File) fileIter.next(); Path path = new Path(inpathFile.getAbsolutePath()); String pathStr = path.toPortableString(); if (!noDups.contains(pathStr)) { changedEntries.add(pathStr); noDups.add(pathStr); } } } return changedEntries; } catch (Exception e) { // something went wrong. // return null to imply everything's changed AspectJPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, AspectJPlugin.PLUGIN_ID, "Error determining list of entries on classpath that have changed.", e)); return null; } }
From source file:org.eclipse.ajdt.core.builder.AJBuilder.java
License:Open Source License
/** * This is taken straight from the JavaBuilder - and is what is returned * from the build method/* w ww .j a va 2 s. c o m*/ */ private IProject[] getRequiredProjects(IProject project, boolean includeBinaryPrerequisites) { JavaProject javaProject = (JavaProject) JavaCore.create(project); IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot(); if (javaProject == null || workspaceRoot == null) return new IProject[0]; ArrayList<IProject> projects = new ArrayList<IProject>(); try { IClasspathEntry[] entries = javaProject.getExpandedClasspath(); for (int i = 0, l = entries.length; i < l; i++) { IClasspathEntry entry = entries[i]; IPath path = entry.getPath(); IProject p = null; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: // missing projects are considered too p = workspaceRoot.getProject(path.lastSegment()); break; case IClasspathEntry.CPE_LIBRARY: if (includeBinaryPrerequisites && path.segmentCount() > 1) { // some binary resources on the class path can come from // projects that are not included in the project // references IResource resource = workspaceRoot.findMember(path.segment(0)); if (resource instanceof IProject) p = (IProject) resource; } } if (p != null && !projects.contains(p)) projects.add(p); } } catch (JavaModelException e) { return new IProject[0]; } IProject[] result = new IProject[projects.size()]; projects.toArray(result); return result; }
From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java
License:Open Source License
private boolean projectHasProjectDependency(IJavaProject proj, IProject projectDependedOn) throws JavaModelException { IClasspathEntry[] entries = proj.getRawClasspath(); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { if (entry.getPath().equals(projectDependedOn.getFullPath()) || entry.getPath().equals(projectDependedOn.getFullPath().makeAbsolute())) { return true; }/*w w w .j ava 2s. co m*/ } } return false; }
From source file:org.eclipse.ajdt.core.tests.builder.Bug99133Test.java
License:Open Source License
private void removeProjectDependency(IProject project, IProject projectDependedOn) throws JavaModelException { IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] cpEntry = javaProject.getRawClasspath(); List newEntries = new ArrayList(); for (int j = 0; j < cpEntry.length; j++) { IClasspathEntry entry = cpEntry[j]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { if (!entry.getPath().equals(projectDependedOn.getFullPath()) && !entry.getPath().equals(projectDependedOn.getFullPath().makeAbsolute())) { newEntries.add(entry);//from ww w.j a v a2 s . c o m } } else { newEntries.add(entry); } } IClasspathEntry[] newCP = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]); javaProject.setRawClasspath(newCP, null); }
From source file:org.eclipse.ajdt.internal.core.ajde.CoreOutputLocationManager.java
License:Open Source License
private void handleClassPathEntry(IJavaProject jp, IClasspathEntry cpe) throws JavaModelException { switch (cpe.getEntryKind()) { case IClasspathEntry.CPE_CONTAINER: IClasspathContainer container = JavaCore.getClasspathContainer(cpe.getPath(), jp); if (container != null && container.getKind() != IClasspathContainer.K_DEFAULT_SYSTEM) { IClasspathEntry[] cpes = container.getClasspathEntries(); for (int i = 0; i < cpes.length; i++) { handleClassPathEntry(jp, cpes[i]); }//from w w w . j a v a 2 s. c o m } break; case IClasspathEntry.CPE_LIBRARY: File libFile = pathToFile(cpe.getPath()); if (libFile.isDirectory()) { // ignore jar files if (libFile != null && !binFolderToProject.containsKey(libFile)) { binFolderToProject.put(libFile, jp.getProject()); } } break; case IClasspathEntry.CPE_PROJECT: IJavaProject jpClasspath = pathToProject(cpe.getPath()); if (jpClasspath != null) { mapProject(jpClasspath); } break; case IClasspathEntry.CPE_SOURCE: File outFile = pathToFile( cpe.getOutputLocation() == null ? jp.getOutputLocation() : cpe.getOutputLocation()); if (outFile != null && !binFolderToProject.containsKey(outFile)) { binFolderToProject.put(outFile, jp.getProject()); } break; case IClasspathEntry.CPE_VARIABLE: IClasspathEntry cpeResolved = JavaCore.getResolvedClasspathEntry(cpe); if (cpeResolved != null) { handleClassPathEntry(jp, cpeResolved); } break; } }
From source file:org.eclipse.ajdt.internal.core.builder.BuildClasspathResolver.java
License:Open Source License
private void computeClasspathLocations(IWorkspaceRoot root, JavaProject javaProject, SimpleLookupTable binaryLocationsPerProject) throws CoreException { /* Update cycle marker */ IMarker cycleMarker = javaProject.getCycleMarker(); if (cycleMarker != null) { int severity = JavaCore.ERROR.equals(javaProject.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true)) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING; if (severity != ((Integer) cycleMarker.getAttribute(IMarker.SEVERITY)).intValue()) cycleMarker.setAttribute(IMarker.SEVERITY, severity); }/*w w w .j av a2s . com*/ IClasspathEntry[] classpathEntries = javaProject.getExpandedClasspath(); ArrayList sLocations = new ArrayList(classpathEntries.length); ArrayList bLocations = new ArrayList(classpathEntries.length); nextEntry: for (int i = 0, l = classpathEntries.length; i < l; i++) { ClasspathEntry entry = (ClasspathEntry) classpathEntries[i]; IPath path = entry.getPath(); Object target = JavaModel.getTarget(path, true); if (target == null) continue nextEntry; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_SOURCE: if (!(target instanceof IContainer)) continue nextEntry; IPath outputPath = entry.getOutputLocation() != null ? entry.getOutputLocation() : javaProject.getOutputLocation(); IContainer outputFolder; if (outputPath.segmentCount() == 1) { outputFolder = javaProject.getProject(); } else { outputFolder = root.getFolder(outputPath); // AspectJ Change Begin // This method can be executing on the wrong thread, where createFolder() will hang, so don't do it! // if (!outputFolder.exists()) // createFolder(outputFolder); // AspectJ Change End } sLocations.add(ClasspathLocation.forSourceFolder((IContainer) target, outputFolder, entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars())); continue nextEntry; case IClasspathEntry.CPE_PROJECT: if (!(target instanceof IProject)) continue nextEntry; IProject prereqProject = (IProject) target; if (!JavaProject.hasJavaNature(prereqProject)) continue nextEntry; // if project doesn't have java nature or is not accessible JavaProject prereqJavaProject = (JavaProject) JavaCore.create(prereqProject); IClasspathEntry[] prereqClasspathEntries = prereqJavaProject.getRawClasspath(); ArrayList seen = new ArrayList(); nextPrereqEntry: for (int j = 0, m = prereqClasspathEntries.length; j < m; j++) { IClasspathEntry prereqEntry = prereqClasspathEntries[j]; if (prereqEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { Object prereqTarget = JavaModel.getTarget(prereqEntry.getPath(), true); if (!(prereqTarget instanceof IContainer)) continue nextPrereqEntry; IPath prereqOutputPath = prereqEntry.getOutputLocation() != null ? prereqEntry.getOutputLocation() : prereqJavaProject.getOutputLocation(); IContainer binaryFolder = prereqOutputPath.segmentCount() == 1 ? (IContainer) prereqProject : (IContainer) root.getFolder(prereqOutputPath); if (binaryFolder.exists() && !seen.contains(binaryFolder)) { seen.add(binaryFolder); ClasspathLocation bLocation = ClasspathLocation.forBinaryFolder(binaryFolder, true, entry.getAccessRuleSet()); bLocations.add(bLocation); if (binaryLocationsPerProject != null) { // normal builder mode ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject .get(prereqProject); if (existingLocations == null) { existingLocations = new ClasspathLocation[] { bLocation }; } else { int size = existingLocations.length; System.arraycopy(existingLocations, 0, existingLocations = new ClasspathLocation[size + 1], 0, size); existingLocations[size] = bLocation; } binaryLocationsPerProject.put(prereqProject, existingLocations); } } } } continue nextEntry; case IClasspathEntry.CPE_LIBRARY: if (target instanceof IResource) { IResource resource = (IResource) target; ClasspathLocation bLocation = null; if (resource instanceof IFile) { if (!(org.eclipse.jdt.internal.compiler.util.Util .isPotentialZipArchive(path.lastSegment()))) continue nextEntry; AccessRuleSet accessRuleSet = JavaCore.IGNORE.equals( javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null : entry.getAccessRuleSet(); bLocation = ClasspathLocation.forLibrary((IFile) resource, accessRuleSet); } else if (resource instanceof IContainer) { AccessRuleSet accessRuleSet = JavaCore.IGNORE.equals( javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null : entry.getAccessRuleSet(); bLocation = ClasspathLocation.forBinaryFolder((IContainer) target, false, accessRuleSet); // is library folder not output folder } bLocations.add(bLocation); if (binaryLocationsPerProject != null) { // normal builder mode IProject p = resource.getProject(); // can be the project being built ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject .get(p); if (existingLocations == null) { existingLocations = new ClasspathLocation[] { bLocation }; } else { int size = existingLocations.length; System.arraycopy(existingLocations, 0, existingLocations = new ClasspathLocation[size + 1], 0, size); existingLocations[size] = bLocation; } binaryLocationsPerProject.put(p, existingLocations); } } else if (target instanceof File) { if (!(org.eclipse.jdt.internal.compiler.util.Util.isPotentialZipArchive(path.lastSegment()))) continue nextEntry; AccessRuleSet accessRuleSet = JavaCore.IGNORE .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null : entry.getAccessRuleSet(); bLocations.add(ClasspathLocation.forLibrary(path.toString(), accessRuleSet)); } continue nextEntry; } } // now split the classpath locations... place the output folders ahead of the other .class file folders & jars ArrayList outputFolders = new ArrayList(1); this.sourceLocations = new ClasspathMultiDirectory[sLocations.size()]; if (!sLocations.isEmpty()) { sLocations.toArray(this.sourceLocations); // collect the output folders, skipping duplicates next: for (int i = 0, l = sourceLocations.length; i < l; i++) { ClasspathMultiDirectory md = sourceLocations[i]; IPath outputPath = md.binaryFolder.getFullPath(); for (int j = 0; j < i; j++) { // compare against previously walked source folders if (outputPath.equals(sourceLocations[j].binaryFolder.getFullPath())) { md.hasIndependentOutputFolder = sourceLocations[j].hasIndependentOutputFolder; continue next; } } outputFolders.add(md); // also tag each source folder whose output folder is an independent folder & is not also a source folder for (int j = 0, m = sourceLocations.length; j < m; j++) if (outputPath.equals(sourceLocations[j].sourceFolder.getFullPath())) continue next; md.hasIndependentOutputFolder = true; } } // combine the output folders with the binary folders & jars... place the output folders before other .class file folders & jars this.binaryLocations = new ClasspathLocation[outputFolders.size() + bLocations.size()]; int index = 0; for (int i = 0, l = outputFolders.size(); i < l; i++) this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i); for (int i = 0, l = bLocations.size(); i < l; i++) this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i); }
From source file:org.eclipse.ajdt.internal.ui.ajde.UIMessageHandler.java
License:Open Source License
private IResource tryToFindResource(String fileName, IProject project) { IResource ret = null;//from www. j av a 2 s . c o m String toFind = fileName.replace('\\', '/'); IJavaProject jProject = JavaCore.create(project); try { IClasspathEntry[] classpathEntries = jProject.getResolvedClasspath(false); for (int i = 0; i < classpathEntries.length; i++) { IClasspathEntry cpEntry = classpathEntries[i]; if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath sourcePath = cpEntry.getPath(); // remove the first segment because the findMember call // following always adds it back in under the covers (doh!) // and we end up with two first segments otherwise! sourcePath = sourcePath.removeFirstSegments(1); IResource memberResource = project.findMember(sourcePath); if (memberResource != null) { IResource[] srcContainer = new IResource[] { memberResource }; ret = findFile(srcContainer, toFind); } } else if (cpEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { IPath projPath = cpEntry.getPath(); IResource projResource = AspectJPlugin.getWorkspace().getRoot().findMember(projPath); if (projResource != null) { ret = findFile(new IResource[] { projResource }, toFind); } } if (ret != null) { break; } } } catch (JavaModelException jmEx) { AJDTErrorHandler.handleAJDTError(UIMessages.jmCoreException, jmEx); } if (ret == null) ret = project; return ret; }