List of usage examples for org.eclipse.jdt.core IJavaProject getOutputLocation
IPath getOutputLocation() throws JavaModelException;
From source file:org.eclipse.ajdt.ui.tests.builder.ProjectDependenciesUtils.java
License:Open Source License
public static boolean projectHasClassFolderDependency(IProject project, IProject projectDependedOn) throws JavaModelException { IJavaProject javaProject = JavaCore.create(project); IJavaProject depProject = JavaCore.create(projectDependedOn); if (javaProject == null || depProject == null) return false; // first get the output location for projectDependedOn IPath outputLocation = null;//from www .j a v a 2 s . c om IClasspathEntry[] cp = depProject.getRawClasspath(); for (int j = 0; j < cp.length; j++) { IClasspathEntry entry = cp[j]; int contentKind = entry.getContentKind(); if (contentKind == ClasspathEntry.K_OUTPUT) { outputLocation = entry.getOutputLocation(); } if (entry.getEntryKind() == ClasspathEntry.K_OUTPUT) { outputLocation = entry.getOutputLocation(); } } if (outputLocation == null) { outputLocation = depProject.getOutputLocation(); } // now check the classpath entries of project for the output // location just calculated IClasspathEntry[] cpEntry = javaProject.getRawClasspath(); for (int j = 0; j < cpEntry.length; j++) { IClasspathEntry entry = cpEntry[j]; int entryKind = entry.getEntryKind(); IPath entryPath = entry.getPath(); if (entryKind == IClasspathEntry.CPE_LIBRARY && entryPath.equals(outputLocation)) { return true; } } return false; }
From source file:org.eclipse.ajdt.ui.tests.wizards.AspectJProjectWizardTest.java
License:Open Source License
/** * Tests the projects which are created by the perform finish method *//*from w ww . j a va 2s . com*/ public void testProjectWizardPerformFinish() throws Exception { IProject testSrcProject = createPredefinedProject("SourceProject1"); //$NON-NLS-1$ IJavaProject javaProject = JavaCore.create(testSrcProject); int ID = 1; String pDestinationName = "NotVisible" + ID; //$NON-NLS-1$ String pSrcName = testSrcProject.getName(); ID++; testDestinationFile = AspectJPlugin.getWorkspace().getRoot().getLocation().append(pDestinationName) .toFile(); testSrcFile = AspectJPlugin.getWorkspace().getRoot().getLocation().append(pSrcName).toFile(); copyFileStructure(testSrcFile, testDestinationFile); IProject wizardCreatedProject = makeNewWizardProject("TestWizardProject"); //$NON-NLS-1$ runGeneralTests(wizardCreatedProject, "TestWizardProject"); //$NON-NLS-1$ IJavaProject jp = JavaCore.create(wizardCreatedProject); try { //Eclipse 3.3: the default is now to create projects with a bin folder assertEquals("The wizard created project does not have the correct output folder", //$NON-NLS-1$ jp.getPath().append("bin"), jp.getOutputLocation()); //$NON-NLS-1$ } catch (JavaModelException e) { fail("Failed attempting to find the output location of the project"); //$NON-NLS-1$ } IProject newlyFoundProject = makeNewWizardProject(pDestinationName); // The wizard should make the project from the one runGeneralTests(newlyFoundProject, pDestinationName); // existing in the file structure IJavaProject discoveredAJProject = JavaCore.create(newlyFoundProject); try { assertTrue("The wizard discovered project does not have the correct output folder", //$NON-NLS-1$ javaProject.getOutputLocation().lastSegment() .equals(discoveredAJProject.getOutputLocation().lastSegment())); } catch (JavaModelException e) { fail("Failed attempting to find the output location of the project"); //$NON-NLS-1$ } String packagePath = "src" + IPath.SEPARATOR + "TestPackage"; //$NON-NLS-1$ //$NON-NLS-2$ IResource projectPackage = newlyFoundProject.findMember(packagePath); assertTrue("The TestPackage of the discovered project has not been identified correctly", //$NON-NLS-1$ projectPackage != null); String helloPath = packagePath + IPath.SEPARATOR + "Hello.java"; //$NON-NLS-1$ String aspPath = packagePath + IPath.SEPARATOR + "Asp.aj"; //$NON-NLS-1$ String wrongFile = packagePath + IPath.SEPARATOR + "wrongFile.imc"; //$NON-NLS-1$ assertTrue("The Hello.java file has not been correctly discovered", //$NON-NLS-1$ newlyFoundProject.findMember(helloPath) != null); assertTrue("The Asp.aj file has not been correctly discovered", //$NON-NLS-1$ newlyFoundProject.findMember(aspPath) != null); assertTrue("An incorrect file has been created during the discovery of the project", //$NON-NLS-1$ newlyFoundProject.findMember(wrongFile) == null); }
From source file:org.eclipse.amp.escape.ide.ProjectLoader.java
License:Open Source License
/** * Instantiates a new project loader.// w w w . j a va2 s . c o m * * @param project * the project * * @throws LoaderCreationException * the loader creation exception */ public ProjectLoader(IProject project) throws LoaderCreationException { super(new URL[0]); this.project = project; try { project.open(null); IJavaProject javaProject = JavaCore.create(project); addURL(new File( project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/") .toURI().toURL()); bundles = readDependencies(openStream("META-INF/MANIFEST.MF")); mainLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(this); } catch (Exception e) { throw new LoaderCreationException("Problem creating classloaders.", e); } }
From source file:org.eclipse.andmore.internal.build.BuildHelper.java
License:Open Source License
/** * Computes all the project output and dependencies that must go into building the apk. * * @param resMarker//ww w . j a v a 2s .c om * @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 they're in the DEPEDENCIES 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(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES))) { handleCPE(e, javaProject, wsRoot, resMarker); } } } }
From source file:org.eclipse.andmore.internal.build.BuildHelper.java
License:Open Source License
private void handleCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot, ResourceMarker resMarker) {//from w w w . jav a 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(AndmoreAndroidConstants.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 != null && 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. AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath()); } } }
From source file:org.eclipse.andmore.internal.lint.EclipseLintClient.java
License:Open Source License
@Override @NonNull/* ww w . j ava 2 s . com*/ 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) { AndmoreAndroidPlugin.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, null); mProjectInfo.put(project, info); } return info; }
From source file:org.eclipse.andmore.internal.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>//from ww w . j av a 2 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 (AndmoreAndroidConstants.CONTAINER_FRAMEWORK.equals(path)) { foundFrameworkContainer = true; } else if (AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) { foundLibrariesContainer = entry; } else if (AndmoreAndroidConstants.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) { AndmoreAndroidPlugin.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(AndmoreAndroidConstants.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(AndmoreAndroidConstants.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(AndmoreAndroidConstants.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(AndmoreAndroidConstants.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(AndmoreAndroidConstants.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:org.eclipse.buckminster.jdt.internal.ClasspathEmitter.java
License:Open Source License
/** * Returns the default output folder relative to the project. * /*from ww w.j a v a2s. c om*/ * @param project * @return The folder or <code>null</code> if not applicable. * @throws CoreException */ public static IPath getDefaultOutputFolder(IProject project) throws CoreException { String projectName = project.getName(); IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); IJavaProject javaProject = model.getJavaProject(projectName); if (javaProject == null || !javaProject.exists()) return null; return javaProject.getOutputLocation().removeFirstSegments(1); }
From source file:org.eclipse.buckminster.jdt.internal.ClasspathEmitter.java
License:Open Source License
private static void appendPaths(IJavaModel model, IProject project, String target, List<IPath> path, HashSet<IPath> seenPaths, HashSet<String> seenProjects, boolean atTop) throws CoreException { Logger log = Buckminster.getLogger(); String projectName = project.getName(); if (seenProjects.contains(projectName)) return;// w ww. j a v a 2s. c o m seenProjects.add(projectName); log.debug("Emitting classpath for project %s...", projectName); //$NON-NLS-1$ IJavaProject javaProject = model.getJavaProject(projectName); IClasspathEntry[] entries; if (javaProject == null || !javaProject.exists()) { // The project may still be a component that exports jar files. // BMClasspathContainer container = new BMClasspathContainer(project, target); entries = container.getClasspathEntries(); log.debug(" not a java project, contains %d entries", Integer.valueOf(entries.length)); //$NON-NLS-1$ } else { entries = (atTop && target != null) ? changeClasspathForTarget(javaProject, target) : javaProject.getResolvedClasspath(false); log.debug(" java project, contains %d entries", Integer.valueOf(entries.length)); //$NON-NLS-1$ } for (IClasspathEntry entry : entries) { IPath entryPath; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: log.debug(" found library with path: %s", entry.getPath()); //$NON-NLS-1$ if (!(atTop || entry.isExported())) { log.debug(" skipping path %s. It's neither at top nor exported", entry.getPath()); //$NON-NLS-1$ continue; } entryPath = entry.getPath(); break; case IClasspathEntry.CPE_SOURCE: entryPath = entry.getOutputLocation(); if (entryPath == null) { // Uses default output location // IJavaProject proj = model.getJavaProject(entry.getPath().segment(0)); if (proj == null) continue; entryPath = proj.getOutputLocation(); } log.debug(" found source with path: %s", entryPath); //$NON-NLS-1$ break; case IClasspathEntry.CPE_PROJECT: projectName = entry.getPath().segment(0); log.debug(" found project: %s", projectName); //$NON-NLS-1$ if (!(atTop || entry.isExported())) { log.debug(" skipping project %s. It's neither at top nor exported", projectName); //$NON-NLS-1$ continue; } IProject conProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); appendPaths(model, conProject, null, path, seenPaths, seenProjects, false); continue; default: throw BuckminsterException.fromMessage(Messages.unexpected_classpath_entry_kind); } IResource folder = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath); if (folder != null) { log.debug(" path %s is inside workspace, switching to %s", entryPath, folder.getLocation()); //$NON-NLS-1$ entryPath = folder.getLocation(); } if (!seenPaths.contains(entryPath)) { seenPaths.add(entryPath); path.add(entryPath); log.debug(" path %s added", entryPath); //$NON-NLS-1$ } } }
From source file:org.eclipse.buildship.core.launch.internal.GradleClasspathProvider.java
License:Open Source License
private static IRuntimeClasspathEntry[] resolveOutputLocations(IRuntimeClasspathEntry projectEntry, IJavaProject project, LaunchConfigurationScope configurationScopes) throws CoreException { List<IPath> outputLocations = Lists.newArrayList(); boolean hasSourceFolderWithoutCustomOutput = false; if (project.exists() && project.getProject().isOpen()) { for (IClasspathEntry entry : project.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { // only add the output location if it's in the same source set if (configurationScopes.isEntryIncluded(entry)) { IPath path = entry.getOutputLocation(); if (path != null) { outputLocations.add(path); } else { // only use the default output if there's at least one source folder that doesn't have a custom output location hasSourceFolderWithoutCustomOutput = true; }//from w ww . j a va2 s. co m } } } } if (outputLocations.isEmpty()) { return new IRuntimeClasspathEntry[] { projectEntry }; } IPath defaultOutputLocation = project.getOutputLocation(); if (!outputLocations.contains(defaultOutputLocation) && hasSourceFolderWithoutCustomOutput) { outputLocations.add(defaultOutputLocation); } IRuntimeClasspathEntry[] result = new IRuntimeClasspathEntry[outputLocations.size()]; for (int i = 0; i < result.length; i++) { result[i] = new RuntimeClasspathEntry(JavaCore.newLibraryEntry(outputLocations.get(i), null, null)); result[i].setClasspathProperty(projectEntry.getClasspathProperty()); } return result; }