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

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

Introduction

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

Prototype

IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;

Source Link

Document

This is a helper method returning the resolved classpath for the project as a list of simple (non-variable, non-container) classpath entries.

Usage

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Constructor.//from  www. ja va2 s  . c o  m
 * 
 * @param project
 *            is the project
 */
public AcceleoProject(IProject project) {
    if (!registryInitialized) {
        registryInitialized = true;
        registerPackages();
    }
    this.project = project;
    this.sourceFolders = new ArrayList<IPath>();
    final IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            this.sourceFolders.add(entry.getPath());
        }
    }
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * This is a helper method returning the resolved classpath for the project as a list of simple classpath
 * entries./*from www  .  j a v  a2s.  com*/
 * 
 * @return the classpath entries
 */
public List<IPath> getResolvedClasspath() {
    List<IPath> result = new ArrayList<IPath>();
    final IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        AcceleoUIActivator.getDefault().getLog().log(e1.getStatus());
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        result.add(entry.getPath());
    }
    return result;
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Computes the URIs of all the accessible output files (EMTL) in the dependencies of the current project.
 * It browses the resolved classpath of the java project, and keeps each entry of type
 * 'IClasspathEntry.CPE_PROJECT'.//www .j a  v a 2 s .c om
 * 
 * @param outputURIs
 *            is an output parameter with all the URIs
 * @param aProject
 *            is the current project
 */
private void computeAccessibleOutputFilesWithProjectDependencies(List<URI> outputURIs, IProject aProject) {
    final IJavaProject javaProject = JavaCore.create(aProject);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        AcceleoUIActivator.getDefault().getLog().log(e1.getStatus());
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().toString());
            if (requiredProject != null && requiredProject.exists()) {
                computeAccessibleOutputFilesInFolder(outputURIs, getOutputFolder(requiredProject));
            }
        }
    }
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Computes all the accessible projects.
 * /*from w  w w.  j av a  2  s.c o  m*/
 * @param accessibleProjects
 *            is the output list that will contain all the accessible projects
 * @param current
 *            is the current project
 */
private void computeAccessibleProjects(List<IProject> accessibleProjects, IProject current) {
    if (!accessibleProjects.contains(current)) {
        accessibleProjects.add(current);
        IPluginModelBase plugin = PluginRegistry.findModel(current);
        if (plugin != null && plugin.getBundleDescription() != null) {
            BundleDescription[] requiredPlugins = plugin.getBundleDescription().getResolvedRequires();
            for (int i = 0; i < requiredPlugins.length; i++) {
                String requiredSymbolicName = requiredPlugins[i].getSymbolicName();
                IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(requiredSymbolicName);
                if (requiredProject != null && requiredProject.isAccessible()) {
                    computeAccessibleProjects(accessibleProjects, requiredProject);
                }
            }
        }
        final IJavaProject javaProject = JavaCore.create(current);
        IClasspathEntry[] entries;
        try {
            entries = javaProject.getResolvedClasspath(true);
        } catch (JavaModelException e1) {
            AcceleoUIActivator.getDefault().getLog().log(e1.getStatus());
            entries = new IClasspathEntry[] {};
        }
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entry.getPath().toString());
                if (requiredProject != null && requiredProject.exists()) {
                    computeAccessibleProjects(accessibleProjects, requiredProject);
                }
            }
        }
    }
}

From source file:org.eclipse.acceleo.internal.compatibility.parser.mt.ast.core.ProjectParser.java

License:Open Source License

/**
 * Creates the templates for the existing '.mt' files.
 * //from w w w .j  ava 2 s.  c om
 * @param projects
 *            the projects to browse
 * @param root
 *            the root element of the model to create
 * @return a map to attach an '.mt' file and its model representation
 * @throws CoreException
 *             if an issue occurs, it contains a status object describing the cause of the exception
 */
private static Map<IFile, Template> createTemplates(IProject[] projects, ResourceSet root)
        throws CoreException {
    Map<IFile, Template> files = new HashMap<IFile, Template>();
    Set<IFolder> done = new CompactHashSet<IFolder>();
    for (int i = 0; i < projects.length; i++) {
        IProject project = projects[i];
        final IJavaProject javaProject = JavaCore.create(project);
        final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int j = 0; j < entries.length; j++) {
            final IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 1) {
                final IFolder inputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
                if (!done.contains(inputFolder) && inputFolder.exists()) {
                    done.add(inputFolder);
                    createTemplates(files, inputFolder.getFullPath(), inputFolder, root);
                }
            }
        }
    }
    return files;
}

From source file:org.eclipse.ajdt.core.tests.AJDTCoreTestCase.java

License:Open Source License

private IPackageFragmentRoot createDefaultSourceFolder(IJavaProject javaProject) throws CoreException {
    IProject project = javaProject.getProject();
    IFolder folder = project.getFolder("src");
    if (!folder.exists())
        ensureExists(folder);//from   ww  w.j a v  a2  s.co  m

    // if already exists, do nothing
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
    final IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(folder.getFullPath())) {
            return root;
        }
    }

    // else, remove old source folders and add this new one
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    List<IClasspathEntry> oldEntriesList = new ArrayList<IClasspathEntry>();
    oldEntriesList.add(JavaCore.newSourceEntry(root.getPath()));
    for (IClasspathEntry entry : oldEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            oldEntriesList.add(entry);
        }
    }

    IClasspathEntry[] newEntries = oldEntriesList.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newEntries, null);
    return root;
}

From source file:org.eclipse.ajdt.internal.ui.ajde.UIMessageHandler.java

License:Open Source License

private IResource tryToFindResource(String fileName, IProject project) {
    IResource ret = null;/*from w  w  w .  j  a v a  2s.c om*/
    String toFind = fileName.replace('\\', '/');
    IJavaProject jProject = JavaCore.create(project);
    try {
        IClasspathEntry[] classpathEntries = jProject.getResolvedClasspath(false);
        for (int i = 0; i < classpathEntries.length; i++) {
            IClasspathEntry cpEntry = classpathEntries[i];
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath sourcePath = cpEntry.getPath();
                // remove the first segment because the findMember call
                // following always adds it back in under the covers (doh!) 
                // and we end up with two first segments otherwise!
                sourcePath = sourcePath.removeFirstSegments(1);

                IResource memberResource = project.findMember(sourcePath);
                if (memberResource != null) {
                    IResource[] srcContainer = new IResource[] { memberResource };
                    ret = findFile(srcContainer, toFind);
                }
            } else if (cpEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IPath projPath = cpEntry.getPath();
                IResource projResource = AspectJPlugin.getWorkspace().getRoot().findMember(projPath);
                if (projResource != null) {
                    ret = findFile(new IResource[] { projResource }, toFind);
                }
            }
            if (ret != null) {
                break;
            }
        }
    } catch (JavaModelException jmEx) {
        AJDTErrorHandler.handleAJDTError(UIMessages.jmCoreException, jmEx);
    }

    if (ret == null)
        ret = project;
    return ret;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.PathBlock.java

License:Open Source License

/**
 * Checks for duplicate entries on the inpath compared to the Java build
 * path//from  w w w  . j  a va 2 s .  com
 * 
 * This checks to make sure that duplicate entries are not being referred
 * to. For example, it is possible for the JUnit jar to be referred to
 * through a classpath variable as well as a classpath container. This
 * method checks for such duplicates
 * 
 * 1. if an inpath entry is on the build path, then remove it from checking
 * 2. resolve the remaining inpath entries 3. resolve the build path 4.
 * there should be no overlap
 */
private IJavaModelStatus checkForDuplicates(IJavaProject currJProject, IClasspathEntry[] entries) {
    try {
        Map<String, IClasspathEntry> allEntries = new HashMap<String, IClasspathEntry>(entries.length, 1.0f);
        for (int i = 0; i < entries.length; i++) {
            // ignore entries that are inside of a container
            if (getClasspathContainer(entries[i]) == null) {
                allEntries.put(entries[i].getPath().toPortableString(), entries[i]);
            }
        }

        IClasspathEntry[] rawProjectClasspath = currJProject.getRawClasspath();
        for (int i = 0; i < rawProjectClasspath.length; i++) {
            allEntries.remove(rawProjectClasspath[i].getPath().toPortableString());
        }

        IClasspathEntry[] resolvedProjectClasspath = currJProject.getResolvedClasspath(true);
        Map<String, IClasspathEntry> resolvedEntries = new HashMap<String, IClasspathEntry>();
        Iterator<IClasspathEntry> allEntriesIter = allEntries.values().iterator();
        while (allEntriesIter.hasNext()) {
            ClasspathEntry rawEntry = (ClasspathEntry) allEntriesIter.next();
            switch (rawEntry.entryKind) {
            case IClasspathEntry.CPE_SOURCE:
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_VARIABLE:
                IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry);
                resolvedEntries.put(resolvedEntry.getPath().toPortableString(), resolvedEntry);
                break;
            case IClasspathEntry.CPE_CONTAINER:
                List containerEntries = AspectJCorePreferences.resolveClasspathContainer(rawEntry,
                        currJProject.getProject());

                for (Iterator containerIter = containerEntries.iterator(); containerIter.hasNext();) {
                    IClasspathEntry containerEntry = (IClasspathEntry) containerIter.next();
                    resolvedEntries.put(containerEntry.getPath().toPortableString(), containerEntry);
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                IProject thisProject = currJProject.getProject();
                IProject requiredProj = thisProject.getWorkspace().getRoot()
                        .getProject(rawEntry.getPath().makeRelative().toPortableString());
                if (!requiredProj.getName().equals(thisProject.getName()) && requiredProj.exists()) {
                    List containerEntries2 = AspectJCorePreferences.resolveDependentProjectClasspath(rawEntry,
                            requiredProj);
                    for (Iterator containerIter = containerEntries2.iterator(); containerIter.hasNext();) {
                        IClasspathEntry containerEntry = (IClasspathEntry) containerIter.next();
                        resolvedEntries.put(containerEntry.getPath().toPortableString(), containerEntry);
                    }

                }
                break;
            }
        }

        for (int i = 0; i < resolvedProjectClasspath.length; i++) {
            if (resolvedEntries.containsKey(resolvedProjectClasspath[i].getPath().toPortableString())) {
                // duplicate found.
                return new JavaModelStatus(IStatus.WARNING, IStatus.WARNING, currJProject,
                        currJProject.getPath(),
                        UIMessages.InPathBlock_DuplicateBuildEntry + resolvedProjectClasspath[i].getPath());
            }
        }

        return JavaModelStatus.VERIFIED_OK;
    } catch (JavaModelException e) {
        return new JavaModelStatus(e);
    }

}

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;//from  w  ww .ja v a 2s. co 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.buckminster.jdt.internal.ClasspathEmitter.java

License:Open Source License

/**
 * This method obtains the raw classpath from the javaProject and scans it
 * for BMClasspathContainer. The first one found is either kept if it
 * corresponds to the target or replaced if not. All other
 * BMClasspathContainers are removed. If no BMClasspathContainer was found,
 * a new one that represents the target is added first in the list. All
 * IClasspathEntry instances are then resolved.
 * /*from   w w w  . ja va2s. c o m*/
 * @param javaProject
 * @param target
 * @return
 * @throws JavaModelException
 */
private static IClasspathEntry[] changeClasspathForTarget(IJavaProject javaProject, String target)
        throws CoreException {
    boolean entriesChanged = false;
    boolean haveOtherBMCPs = false;
    boolean targetContainerInstalled = false;

    Logger log = Buckminster.getLogger();
    log.debug("Changing classpath for project %s into %s", javaProject.getProject().getName(), target); //$NON-NLS-1$
    IPath desiredContainer = BMClasspathContainer.PATH.append(target);
    IClasspathEntry[] rawEntries = javaProject.readRawClasspath();
    int top = rawEntries.length;
    for (int idx = 0; idx < top; ++idx) {
        IClasspathEntry rawEntry = rawEntries[idx];
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            IPath entryPath = rawEntry.getPath();
            if (BMClasspathContainer.PATH.isPrefixOf(entryPath)) {
                if (!targetContainerInstalled) {
                    if (!desiredContainer.equals(entryPath)) {
                        // This is not the desired container. Replace it.
                        //
                        rawEntries[idx] = JavaCore.newContainerEntry(desiredContainer);
                        entriesChanged = true;
                    }
                    targetContainerInstalled = true;
                } else
                    haveOtherBMCPs = true;
            }
        }
    }

    if (targetContainerInstalled) {
        if (haveOtherBMCPs) {
            // Remove other Buckminster containers
            //
            ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(top);
            for (int idx = 0; idx < top; ++idx) {
                IClasspathEntry rawEntry = rawEntries[idx];
                if (rawEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                        || rawEntry.getPath().equals(desiredContainer))
                    newEntries.add(rawEntry);
            }
            rawEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            entriesChanged = true;
        }
    } else {
        rawEntries = ArrayUtils.appendFirst(rawEntries,
                new IClasspathEntry[] { JavaCore.newContainerEntry(desiredContainer) });
        entriesChanged = true;
    }

    log.debug(entriesChanged ? " changes detected" : " no changes detected"); //$NON-NLS-1$ //$NON-NLS-2$

    return entriesChanged ? getResolvedClasspath(javaProject, rawEntries)
            : javaProject.getResolvedClasspath(false);
}