List of usage examples for org.eclipse.jdt.core IJavaProject getOutputLocation
IPath getOutputLocation() throws JavaModelException;
From source file:com.android.ide.eclipse.adt.internal.lint.EclipseLintClient.java
License:Open Source License
@Override @NonNull//from w ww . j a va2 s. c o m 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); if (entry == null) { // It's possible that the variable is no longer valid; ignore continue; } 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 (SdkUtils.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(); } // No test folders in Eclipse: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=224708 List<File> tests = Collections.emptyList(); info = new ClassPathInfo(sources, classes, libraries, tests); mProjectInfo.put(project, info); } return info; }
From source file:com.android.ide.eclipse.adt.internal.project.BaseProjectHelper.java
License:Open Source License
/** * Returns the {@link IFolder} representing the output for the project for Android specific * files./*from ww w .ja v a 2 s .c om*/ * <p> * The project must be a java project and be opened, or the method will return null. * @param project the {@link IProject} * @return an IFolder item or null. */ public final static IFolder getJavaOutputFolder(IProject project) { try { if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) { // get a java project from the normal project object IJavaProject javaProject = JavaCore.create(project); IPath path = javaProject.getOutputLocation(); path = path.removeFirstSegments(1); return project.getFolder(path); } } catch (JavaModelException e) { // Let's do nothing and return null } catch (CoreException e) { // Let's do nothing and return null } return null; }
From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>/*from w w w. j a va2 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; boolean forceRewriteOfCPE = false; // 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; IClasspathEntry foundLibrariesContainer = null; IClasspathEntry foundDependenciesContainer = null; 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; } else if (AdtConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) { foundLibrariesContainer = entry; } else if (AdtConstants.CONTAINER_DEPENDENCIES.equals(path)) { foundDependenciesContainer = entry; } } i++; } // look to see if we have the m2eclipse nature boolean m2eNature = false; try { m2eNature = javaProject.getProject().hasNature("org.eclipse.m2e.core.maven2Nature"); } catch (CoreException e) { AdtPlugin.log(e, "Failed to query project %s for m2e nature", javaProject.getProject().getName()); } // if the framework container is not there, we add it if (!foundFrameworkContainer) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_FRAMEWORK))); } // same thing for the library container if (foundLibrariesContainer == null) { // add the exported libraries android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_PRIVATE_LIBRARIES), true)); } else if (!m2eNature && !foundLibrariesContainer.isExported()) { // the container is present but it's not exported and since there's no m2e nature // we do want it to be exported. // keep all the other parameters the same. entries = ProjectHelper.replaceEntryInClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_PRIVATE_LIBRARIES), foundLibrariesContainer.getAccessRules(), foundLibrariesContainer.getExtraAttributes(), true)); forceRewriteOfCPE = true; } // same thing for the dependencies container if (foundDependenciesContainer == null) { // add the android dependencies container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_DEPENDENCIES), true)); } else if (!m2eNature && !foundDependenciesContainer.isExported()) { // the container is present but it's not exported and since there's no m2e nature // we do want it to be exported. // keep all the other parameters the same. entries = ProjectHelper.replaceEntryInClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_DEPENDENCIES), foundDependenciesContainer.getAccessRules(), foundDependenciesContainer.getExtraAttributes(), true)); forceRewriteOfCPE = true; } // set the new list of entries to the project if (entries != oldEntries || forceRewriteOfCPE) { 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.internal.resources.manager.ProjectClassLoader.java
License:Open Source License
/** * Attempts to load a class from a project output folder. * @param project the project to load the class from * @param name the name of the class//from w ww .ja va2 s . com * @return a class object if found, null otherwise. */ private Class<?> loadFromProject(IJavaProject project, String name) { try { // get the project output folder. IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IPath outputLocation = project.getOutputLocation(); IResource outRes = root.findMember(outputLocation); if (outRes == null) { return null; } File outFolder = new File(outRes.getLocation().toOSString()); // get the class name segments String[] segments = name.split("\\."); //$NON-NLS-1$ // try to load the class from the bin folder of the project. File classFile = getFile(outFolder, segments, 0); if (classFile == null) { return null; } // load the content of the file and create the class. FileInputStream fis = new FileInputStream(classFile); byte[] data = new byte[(int) classFile.length()]; int read = 0; try { read = fis.read(data); } catch (IOException e) { data = null; } fis.close(); if (data != null) { try { Class<?> clazz = defineClass(null, data, 0, read); if (clazz != null) { return clazz; } } catch (UnsupportedClassVersionError e) { // Attempt to reload on lower version int maxVersion = 50; // JDK 1.6 try { byte[] rewritten = rewriteClass(data, maxVersion, 0); return defineClass(null, rewritten, 0, rewritten.length); } catch (UnsupportedClassVersionError e2) { throw e; // throw *original* exception, not attempt to rewrite } } } } catch (Exception e) { // log the exception? } return null; }
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 ww . j a v 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 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.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/* w w w . j a v a2s. 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 ww w . ja va 2 s. c o m*/ // 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.lint.EclipseLintClient.java
License:Open Source License
@Override @NonNull/*from w w w .j av a 2 s.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.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>//from w w w.j av a 2s.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 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
/** * Attempts to load a class from a project output folder. * @param project the project to load the class from * @param name the name of the class//from w ww. j a va2 s . c om * @return a class object if found, null otherwise. */ private Class<?> loadFromProject(IJavaProject project, String name) { try { // get the project output folder. IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IPath outputLocation = project.getOutputLocation(); IResource outRes = root.findMember(outputLocation); if (outRes == null) { return null; } File outFolder = new File(outRes.getLocation().toOSString()); // get the class name segments String[] segments = name.split("\\."); //$NON-NLS-1$ // try to load the class from the bin folder of the project. File classFile = getFile(outFolder, segments, 0); if (classFile == null) { return null; } // load the content of the file and create the class. FileInputStream fis = new FileInputStream(classFile); byte[] data = new byte[(int) classFile.length()]; int read = 0; try { read = fis.read(data); } catch (IOException e) { data = null; } fis.close(); if (data != null) { Class<?> clazz = defineClass(null, data, 0, read); if (clazz != null) { return clazz; } } } catch (Exception e) { // log the exception? } return null; }