List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER
int CPE_CONTAINER
To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER.
Click Source Link
From source file:com.android.ide.eclipse.adt.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>//from w w w . jav a 2 s .co m * <li>The project does not reference any old android.zip/android.jar archive.</li> * <li>The project does not use its output folder as a sourc folder.</li> * <li>The project does not reference a desktop JRE</li> * <li>The project references the AndroidClasspathContainer. * </ul> * @param javaProject The project to fix. * @throws JavaModelException */ public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException { // get the project classpath IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] oldEntries = entries; // check if the JRE is set as library int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER, IClasspathEntry.CPE_CONTAINER); if (jreIndex != -1) { // the project has a JRE included, we remove it entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex); } // get the output folder IPath outputFolder = javaProject.getOutputLocation(); boolean foundContainer = false; for (int i = 0; i < entries.length;) { // get the entry and kind IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath(); if (path.equals(outputFolder)) { entries = ProjectHelper.removeEntryFromClasspath(entries, i); // continue, to skip the i++; continue; } } else if (kind == IClasspathEntry.CPE_CONTAINER) { if (AndroidClasspathContainerInitializer.checkPath(entry.getPath())) { foundContainer = true; } } i++; } // if the framework container is not there, we add it if (foundContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, AndroidClasspathContainerInitializer.getContainerEntry()); } // set the new list of entries to the project if (entries != oldEntries) { javaProject.setRawClasspath(entries, new NullProgressMonitor()); } // If needed, check and fix compiler compliance and source compatibility ProjectHelper.checkAndFixCompilerCompliance(javaProject); }
From source file:com.android.ide.eclipse.adt.project.ProjectHelperTest.java
License:Open Source License
public final void testFixProjectClasspathEntriesFromOldContainer() throws JavaModelException { // create a project with a path to an android .zip JavaProjectMock javaProject = new JavaProjectMock( new IClasspathEntry[] { new ClasspathEntryMock(new Path("Project/src"), //$NON-NLS-1$ IClasspathEntry.CPE_SOURCE), new ClasspathEntryMock(new Path(OLD_CONTAINER_ID), IClasspathEntry.CPE_CONTAINER), }, new Path("Project/bin")); ProjectHelper.fixProjectClasspathEntries(javaProject); IClasspathEntry[] fixedEntries = javaProject.getRawClasspath(); assertEquals(3, fixedEntries.length); assertEquals("Project/src", fixedEntries[0].getPath().toString()); assertEquals(OLD_CONTAINER_ID, fixedEntries[1].getPath().toString()); assertEquals(CONTAINER_ID, fixedEntries[2].getPath().toString()); }
From source file:com.android.ide.eclipse.auidt.internal.build.BuildHelper.java
License:Open Source License
/** * Computes all the project output and dependencies that must go into building the apk. * * @param resMarker/*from w ww .j av a 2 s . c o m*/ * @throws CoreException */ private void gatherPaths(ResourceMarker resMarker) throws CoreException { IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); // get a java project for the project. IJavaProject javaProject = JavaCore.create(mProject); // get the output of the main project IPath path = javaProject.getOutputLocation(); IResource outputResource = wsRoot.findMember(path); if (outputResource != null && outputResource.getType() == IResource.FOLDER) { mCompiledCodePaths.add(outputResource.getLocation().toOSString()); } // we could use IJavaProject.getResolvedClasspath directly, but we actually // want to see the containers themselves. IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { // ignore non exported entries, unless it's the LIBRARIES container, // in which case we always want it (there may be some older projects that // have it as non exported). if (e.isExported() || (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER && e.getPath().toString().equals(AdtConstants.CONTAINER_LIBRARIES))) { handleCPE(e, javaProject, wsRoot, resMarker); } } } }
From source file:com.android.ide.eclipse.auidt.internal.build.BuildHelper.java
License:Open Source License
private void handleCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot, ResourceMarker resMarker) {/*from w w w . jav a2 s. c om*/ // if this is a classpath variable reference, we resolve it. if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { entry = JavaCore.getResolvedClasspathEntry(entry); } if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { IProject refProject = wsRoot.getProject(entry.getPath().lastSegment()); try { // ignore if it's an Android project, or if it's not a Java Project if (refProject.hasNature(JavaCore.NATURE_ID) && refProject.hasNature(AdtConstants.NATURE_DEFAULT) == false) { IJavaProject refJavaProject = JavaCore.create(refProject); // get the output folder IPath path = refJavaProject.getOutputLocation(); IResource outputResource = wsRoot.findMember(path); if (outputResource != null && outputResource.getType() == IResource.FOLDER) { mCompiledCodePaths.add(outputResource.getLocation().toOSString()); } } } catch (CoreException exception) { // can't query the project nature? ignore } } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { handleClasspathLibrary(entry, wsRoot, resMarker); } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { // get the container try { IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject); // ignore the system and default_system types as they represent // libraries that are part of the runtime. if (container.getKind() == IClasspathContainer.K_APPLICATION) { IClasspathEntry[] entries = container.getClasspathEntries(); for (IClasspathEntry cpe : entries) { handleCPE(cpe, javaProject, wsRoot, resMarker); } } } catch (JavaModelException jme) { // can't resolve the container? ignore it. AdtPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath()); } } }
From source file:com.android.ide.eclipse.auidt.internal.project.LibraryClasspathContainerInitializer.java
License:Open Source License
private static IClasspathContainer allocateLibraryContainer(IJavaProject javaProject) { final IProject iProject = javaProject.getProject(); AdtPlugin plugin = AdtPlugin.getDefault(); if (plugin == null) { // This is totally weird, but I've seen it happen! return null; }/*from www . j a v a 2 s . c o m*/ // First check that the project has a library-type container. try { IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); IClasspathEntry[] oldRawClasspath = rawClasspath; boolean foundLibrariesContainer = false; for (IClasspathEntry entry : rawClasspath) { // get the entry and kind int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_CONTAINER) { String path = entry.getPath().toString(); if (AdtConstants.CONTAINER_LIBRARIES.equals(path)) { foundLibrariesContainer = true; break; } } } // if there isn't any, add it. if (foundLibrariesContainer == false) { // add the android container to the array rawClasspath = ProjectHelper.addEntryToClasspath(rawClasspath, JavaCore .newContainerEntry(new Path(AdtConstants.CONTAINER_LIBRARIES), true /*isExported*/)); } // set the new list of entries to the project if (rawClasspath != oldRawClasspath) { javaProject.setRawClasspath(rawClasspath, new NullProgressMonitor()); } } catch (JavaModelException e) { // This really shouldn't happen, but if it does, simply return null (the calling // method will fails as well) return null; } // check if the project has a valid target. ProjectState state = Sdk.getProjectState(iProject); if (state == null) { // getProjectState should already have logged an error. Just bail out. return null; } /* * At this point we're going to gather a list of all that need to go in the * dependency container. * - Library project outputs (direct and indirect) * - Java project output (those can be indirectly referenced through library projects * or other other Java projects) * - Jar files: * + inside this project's libs/ * + inside the library projects' libs/ * + inside the referenced Java projects' classpath */ List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(); IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); // list of java project dependencies and jar files that will be built while // going through the library projects. Set<File> jarFiles = new HashSet<File>(); Set<IProject> refProjects = new HashSet<IProject>(); // process all the libraries List<IProject> libProjects = state.getFullLibraryProjects(); for (IProject libProject : libProjects) { // get the project output IFolder outputFolder = BaseProjectHelper.getAndroidOutputFolder(libProject); if (outputFolder != null) { // can happen when closing/deleting a library) IFile jarIFile = outputFolder.getFile(libProject.getName().toLowerCase() + AdtConstants.DOT_JAR); // get the source folder for the library project List<IPath> srcs = BaseProjectHelper.getSourceClasspaths(libProject); // find the first non-derived source folder. IPath sourceFolder = null; for (IPath src : srcs) { IFolder srcFolder = workspaceRoot.getFolder(src); if (srcFolder.isDerived() == false) { sourceFolder = src; break; } } // we can directly add a CPE for this jar as there's no risk of a duplicate. IClasspathEntry entry = JavaCore.newLibraryEntry(jarIFile.getLocation(), sourceFolder, // source attachment path null, // default source attachment root path. true /*isExported*/); entries.add(entry); // process all of the library project's dependencies getDependencyListFromClasspath(libProject, refProjects, jarFiles, true); // and the content of its libs folder. getJarListFromLibsFolder(libProject, jarFiles); } } // now process this projects' referenced projects only. processReferencedProjects(iProject, refProjects, jarFiles); // and the content of its libs folder getJarListFromLibsFolder(iProject, jarFiles); // annotations support for older version of android if (state.getTarget() != null && state.getTarget().getVersion().getApiLevel() <= 15) { File annotationsJar = new File(Sdk.getCurrent().getSdkLocation(), SdkConstants.FD_TOOLS + File.separator + SdkConstants.FD_SUPPORT + File.separator + SdkConstants.FN_ANNOTATIONS_JAR); jarFiles.add(annotationsJar); } // now add a classpath entry for each Java project (this is a set so dups are already // removed) for (IProject p : refProjects) { entries.add(JavaCore.newProjectEntry(p.getFullPath(), true /*isExported*/)); } // and process the jar files list, but first sanitize it to remove dups. JarListSanitizer sanitizer = new JarListSanitizer( iProject.getFolder(SdkConstants.FD_OUTPUT).getLocation().toFile(), new AndroidPrintStream(iProject, null /*prefix*/, AdtPlugin.getOutStream())); String errorMessage = null; try { List<File> sanitizedList = sanitizer.sanitize(jarFiles); for (File jarFile : sanitizedList) { if (jarFile instanceof CPEFile) { CPEFile cpeFile = (CPEFile) jarFile; IClasspathEntry e = cpeFile.getClasspathEntry(); entries.add(JavaCore.newLibraryEntry(e.getPath(), e.getSourceAttachmentPath(), e.getSourceAttachmentRootPath(), e.getAccessRules(), e.getExtraAttributes(), true /*isExported*/)); } else { String jarPath = jarFile.getAbsolutePath(); IPath sourceAttachmentPath = null; IClasspathAttribute javaDocAttribute = null; File jarProperties = new File(jarPath + DOT_PROPERTIES); if (jarProperties.isFile()) { Properties p = new Properties(); InputStream is = null; try { p.load(is = new FileInputStream(jarProperties)); String value = p.getProperty(ATTR_SRC); if (value != null) { File srcPath = getFile(jarFile, value); if (srcPath.exists()) { sourceAttachmentPath = new Path(srcPath.getAbsolutePath()); } } value = p.getProperty(ATTR_DOC); if (value != null) { File docPath = getFile(jarFile, value); if (docPath.exists()) { try { javaDocAttribute = JavaCore.newClasspathAttribute( IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, docPath.toURI().toURL().toString()); } catch (MalformedURLException e) { AdtPlugin.log(e, "Failed to process 'doc' attribute for %s", jarProperties.getAbsolutePath()); } } } } catch (FileNotFoundException e) { // shouldn't happen since we check upfront } catch (IOException e) { AdtPlugin.log(e, "Failed to read %s", jarProperties.getAbsolutePath()); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // ignore } } } } if (javaDocAttribute != null) { entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath, null /*sourceAttachmentRootPath*/, new IAccessRule[0], new IClasspathAttribute[] { javaDocAttribute }, true /*isExported*/)); } else { entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath, null /*sourceAttachmentRootPath*/, true /*isExported*/)); } } } } catch (DifferentLibException e) { errorMessage = e.getMessage(); AdtPlugin.printErrorToConsole(iProject, (Object[]) e.getDetails()); } catch (Sha1Exception e) { errorMessage = e.getMessage(); } processError(iProject, errorMessage, AdtConstants.MARKER_DEPENDENCY, true /*outputToConsole*/); return new AndroidClasspathContainer(entries.toArray(new IClasspathEntry[entries.size()]), new Path(AdtConstants.CONTAINER_LIBRARIES), "Android Dependencies", IClasspathContainer.K_APPLICATION); }
From source file:com.android.ide.eclipse.auidt.internal.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>//ww w . j av a2 s.c o m * <li>The project does not reference any old android.zip/android.jar archive.</li> * <li>The project does not use its output folder as a sourc folder.</li> * <li>The project does not reference a desktop JRE</li> * <li>The project references the AndroidClasspathContainer. * </ul> * @param javaProject The project to fix. * @throws JavaModelException */ public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException { // get the project classpath IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] oldEntries = entries; // check if the JRE is set as library int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER, IClasspathEntry.CPE_CONTAINER); if (jreIndex != -1) { // the project has a JRE included, we remove it entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex); } // get the output folder IPath outputFolder = javaProject.getOutputLocation(); boolean foundFrameworkContainer = false; boolean foundLibrariesContainer = false; for (int i = 0; i < entries.length;) { // get the entry and kind IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath(); if (path.equals(outputFolder)) { entries = ProjectHelper.removeEntryFromClasspath(entries, i); // continue, to skip the i++; continue; } } else if (kind == IClasspathEntry.CPE_CONTAINER) { String path = entry.getPath().toString(); if (AdtConstants.CONTAINER_FRAMEWORK.equals(path)) { foundFrameworkContainer = true; } if (AdtConstants.CONTAINER_LIBRARIES.equals(path)) { foundLibrariesContainer = true; } } i++; } // same thing for the library container if (foundLibrariesContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_LIBRARIES))); } // if the framework container is not there, we add it if (foundFrameworkContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_FRAMEWORK))); } // set the new list of entries to the project if (entries != oldEntries) { javaProject.setRawClasspath(entries, new NullProgressMonitor()); } // If needed, check and fix compiler compliance and source compatibility ProjectHelper.checkAndFixCompilerCompliance(javaProject); }
From source file:com.centurylink.mdw.plugin.project.assembly.ProjectConfigurator.java
License:Apache License
public void addJavaProjectSourceFolder(String sourceFolder, String outputFolder, IProgressMonitor monitor) throws CoreException, JavaModelException { if (!isJavaCapable()) return;//from w ww . j a v a2s. c o m IFolder folder = project.getSourceProject().getFolder(sourceFolder); if (!folder.exists()) folder.create(true, true, monitor); // projects with newly-added Java Nature contain root source entry, // which must be removed IClasspathEntry rootEntry = JavaCore.newSourceEntry(project.getSourceProject().getFullPath()); IPath outputPath = outputFolder == null ? null : project.getSourceProject().getFolder(outputFolder).getFullPath(); IClasspathEntry newEntry = JavaCore.newSourceEntry(folder.getFullPath(), ClasspathEntry.EXCLUDE_NONE, outputPath); boolean includesContainer = false; List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>(); IClasspathEntry toRemove = null; for (IClasspathEntry existingEntry : project.getSourceJavaProject().getRawClasspath()) { if (newEntry.equals(existingEntry)) return; // already on classpath else if (newEntry.getPath().equals(existingEntry.getPath()) && newEntry.getEntryKind() == existingEntry.getEntryKind()) toRemove = existingEntry; // to be replaced with new entry if (!rootEntry.equals(existingEntry)) classpathEntries.add(existingEntry); if (existingEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && String.valueOf(existingEntry.getPath()).indexOf("JRE_CONTAINER") >= 0) includesContainer = true; } if (toRemove != null) classpathEntries.remove(toRemove); classpathEntries.add(newEntry); if (!includesContainer) { IClasspathEntry jre = getJreContainerClasspathEntry(project.getJavaVersion()); if (jre == null) jre = JavaRuntime.getDefaultJREContainerEntry(); // fallback to // any // available classpathEntries.add(jre); } project.getSourceJavaProject().setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]), monitor); J2EEComponentClasspathUpdater.getInstance().queueUpdate(project.getSourceProject()); }
From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java
License:Open Source License
/** * Returns the kind of a <code>PackageFragmentRoot</code> from its <code>String</code> form. *///from w ww . ja va 2 s .c om static int kindFromString(String kindStr) { if (kindStr.equalsIgnoreCase("prj")) //$NON-NLS-1$ return IClasspathEntry.CPE_PROJECT; if (kindStr.equalsIgnoreCase("var")) //$NON-NLS-1$ return IClasspathEntry.CPE_VARIABLE; if (kindStr.equalsIgnoreCase("con")) //$NON-NLS-1$ return IClasspathEntry.CPE_CONTAINER; if (kindStr.equalsIgnoreCase("src")) //$NON-NLS-1$ return IClasspathEntry.CPE_SOURCE; if (kindStr.equalsIgnoreCase("lib")) //$NON-NLS-1$ return IClasspathEntry.CPE_LIBRARY; if (kindStr.equalsIgnoreCase("output")) //$NON-NLS-1$ return ClasspathEntry.K_OUTPUT; return -1; }
From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java
License:Open Source License
/** * Returns a <code>String</code> for the kind of a class path entry. */// w ww .j a v a 2s .co m static String kindToString(int kind) { switch (kind) { case IClasspathEntry.CPE_PROJECT: return "src"; // backward compatibility //$NON-NLS-1$ case IClasspathEntry.CPE_SOURCE: return "src"; //$NON-NLS-1$ case IClasspathEntry.CPE_LIBRARY: return "lib"; //$NON-NLS-1$ case IClasspathEntry.CPE_VARIABLE: return "var"; //$NON-NLS-1$ case IClasspathEntry.CPE_CONTAINER: return "con"; //$NON-NLS-1$ case ClasspathEntry.K_OUTPUT: return "output"; //$NON-NLS-1$ default: return "unknown"; //$NON-NLS-1$ } }
From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java
License:Open Source License
/** * Returns a printable representation of this classpath entry. */// w w w . j av a 2 s.c om 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(); }