Example usage for org.eclipse.jdt.core IJavaProject getRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getRawClasspath.

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:copied.org.eclipse.pde.internal.launching.sourcelookup.PDESourceLookupParticipant.java

License:Open Source License

private void addProjectSourceContainers(IProject project, ArrayList<IRuntimeClasspathEntry> result)
        throws CoreException {
    if (project == null || !project.hasNature(JavaCore.NATURE_ID))
        return;//w  w  w. j av  a  2 s  .  c  o  m

    IJavaProject jProject = JavaCore.create(project);
    result.add(JavaRuntime.newProjectRuntimeClasspathEntry(jProject));

    IClasspathEntry[] entries = jProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IRuntimeClasspathEntry rte = convertClasspathEntry(entry);
            if (rte != null)
                result.add(rte);
        }
    }

    // Add additional entries from contributed classpath container resolvers
    IBundleClasspathResolver[] resolvers = PDECore.getDefault().getClasspathContainerResolverManager()
            .getBundleClasspathResolvers(project);
    for (int i = 0; i < resolvers.length; i++) {
        result.addAll(resolvers[i].getAdditionalSourceEntries(jProject));
    }
}

From source file:de.akra.idocit.java.utils.JavaTestUtils.java

License:Apache License

/**
 * Creates and initializes a IJavaProject within the currently running test workspace.
 * /* w  w w .j a v a 2s .  c o m*/
 * @destination The current test workspace.
 * @param projectName
 *            [PRIMARY_KEY]
 * @param filesToAdd
 *            [ATTRIBUTE] files to add to the test workspace
 * @return [OBJECT] the created project.
 * @throws CoreException
 * @throws FileNotFoundException
 * @thematicgrid Putting Operations
 */
public static IProject initProjectInWorkspace(final String projectName, final Collection<File> filesToAdd)
        throws CoreException, FileNotFoundException {
    /*
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    final IProgressMonitor progressMonitor = new NullProgressMonitor();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IProject project = root.getProject(projectName);
    project.create(progressMonitor);
    project.open(progressMonitor);

    final IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    final IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    final IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    final IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java files
    for (final File file : filesToAdd) {
        final IFile customerWorkspaceFile = packageFolder.getFile(file.getName());
        customerWorkspaceFile.create(new FileInputStream(file), true, progressMonitor);
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
    return project;
}

From source file:de.akra.idocit.ui.components.DocumentationEditorTest.java

License:Apache License

@Before
public void setupWorkspace() throws CoreException, IOException {
    /*/*  w w  w. j  a v  a  2  s .  c  o  m*/
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(PROJECT_NAME);
    project.create(progressMonitor);
    project.open(progressMonitor);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java file
    File customerFile = new File(SOURCE_DIR + "EmptyInterface.java");
    IFile customerWorkspaceFile = packageFolder.getFile("EmptyInterface.java");

    FileInputStream javaStream = null;

    try {
        javaStream = new FileInputStream(customerFile);
        customerWorkspaceFile.create(javaStream, true, progressMonitor);
    } finally {
        if (javaStream != null) {
            javaStream.close();
        }
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
}

From source file:de.devboost.commenttemplate.builder.CommentTemplateCompilationParticipant.java

License:Open Source License

private void createSrcGenFolder(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries;//from w w w.  ja va 2s.  c  o m
    try {
        entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; ++i) {
            IPath path = entries[i].getPath();
            if (path.segmentCount() == 2 && path.segment(1).equals(CommentTemplateCompiler.SRC_GEN_FOLDER)) {
                return;
            }
        }
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        IClasspathEntry entry = JavaCore
                .newSourceEntry(project.getFullPath().append(CommentTemplateCompiler.SRC_GEN_FOLDER));
        newEntries[newEntries.length - 1] = entry;
        javaProject.setRawClasspath(newEntries, null);
    } catch (JavaModelException e) {
        CommentTemplatePlugin.logError("Can't set classpath for project " + project.getName(), e);
    }
}

From source file:de.devboost.emfcustomize.builder.EMFCustomizeBuilder.java

License:Open Source License

private void adjustFactory(IFile iFile, ResourceSet resourceSet) {
    //adjust factory for generation gap pattern
    IJavaProject javaProject = JavaCore.create(iFile.getProject());
    try {//  w w w .j a va 2  s .c o  m
        for (IClasspathEntry cpEntry : javaProject.getRawClasspath()) {
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                //to register newly generated code;
                updateClaspath(new File(
                        iFile.getWorkspace().getRoot().getLocation().toString() + cpEntry.getPath().toString()),
                        "", resourceSet);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement/*from  w w w .  j a va  2s  .  c o  m*/
 * @return absolute path of generated bytecode package for given element
 * @throws JavaModelException
 */
private static String getPackageOutputPath(IJavaElement javaElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (javaElement == null) {
        return dir;
    }

    IJavaProject project = javaElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    // default bytecode location
    IPath path = project.getOutputLocation();

    IResource resource = javaElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null && classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                    path = outputPath;
                    break;
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    if (isPackageRoot(project, resource)) {
        dir = path.toOSString();
    } else {
        String packPath = EclipseUtils.getJavaPackageName(javaElement).replace(Signature.C_DOT,
                PACKAGE_SEPARATOR);
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:de.loskutov.eclipse.jdepend.views.TreeObject.java

License:Open Source License

protected String getPackageOutputPath(IJavaElement jElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (jElement == null) {
        return dir;
    }/*  w  w w .j ava2s  .com*/

    IJavaProject project = jElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    IPath path = project.getOutputLocation();

    IResource resource = jElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();

        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null) {
                    // if this source folder contains specified java resource
                    // then take bytecode location from himself
                    if (classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                        path = outputPath;
                        break;
                    }
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    if (path == null) {
        return dir;
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    boolean packageRoot = false;
    try {
        packageRoot = isPackageRoot(project, resource);
    } catch (JavaModelException e) {
        // seems to be a bug in 3.3
    }
    if (packageRoot) {
        dir = path.toOSString();
    } else {
        String packPath = getPackageName().replace('.', '/');
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:de.lynorics.eclipse.jangaroo.m2e.JangarooProjectConfigurator.java

License:Open Source License

private void declareSourcePath(ProjectConfigurationRequest request, IProgressMonitor monitor)
        throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(request.getProject());
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    List<IClasspathEntry> list = new Vector<IClasspathEntry>();
    list.addAll(Arrays.asList(classpath));
    //      List<IClasspathEntry> list = new Vector<IClasspathEntry>();
    addSourcePath(javaProject, list, "src/main/joo");
    addSourcePath(javaProject, list, "src/test/joo");
    addSourcePath(javaProject, list, "src/main/resources");
    addSourcePath(javaProject, list, "target/generated-sources/joo");
    javaProject.setRawClasspath((IClasspathEntry[]) list.toArray(new IClasspathEntry[list.size()]), monitor);
}

From source file:de.lynorics.eclipse.jangaroo.m2e.JangarooProjectConversionParticipant.java

License:Open Source License

private void configureBuildSourceDirectories(Model model, IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    Set<String> sources = new LinkedHashSet<String>();
    Set<String> potentialTestSources = new LinkedHashSet<String>();
    Set<String> potentialResourceDirectories = new LinkedHashSet<String>();
    Set<String> potentialTestResourceDirectories = new LinkedHashSet<String>();
    IPath projectPath = javaProject.getPath();

    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entries[i].getPath().makeRelativeTo(projectPath);
            if (path.isAbsolute()) {
                //We only support paths relative to the project root, so we skip this one
                continue;
            }//from w ww .  j a va2s .com
            String portablePath = path.toPortableString();
            boolean isPotentialTestSource = isPotentialTestSource(path);
            boolean isResource = false;
            if (isPotentialTestSource) {
                //          if(DEFAULT_TEST_RESOURCES.equals(portablePath)) {
                //            isResource = potentialTestResourceDirectories.add(portablePath);
                //          } else {
                //            potentialTestSources.add(portablePath);
                //          }
            } else {
                if (DEFAULT_RESOURCES.equals(portablePath)) {
                    isResource = potentialResourceDirectories.add(portablePath);
                } else {
                    sources.add(portablePath);
                }
            }

            if (!isResource) {
                //For source folders not already flagged as resource folder, check if 
                // they contain non-java sources, so we can add them as resources too
                boolean hasNonJangarooResources = false;
                IFolder folder = javaProject.getProject().getFolder(path);
                if (folder.isAccessible()) {
                    NonJangarooResourceVisitor nonJangarooResourceVisitor = new NonJangarooResourceVisitor();
                    try {
                        folder.accept(nonJangarooResourceVisitor);
                    } catch (NonJangarooResourceFoundException ex) {
                        //Expected
                        hasNonJangarooResources = true;
                    } catch (CoreException ex) {
                        //385666 ResourceException is thrown in Helios
                        if (ex.getCause() instanceof NonJangarooResourceFoundException) {
                            hasNonJangarooResources = true;
                        } else {
                            //                log.error("An error occured while analysing {} : {}", folder, ex.getMessage());
                        }
                    }
                }

                if (hasNonJangarooResources) {
                    if (isPotentialTestSource) {
                        potentialTestResourceDirectories.add(portablePath);
                    } else {
                        potentialResourceDirectories.add(portablePath);
                    }
                }
            }
        }
    }

    Build build = getOrCreateBuild(model);

    if (!sources.isEmpty()) {
        if (sources.size() > 1) {
            //We don't know how to handle multiple sources, i.e. how to map to a resource or test source directory
            //That should be dealt by setting the build-helper-plugin config (http://mojo.codehaus.org/build-helper-maven-plugin/usage.html)
            //        log.warn("{} has multiple source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        String sourceDirectory = sources.iterator().next();
        if (!DEFAULT_JANGAROO_SOURCE.equals(sourceDirectory)) {
            build.setSourceDirectory(sourceDirectory);
        }

        for (String resourceDirectory : potentialResourceDirectories) {
            if (!DEFAULT_RESOURCES.equals(resourceDirectory) || potentialResourceDirectories.size() > 1) {
                build.addResource(createResource(resourceDirectory));
            }
        }
    }

    if (!potentialTestSources.isEmpty()) {
        if (potentialTestSources.size() > 1) {
            //        log.warn("{} has multiple test source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        //      String testSourceDirectory = potentialTestSources.iterator().next();
        //      if(!DEFAULT_JAVA_TEST_SOURCE.equals(testSourceDirectory)) {
        //        build.setTestSourceDirectory(testSourceDirectory);
        //      }
        //      for(String resourceDirectory : potentialTestResourceDirectories) {
        //        if(!DEFAULT_TEST_RESOURCES.equals(resourceDirectory) || potentialTestResourceDirectories.size() > 1) {
        //          build.addTestResource(createResource(resourceDirectory));
        //        }
        //      }
    }

    //Ensure we don't attach a new empty build definition to the model
    if (build.getSourceDirectory() != null || build.getTestSourceDirectory() != null
            || !build.getResources().isEmpty() || !build.getTestResources().isEmpty()) {
        model.setBuild(build);
    }
}

From source file:de.ovgu.featureide.core.framework.FrameworkComposer.java

License:Open Source License

/**
 * Update .classpath file/*from   ww w.  j a  v  a 2 s  .  com*/
 * 
 * @param project
 */
private void setBuildpaths(IProject project) {

    try {
        final IJavaProject javaProject = JavaCore.create(project);
        final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        /** copy existing non-feature entries **/
        for (int i = 0; i < oldEntries.length; i++) {
            if (oldEntries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                final IPath path = oldEntries[i].getPath();
                if (!isFeatureLib(path)) {
                    entries.add(oldEntries[i]);
                }
            } else {
                entries.add(oldEntries[i]);
            }
        }

        /** add selected features **/
        try {
            for (final IResource res : getJarFolder().members()) {
                final String featureName = res.getName();
                if (selectedFeatures.contains(featureName)) {
                    final List<IPath> newEntries = createNewIPath(res);
                    for (final IPath entry : newEntries) {
                        final IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(entry, null, null);
                        entries.add(newLibraryEntry);
                    }
                }
            }
        } catch (final CoreException e) {
            FrameworkCorePlugin.getDefault().logError(e);
        }

        final IClasspathEntry[] result = entries.toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(result, null);
    } catch (final JavaModelException e) {
        FrameworkCorePlugin.getDefault().logError(e);
    }
}