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.jst.jsf.core.jsfappconfig.JSFAppConfigUtils.java

License:Open Source License

/**
 * Gets list of JAR file names, where each file name represents a JAR on
 * the classpath that contains a /META-INF/faces-config.xml entry. Will
 * return an empty list if no such JAR files are located.
 * //from  w  w  w  . ja v  a2  s  .c o m
 * @param project IProject instance for which to scan the classpath.
 * @return List of JAR file names, where each file name represents a JAR
 * on the classpath that contains a ...META-INF/faces-config.xml entry;
 * list may be empty.
 * @throws CoreException Thrown when underlying calls into JavaCore fail.
 * @throws IOException Thrown when attempt to open JAR to determine if it
 * contains a /META-INF/faces-config.xml entry fails.
 */
public static List getConfigFileJARsFromClasspath(IProject project) throws CoreException, IOException {
    ArrayList JARsList = new ArrayList();
    if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            if (classpathEntries != null && classpathEntries.length > 0) {
                IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                for (int i = 0; i < classpathEntries.length; i++) {
                    IClasspathEntry classpathEntry = classpathEntries[i];
                    if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath libraryPath = classpathEntry.getPath();
                        if (libraryPath.getFileExtension() != null
                                && libraryPath.getFileExtension().length() > 0) {
                            if (libraryPath.getDevice() == null
                                    && workspaceRoot.getProject(libraryPath.segment(0)).exists()) {
                                libraryPath = workspaceRoot.getFile(libraryPath).getLocation();
                            }
                            String libraryPathString = libraryPath.toString();
                            JarFile jarFile = null;
                            try {
                                //check existence first [222249]
                                File file = new File(libraryPathString);
                                if (file.exists()) {
                                    jarFile = new JarFile(file, false);
                                    if (jarFile != null) {
                                        JarEntry jarEntry = jarFile.getJarEntry(FACES_CONFIG_IN_JAR_PATH);
                                        if (jarEntry != null) {
                                            JARsList.add(libraryPathString);
                                        }
                                    }
                                }
                            } catch (FileNotFoundException fnfex) {
                                //should not get here, but eat error since this could only occur in under strange circumstances [222249]
                            } catch (IOException ioe) {
                                JSFCorePlugin.log(IStatus.ERROR,
                                        NLS.bind(Messages.JSFAppConfigUtils_ErrorOpeningJarFile,
                                                libraryPathString),
                                        ioe);
                            } finally {
                                if (jarFile != null) {
                                    jarFile.close();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return JARsList;
}

From source file:org.eclipse.jst.jsp.core.taglib.ProjectDescription.java

License:Open Source License

private void ensureUpTodate() {
    IClasspathEntry[] entries = null;//from w ww . j  a  v a 2 s .  c o m
    try {
        /*
         * If the Java nature isn't present (or something else is
         * wrong), don't check the build path.
         */
        IJavaProject jproject = JavaCore.create(fProject);
        if (jproject != null && jproject.exists()) {
            entries = jproject.getResolvedClasspath(true);
        }
    } catch (JavaModelException e) {
        Logger.logException(e);
    }
    if (entries != null) {
        try {
            LOCK.acquire();
            /*
             * Double-check that the number of build path entries has not
             * changed. This should cover most cases such as when a
             * library is added into or removed from a container.
             */
            fBuildPathIsDirty = fBuildPathIsDirty || (fBuildPathEntryCount != entries.length);

            if (fBuildPathIsDirty) {
                indexClasspath(entries);
                fBuildPathIsDirty = false;
            } else {
                PackageFragmentRootDelta[] removes = (PackageFragmentRootDelta[]) fPackageFragmentRootsRemoved
                        .values().toArray(new PackageFragmentRootDelta[fPackageFragmentRootsRemoved.size()]);
                for (int i = 0; i < removes.length; i++) {
                    handleElementChanged(removes[i].elementPath, removes[i].deltaKind, removes[i].isExported);
                }
                PackageFragmentRootDelta[] changes = (PackageFragmentRootDelta[]) fPackageFragmentRootsChanged
                        .values().toArray(new PackageFragmentRootDelta[fPackageFragmentRootsChanged.size()]);
                for (int i = 0; i < changes.length; i++) {
                    handleElementChanged(changes[i].elementPath, changes[i].deltaKind, changes[i].isExported);
                }
                PackageFragmentRootDelta[] adds = (PackageFragmentRootDelta[]) fPackageFragmentRootsAdded
                        .values().toArray(new PackageFragmentRootDelta[fPackageFragmentRootsAdded.size()]);
                for (int i = 0; i < adds.length; i++) {
                    handleElementChanged(adds[i].elementPath, adds[i].deltaKind, adds[i].isExported);
                }
                fPackageFragmentRootsRemoved.clear();
                fPackageFragmentRootsChanged.clear();
                fPackageFragmentRootsAdded.clear();
            }
        } finally {
            LOCK.release();
        }
    }
}

From source file:org.eclipse.jst.pagedesigner.jsf.ui.converter.operations.jsf.LoadBundleOperation.java

License:Open Source License

/**
 * Find specified file in any source folder of the specified project.
 * /* www  .j a v  a 2s. co  m*/
 * @param project IProject instance.
 * @param filePath Source folder-relative path of the file to be located.
 * @return the specified file in any source folder of the specified project.
 */
protected IResource findFileInSrcFolder(IProject project, String filePath) {
    IResource resource = null;
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
        try {
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < classpathEntries.length; i++) {
                IClasspathEntry classpathEntry = classpathEntries[i];
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath srcPath = classpathEntry.getPath();
                    //srcPath = srcPath.removeFirstSegments(srcPath.matchingFirstSegments(project.getFullPath()));
                    IPath srcFilePath = srcPath.append(filePath);
                    IResource tmpResource = project.getParent().findMember(srcFilePath);
                    if (tmpResource != null) {
                        resource = tmpResource;
                        break;
                    }
                }
            }
        } catch (JavaModelException jme) {
            //ignore - returning null from method will indicate failure
        }
    }
    return resource;
}

From source file:org.eclipse.jst.server.jetty.core.internal.wst.ModuleTraverser.java

License:Open Source License

private static Map getComponentClasspathDependencies(final IJavaProject javaProject, final boolean isWebApp)
        throws CoreException {

    // get the raw entries
    final Map referencedRawEntries = getRawComponentClasspathDependencies(javaProject);
    final Map validRawEntries = new HashMap();

    // filter out non-valid referenced raw entries
    final Iterator i = referencedRawEntries.keySet().iterator();
    while (i.hasNext()) {
        final IClasspathEntry entry = (IClasspathEntry) i.next();
        final IClasspathAttribute attrib = (IClasspathAttribute) referencedRawEntries.get(entry);
        if (isValid(entry, attrib, isWebApp, javaProject.getProject())) {
            validRawEntries.put(entry, attrib);
        }//from w  ww  .j a  v  a 2s.  co m
    }

    // if we have no valid raw entries, return empty map
    if (validRawEntries.isEmpty()) {
        return Collections.EMPTY_MAP;
    }

    // XXX Would like to replace the code below with use of a public JDT API that returns
    // the raw IClasspathEntry for a given resolved IClasspathEntry (see see https://bugs.eclipse.org/bugs/show_bug.cgi?id=183995)
    // The code must currently leverage IPackageFragmentRoot to determine this
    // mapping and, because IPackageFragmentRoots do not maintain IClasspathEntry data, a prior
    // call is needed to getResolvedClasspath() and the resolved IClasspathEntries have to be stored in a Map from IPath-to-IClasspathEntry to
    // support retrieval using the resolved IPackageFragmentRoot

    // retrieve the resolved classpath
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    final Map pathToResolvedEntry = new HashMap();

    // store in a map from path to entry
    for (int j = 0; j < entries.length; j++) {
        pathToResolvedEntry.put(entries[j].getPath(), entries[j]);
    }

    final Map referencedEntries = new LinkedHashMap();

    // grab all IPackageFragmentRoots
    final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    for (int j = 0; j < roots.length; j++) {
        final IPackageFragmentRoot root = roots[j];
        final IClasspathEntry rawEntry = root.getRawClasspathEntry();

        // is the raw entry valid?
        IClasspathAttribute attrib = (IClasspathAttribute) validRawEntries.get(rawEntry);
        if (attrib == null) {
            continue;
        }

        final IPath pkgFragPath = root.getPath();
        final IClasspathEntry resolvedEntry = (IClasspathEntry) pathToResolvedEntry.get(pkgFragPath);
        final IClasspathAttribute resolvedAttrib = checkForComponentDependencyAttribute(resolvedEntry,
                DEPENDECYATTRIBUTETYPE_DEPENDENCY_OR_NONDEPENDENCY);
        // attribute for the resolved entry must either be unspecified or it must be the
        // dependency attribute for it to be included
        if (resolvedAttrib == null || resolvedAttrib.getName().equals(CLASSPATH_COMPONENT_DEPENDENCY)) {
            // filter out resolved entry if it doesn't pass the validation rules
            if (isValid(resolvedEntry, resolvedAttrib != null ? resolvedAttrib : attrib, isWebApp,
                    javaProject.getProject())) {
                if (resolvedAttrib != null) {
                    // if there is an attribute on the sub-entry, use that
                    attrib = resolvedAttrib;
                }
                referencedEntries.put(resolvedEntry, attrib);
            }
        }
    }

    return referencedEntries;
}

From source file:org.eclipse.jst.ws.internal.axis.consumption.ui.util.ClasspathUtils.java

License:Open Source License

private String[] getClasspathForJavaProject(IJavaProject javaProject) {
    ArrayList projectClasspath = new ArrayList();
    try {//from   www  .  java 2 s . c  o m
        IClasspathEntry[] buildPath = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < buildPath.length; i++) {
            String[] buildPathString = classpathEntry2String(buildPath[i], javaProject.getProject());
            for (int j = 0; j < buildPathString.length; j++) {
                projectClasspath.add(buildPathString[j]);
            }
        }
    } catch (JavaModelException jme) {
    }

    String[] utilityJarsClasspath;
    IProject project = javaProject.getProject();
    IProject[] referencingProjects = project.getReferencingProjects();
    for (int i = 0; i < referencingProjects.length; i++) {
        utilityJarsClasspath = getUtilityJarClasspath(referencingProjects[i]);
        for (int j = 0; j < utilityJarsClasspath.length; j++) {
            projectClasspath.add(utilityJarsClasspath[j]);
        }
    }

    return (String[]) projectClasspath.toArray(new String[projectClasspath.size()]);
}

From source file:org.eclipse.ocl.examples.editor.ui.builder.CommonBuilder.java

License:Open Source License

/**
 * Return the classpath entries applicable to the current project.
 * Returns null if not a Java project.//from   ww  w  .  j  a  va  2 s. com
 */
protected IClasspathEntry[] getClasspathEntries(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null)
        return null;
    try {
        return javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.eclipse.osee.framework.ui.ws.AJavaProject.java

License:Open Source License

private static IClasspathEntry[] localGetResolvedClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] paths = cachedPath.get(javaProject);
    if (paths == null) {
        paths = javaProject.getResolvedClasspath(true);
        cachedPath.put(javaProject, paths);
    }//w  ww.  j a  va  2 s . co  m
    return paths;
}

From source file:org.eclipse.pde.api.tools.internal.builder.ApiAnalysisBuilder.java

License:Open Source License

/**
 * Returns the complete listing of required projects from the classpath of
 * the backing project// w  w  w.  j a  v a2s . c  o m
 * 
 * @param includeBinaryPrerequisites
 * @return the list of projects required
 * @throws CoreException
 */
IProject[] getRequiredProjects(boolean includebinaries) throws CoreException {
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    if (this.currentproject == null || workspaceRoot == null) {
        return new IProject[0];
    }
    ArrayList<IProject> projects = new ArrayList<IProject>();
    try {
        IJavaProject javaProject = JavaCore.create(this.currentproject);
        HashSet<IPath> blocations = new HashSet<IPath>();
        blocations.add(javaProject.getOutputLocation());
        this.output_locs.put(this.currentproject, blocations);
        HashSet<IPath> slocations = new HashSet<IPath>();
        IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].isArchive()) {
                continue;
            }
            slocations.add(roots[i].getPath());
        }
        this.src_locs.put(this.currentproject, slocations);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int i = 0, l = entries.length; i < l; i++) {
            IClasspathEntry entry = entries[i];
            IPath path = entry.getPath();
            IProject p = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_PROJECT: {
                p = workspaceRoot.getProject(path.lastSegment()); // missing
                // projects
                // are
                // considered
                // too
                if (isOptional(entry) && !p.hasNature(ApiPlugin.NATURE_ID)) {// except
                    // if
                    // entry
                    // is
                    // optional
                    p = null;
                }
                break;
            }
            case IClasspathEntry.CPE_LIBRARY: {
                if (includebinaries && path.segmentCount() > 1) {
                    // some binary resources on the class path can come
                    // from projects that are not included in the
                    // project references
                    IResource resource = workspaceRoot.findMember(path.segment(0));
                    if (resource instanceof IProject) {
                        p = (IProject) resource;
                    }
                }
                break;
            }
            case IClasspathEntry.CPE_SOURCE: {
                IPath entrypath = entry.getOutputLocation();
                if (entrypath != null) {
                    blocations.add(entrypath);
                }
                break;
            }
            default: {
                break;
            }
            }
            if (p != null && !projects.contains(p)) {
                projects.add(p);
                // try to derive all of the output locations for each of the
                // projects
                javaProject = JavaCore.create(p);
                HashSet<IPath> bins = new HashSet<IPath>();
                HashSet<IPath> srcs = new HashSet<IPath>();
                if (javaProject.exists()) {
                    bins.add(javaProject.getOutputLocation());
                    IClasspathEntry[] source = javaProject.getRawClasspath();
                    IPath entrypath = null;
                    for (int j = 0; j < source.length; j++) {
                        if (source[j].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            srcs.add(source[j].getPath());
                            entrypath = source[j].getOutputLocation();
                            if (entrypath != null) {
                                bins.add(entrypath);
                            }
                        }
                    }
                    this.output_locs.put(p, bins);
                    this.src_locs.put(p, srcs);
                }
            }
        }
    } catch (JavaModelException e) {
        return new IProject[0];
    }
    IProject[] result = new IProject[projects.size()];
    projects.toArray(result);
    return result;
}

From source file:org.eclipse.pde.api.tools.internal.builder.BuildState.java

License:Open Source License

/**
 * Computes and returns a CRC of the projects resolved build path, or -1 if
 * unknown.//ww w . ja v  a  2 s  .co  m
 * 
 * @param project project
 * @return build path CRC or -1
 */
public static long computeBuildPathCRC(IProject project) {
    IJavaProject jp = JavaCore.create(project);
    try {
        IClasspathEntry[] classpath = jp.getResolvedClasspath(true);
        CRC32 crc32 = new CRC32();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            crc32.update(entry.getPath().toPortableString().getBytes());
        }
        return crc32.getValue();
    } catch (JavaModelException e) {
    }
    return -1L;
}

From source file:org.eclipse.pde.internal.core.builders.BuildErrorReporter.java

License:Open Source License

private void validateSourceFoldersInSrcIncludes(IBuildEntry includes) {
    if (includes == null)
        return;//from   ww w .j  a  va  2  s . c  o  m

    List<IPath> sourceFolderList = new ArrayList<IPath>(0);
    try {
        IJavaProject javaProject = JavaCore.create(fProject);
        if (javaProject.exists()) {
            IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);

            for (int index = 0; index < classPathEntries.length; index++) {
                if (classPathEntries[index].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    sourceFolderList.add(classPathEntries[index].getPath());
                }
            }
        }
    } catch (JavaModelException e) { //do nothing
    }

    List<String> reservedTokens = Arrays.asList(RESERVED_NAMES);

    String[] tokens = includes.getTokens();
    for (int i = 0; i < tokens.length; i++) {
        IResource res = fProject.findMember(tokens[i]);
        if (res == null)
            continue;
        String errorMessage = null;
        if (sourceFolderList.contains(res.getFullPath())) {
            errorMessage = PDECoreMessages.BuildErrorReporter_srcIncludesSourceFolder;
        } else if (tokens[i].startsWith(".") //$NON-NLS-1$
                || reservedTokens.contains(res.getName().toString().toLowerCase())) {
            errorMessage = NLS.bind(PDECoreMessages.BuildErrorReporter_srcIncludesSourceFolder1, res.getName());
        }

        if (errorMessage != null) {
            prepareError(includes.getName(), tokens[i], errorMessage, PDEMarkerFactory.B_REMOVAL,
                    fSrcInclSeverity, PDEMarkerFactory.CAT_OTHER);
        }
    }

}