List of usage examples for org.eclipse.jdt.core IClasspathEntry getPath
IPath getPath();
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 . j av a 2s. 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.build.BuildHelper.java
License:Open Source License
private void handleClasspathLibrary(IClasspathEntry e, IWorkspaceRoot wsRoot, ResourceMarker resMarker) { // get the IPath IPath path = e.getPath(); IResource resource = wsRoot.findMember(path); if (resource != null && resource.getType() == IResource.PROJECT) { // if it's a project we should just ignore it because it's going to be added // later when we add all the referenced projects. } else if (AdtConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) { // case of a jar file (which could be relative to the workspace or a full path) if (resource != null && resource.exists() && resource.getType() == IResource.FILE) { mCompiledCodePaths.add(resource.getLocation().toOSString()); } else {/* ww w. j av a 2 s .com*/ // if the jar path doesn't match a workspace resource, // then we get an OSString and check if this links to a valid file. String osFullPath = path.toOSString(); File f = new File(osFullPath); if (f.isFile()) { mCompiledCodePaths.add(osFullPath); } else { String message = String.format(Messages.Couldnt_Locate_s_Error, path); // always output to the console mOutStream.println(message); // put a marker if (resMarker != null) { resMarker.setWarning(mProject, message); } } } } else { // this can be the case for a class folder. if (resource != null && resource.exists() && resource.getType() == IResource.FOLDER) { mCompiledCodePaths.add(resource.getLocation().toOSString()); } else { // if the path doesn't match a workspace resource, // then we get an OSString and check if this links to a valid folder. String osFullPath = path.toOSString(); File f = new File(osFullPath); if (f.isDirectory()) { mCompiledCodePaths.add(osFullPath); } } } }
From source file:com.android.ide.eclipse.auidt.internal.lint.EclipseLintClient.java
License:Open Source License
@Override @NonNull//from w ww .jav a2s .c om protected ClassPathInfo getClassPath(@NonNull Project project) { ClassPathInfo info; if (mProjectInfo == null) { mProjectInfo = Maps.newHashMap(); info = null; } else { info = mProjectInfo.get(project); } if (info == null) { List<File> sources = null; List<File> classes = null; List<File> libraries = null; IProject p = getProject(project); if (p != null) { try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(p); // Output path File file = workspacePathToFile(javaProject.getOutputLocation()); classes = Collections.singletonList(file); // Source path IClasspathEntry[] entries = javaProject.getRawClasspath(); sources = new ArrayList<File>(entries.length); libraries = new ArrayList<File>(entries.length); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_VARIABLE) { entry = JavaCore.getResolvedClasspathEntry(entry); kind = entry.getEntryKind(); } if (kind == IClasspathEntry.CPE_SOURCE) { sources.add(workspacePathToFile(entry.getPath())); } else if (kind == IClasspathEntry.CPE_LIBRARY) { libraries.add(entry.getPath().toFile()); } // Note that we ignore IClasspathEntry.CPE_CONTAINER: // Normal Android Eclipse projects supply both // AdtConstants.CONTAINER_FRAMEWORK // and // AdtConstants.CONTAINER_LIBRARIES // here. We ignore the framework classes for obvious reasons, // but we also ignore the library container because lint will // process the libraries differently. When Eclipse builds a // project, it gets the .jar output of the library projects // from this container, which means it doesn't have to process // the library sources. Lint on the other hand wants to process // the source code, so instead it actually looks at the // project.properties file to find the libraries, and then it // iterates over all the library projects in turn and analyzes // those separately (but passing the main project for context, // such that the including project's manifest declarations // are used for data like minSdkVersion level). // // Note that this container will also contain *other* // libraries (Java libraries, not library projects) that we // *should* include. However, we can't distinguish these // class path entries from the library project jars, // so instead of looking at these, we simply listFiles() in // the libs/ folder after processing the classpath info } // Add in libraries File libs = new File(project.getDir(), FD_NATIVE_LIBS); if (libs.isDirectory()) { File[] jars = libs.listFiles(); if (jars != null) { for (File jar : jars) { if (AdtUtils.endsWith(jar.getPath(), DOT_JAR)) { libraries.add(jar); } } } } } catch (CoreException e) { AdtPlugin.log(e, null); } } if (sources == null) { sources = super.getClassPath(project).getSourceFolders(); } if (classes == null) { classes = super.getClassPath(project).getClassFolders(); } if (libraries == null) { libraries = super.getClassPath(project).getLibraries(); } info = new ClassPathInfo(sources, classes, libraries); mProjectInfo.put(project, info); } return info; }
From source file:com.android.ide.eclipse.auidt.internal.project.AndroidClasspathContainerInitializer.java
License:Open Source License
@Override public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException { AdtPlugin plugin = AdtPlugin.getDefault(); synchronized (Sdk.getLock()) { boolean sdkIsLoaded = plugin.getSdkLoadStatus() == LoadStatus.LOADED; // check if the project has a valid target. IAndroidTarget target = null;//from w ww . j a v a 2 s . c o m if (sdkIsLoaded) { target = Sdk.getCurrent().getTarget(project.getProject()); } if (sdkIsLoaded && target != null) { String[] paths = getTargetPaths(target); IPath android_lib = new Path(paths[CACHE_INDEX_JAR]); IClasspathEntry[] entries = containerSuggestion.getClasspathEntries(); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { IPath entryPath = entry.getPath(); if (entryPath != null) { if (entryPath.equals(android_lib)) { IPath entrySrcPath = entry.getSourceAttachmentPath(); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); if (entrySrcPath != null) { ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), entrySrcPath.toString()); } else { ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), null); } IClasspathAttribute[] extraAttributtes = entry.getExtraAttributes(); if (extraAttributtes.length == 0) { ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, NULL_API_URL); } for (int j = 0; j < extraAttributtes.length; j++) { IClasspathAttribute extraAttribute = extraAttributtes[j]; String value = extraAttribute.getValue(); if ((value == null || value.trim().length() == 0) && IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME .equals(extraAttribute.getName())) { value = NULL_API_URL; } if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME .equals(extraAttribute.getName())) { ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, value); } } } } } } rebindClasspathEntries(project.getJavaModel(), containerPath); } } }
From source file:com.android.ide.eclipse.auidt.internal.project.BaseProjectHelper.java
License:Open Source License
/** * returns a list of source classpath for a specified project * @param javaProject/*from w ww .j a v a 2 s. co m*/ * @return a list of path relative to the workspace root. */ public static List<IPath> getSourceClasspaths(IJavaProject javaProject) { ArrayList<IPath> sourceList = new ArrayList<IPath>(); IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourceList.add(e.getPath()); } } } return sourceList; }
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; }//ww w .jav a 2s.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.LibraryClasspathContainerInitializer.java
License:Open Source License
private static void handleClasspathLibrary(IClasspathEntry e, IWorkspaceRoot wsRoot, Set<File> jarFiles) { // get the IPath IPath path = e.getPath(); IResource resource = wsRoot.findMember(path); if (AdtConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) { // case of a jar file (which could be relative to the workspace or a full path) if (resource != null && resource.exists() && resource.getType() == IResource.FILE) { jarFiles.add(new CPEFile(resource.getLocation().toFile(), e)); } else {//from w w w.j a v a 2s. c om // if the jar path doesn't match a workspace resource, // then we get an OSString and check if this links to a valid file. String osFullPath = path.toOSString(); File f = new CPEFile(osFullPath, e); if (f.isFile()) { jarFiles.add(f); } } } }
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>//from w w w. j av a 2s . 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.android.ide.eclipse.auidt.internal.resources.manager.ProjectClassLoader.java
License:Open Source License
private void handleClassPathEntry(IClasspathEntry e, ArrayList<URL> oslibraryList) { // get the IPath IPath path = e.getPath(); // check the name ends with .jar if (AdtConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) { boolean local = false; IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path); if (resource != null && resource.exists() && resource.getType() == IResource.FILE) { local = true;//ww w .j a va 2 s . c om try { oslibraryList.add(new File(resource.getLocation().toOSString()).toURI().toURL()); } catch (MalformedURLException mue) { // pass } } if (local == false) { // if the jar path doesn't match a workspace resource, // then we get an OSString and check if this links to a valid file. String osFullPath = path.toOSString(); File f = new File(osFullPath); if (f.exists()) { try { oslibraryList.add(f.toURI().toURL()); } catch (MalformedURLException mue) { // pass } } } } }
From source file:com.android.ide.eclipse.auidt.internal.sourcelookup.AdtSourceLookupDirector.java
License:Open Source License
@Override public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException { dispose();/*from w w w. j a v a2 s.c o m*/ setLaunchConfiguration(configuration); String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$ if (projectName != null && projectName.length() > 0) { IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); if (project != null && project.isOpen()) { ProjectState state = Sdk.getProjectState(project); if (state == null) { initDefaults(); return; } IAndroidTarget target = state.getTarget(); if (target == null) { initDefaults(); return; } // String path = target.getPath(IAndroidTarget.ANDROID_JAR); String path = AdtPlugin.getAuidtJar(); if (path == null) { initDefaults(); return; } IJavaProject javaProject = JavaCore.create(project); if (javaProject != null && javaProject.isOpen()) { IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); IClasspathEntry androidEntry = null; for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && path.equals(entry.getPath().toString())) { androidEntry = entry; break; } } if (androidEntry != null) { IPath sourceAttachmentPath = androidEntry.getSourceAttachmentPath(); if (sourceAttachmentPath != null) { String androidSrc = sourceAttachmentPath.toString(); if (androidSrc != null && androidSrc.trim().length() > 0) { File srcFile = new File(androidSrc); ISourceContainer adtContainer = null; if (srcFile.isFile()) { adtContainer = new ExternalArchiveSourceContainer(androidSrc, true); } if (srcFile.isDirectory()) { adtContainer = new DirectorySourceContainer(srcFile, false); } if (adtContainer != null) { ISourceContainer defaultContainer = new DefaultSourceContainer(); setSourceContainers(new ISourceContainer[] { adtContainer, defaultContainer }); initializeParticipants(); return; } } } } } } } initDefaults(); }