Example usage for org.eclipse.jdt.core IPackageFragmentRoot getRawClasspathEntry

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getRawClasspathEntry

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getRawClasspathEntry.

Prototype

IClasspathEntry getRawClasspathEntry() throws JavaModelException;

Source Link

Document

Returns the first raw classpath entry that corresponds to this package fragment root.

Usage

From source file:com.liferay.ide.server.remote.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<IClasspathEntry, IClasspathAttribute> validRawEntries = new HashMap<IClasspathEntry, IClasspathAttribute>();

    // 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. com
    }

    // 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<IPath, IClasspathEntry> pathToResolvedEntry = new HashMap<IPath, IClasspathEntry>();

    // 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<IClasspathEntry, IClasspathAttribute> referencedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>();

    // 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 = validRawEntries.get(rawEntry);
        if (attrib == null) {
            continue;
        }

        final IPath pkgFragPath = root.getPath();
        final IClasspathEntry resolvedEntry = 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:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
    IClasspathEntry rawEntry = root.getRawClasspathEntry();
    int rawEntryKind = rawEntry.getEntryKind();
    switch (rawEntryKind) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
    case IClasspathEntry.CPE_CONTAINER: // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
        if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
            IClasspathEntry resolvedEntry = root.getResolvedClasspathEntry();
            if (resolvedEntry.getReferencingEntry() != null)
                return resolvedEntry;
            else//from w  w w .jav  a  2 s .  c o  m
                return rawEntry;
        }
    }
    return rawEntry;
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {/*from w ww. j  a  va2  s .com*/
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}

From source file:com.mountainminds.eclemma.autoMerge.OldFileAnalyzer.java

License:Open Source License

private IResource getClassfilesLocation(IPackageFragmentRoot root) throws CoreException {

    // For binary roots the underlying resource directly points to class files:
    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
        return root.getResource();
    }/*w w  w  .j  a va  2 s .  c o  m*/

    // For source roots we need to find the corresponding output folder:
    IPath path = root.getRawClasspathEntry().getOutputLocation();
    if (path == null) {
        path = root.getJavaProject().getOutputLocation();
    }
    return root.getResource().getWorkspace().getRoot().findMember(path);
}

From source file:com.mountainminds.eclemma.core.ScopeUtils.java

License:Open Source License

/**
 * Remove all JRE runtime entries from the given set
 * /* w  ww  .  j a  va 2s .co  m*/
 * @param scope
 *          set to filter
 * @return filtered set without JRE runtime entries
 */
public static Set<IPackageFragmentRoot> filterJREEntries(Collection<IPackageFragmentRoot> scope)
        throws JavaModelException {
    final Set<IPackageFragmentRoot> filtered = new HashSet<IPackageFragmentRoot>();
    for (final IPackageFragmentRoot root : scope) {
        final IClasspathEntry entry = root.getRawClasspathEntry();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
            filtered.add(root);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                    root.getJavaProject());
            if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                filtered.add(root);
            }
            break;
        }
    }
    return filtered;
}

From source file:com.mountainminds.eclemma.internal.core.instr.ClassFilesStore.java

License:Open Source License

private static IPath getClassFileLocation(IPackageFragmentRoot root) throws JavaModelException {
    IPath path;/*w  w w .j  a  va  2  s  .c o  m*/
    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        IClasspathEntry entry = root.getRawClasspathEntry();
        path = entry.getOutputLocation();
        if (path == null) {
            path = root.getJavaProject().getOutputLocation();
        }
    } else {
        path = root.getPath();
    }
    return path;
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

@Override
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen())
        return NO_CHILDREN;

    List<Object> result = new ArrayList<Object>();

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        IClasspathEntry classpathEntry = root.getRawClasspathEntry();
        int entryKind = classpathEntry.getEntryKind();
        if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            // all ClassPathContainers are added later
        } else if (fShowLibrariesNode
                && (entryKind == IClasspathEntry.CPE_LIBRARY || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            IResource resource = root.getResource();
            if (resource != null && project.getResource().equals(resource.getParent())) {
                // show resource as child of project, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=141906
                result.add(resource);// ww  w  .j a v  a  2s . c o  m
            } else {
                // skip: will add the referenced library node later
            }
        } else {
            if (isProjectPackageFragmentRoot(root)) {
                // filter out package fragments that correspond to projects and
                // replace them with the package fragments directly
                Object[] fragments = getPackageFragmentRootContent(root);
                for (int j = 0; j < fragments.length; j++) {
                    result.add(fragments[j]);
                }
            } else {
                result.add(root);
            }
        }
    }

    if (fShowLibrariesNode) {
        result.add(new LibraryContainer(project));
    }

    // separate loop to make sure all containers are on the classpath (even empty ones)
    IClasspathEntry[] rawClasspath = project.getRawClasspath();
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry classpathEntry = rawClasspath[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            result.add(new ClassPathContainer(project, classpathEntry));
        }
    }
    Object[] resources = project.getNonJavaResources();
    for (int i = 0; i < resources.length; i++) {
        result.add(resources[i]);
    }
    return result.toArray();
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

@Override
protected Object internalGetParent(Object element) {
    if (!fIsFlatLayout && element instanceof IPackageFragment) {
        return getHierarchicalPackageParent((IPackageFragment) element);
    } else if (element instanceof IPackageFragmentRoot) {
        // since we insert logical package containers we have to fix
        // up the parent for package fragment roots so that they refer
        // to the container and containers refer to the project
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;

        try {//from   ww w .  j  av a 2 s.  c  om
            IClasspathEntry entry = root.getRawClasspathEntry();
            int entryKind = entry.getEntryKind();
            if (entryKind == IClasspathEntry.CPE_CONTAINER) {
                return new ClassPathContainer(root.getJavaProject(), entry);
            } else if (fShowLibrariesNode && (entryKind == IClasspathEntry.CPE_LIBRARY
                    || entryKind == IClasspathEntry.CPE_VARIABLE)) {
                return new LibraryContainer(root.getJavaProject());
            }
        } catch (JavaModelException e) {
            // fall through
        }
    } else if (element instanceof PackageFragmentRootContainer) {
        return ((PackageFragmentRootContainer) element).getJavaProject();
    }
    return super.internalGetParent(element);
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModelLoader.java

License:Open Source License

@Override
public synchronized boolean loadPackage(Module module, String packageName, boolean loadDeclarations) {
    packageName = Util.quoteJavaKeywords(packageName);
    if (loadDeclarations && !loadedPackages.add(cacheKeyByModule(module, packageName))) {
        return true;
    }//from ww  w .j  a  v a2  s.c o  m

    if (module instanceof JDTModule) {
        JDTModule jdtModule = (JDTModule) module;
        List<IPackageFragmentRoot> roots = jdtModule.getPackageFragmentRoots();
        IPackageFragment packageFragment = null;
        for (IPackageFragmentRoot root : roots) {
            // skip packages that are not present
            if (!root.exists() || !javaProject.isOnClasspath(root))
                continue;
            try {
                IClasspathEntry entry = root.getRawClasspathEntry();

                //TODO: is the following really necessary?
                //Note that getContentKind() returns an undefined
                //value for a classpath container or variable
                if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                        && entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE
                        && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                        && !CeylonBuilder.isCeylonSourceEntry(entry)) {
                    continue;
                }

                packageFragment = root.getPackageFragment(packageName);
                if (!packageFragment.exists()) {
                    continue;
                }
            } catch (JavaModelException e) {
                if (!e.isDoesNotExist()) {
                    e.printStackTrace();
                }
                continue;
            }
            if (!loadDeclarations) {
                // we found the package
                return true;
            }

            // we have a few virtual types in java.lang that we need to load but they are not listed from class files
            if (module.getNameAsString().equals(JAVA_BASE_MODULE_NAME) && packageName.equals("java.lang")) {
                loadJavaBaseArrays();
            }

            IClassFile[] classFiles = new IClassFile[] {};
            org.eclipse.jdt.core.ICompilationUnit[] compilationUnits = new org.eclipse.jdt.core.ICompilationUnit[] {};
            try {
                classFiles = packageFragment.getClassFiles();
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
            try {
                compilationUnits = packageFragment.getCompilationUnits();
            } catch (JavaModelException e) {
                e.printStackTrace();
            }

            List<IType> typesToLoad = new LinkedList<>();
            for (IClassFile classFile : classFiles) {
                IType type = classFile.getType();
                typesToLoad.add(type);
            }

            for (org.eclipse.jdt.core.ICompilationUnit compilationUnit : compilationUnits) {
                // skip removed CUs
                if (!compilationUnit.exists())
                    continue;
                try {
                    for (IType type : compilationUnit.getTypes()) {
                        typesToLoad.add(type);
                    }
                } catch (JavaModelException e) {
                    e.printStackTrace();
                }
            }

            for (IType type : typesToLoad) {
                String typeFullyQualifiedName = type.getFullyQualifiedName();
                String[] nameParts = typeFullyQualifiedName.split("\\.");
                String typeQualifiedName = nameParts[nameParts.length - 1];
                // only top-levels are added in source declarations
                if (typeQualifiedName.indexOf('$') > 0) {
                    continue;
                }

                if (type.exists()
                        && !sourceDeclarations.containsKey(getToplevelQualifiedName(
                                type.getPackageFragment().getElementName(), typeFullyQualifiedName))
                        && !isTypeHidden(module, typeFullyQualifiedName)) {
                    convertToDeclaration(module, typeFullyQualifiedName, DeclarationType.VALUE);
                }
            }
        }
    }
    return false;
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModule.java

License:Open Source License

public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() {
    if (packageFragmentRoots.isEmpty() && !moduleManager.isExternalModuleLoadedFromSource(getNameAsString())) {
        IJavaProject javaProject = moduleManager.getJavaProject();
        if (javaProject != null) {
            if (this.equals(getLanguageModule())) {
                IClasspathEntry runtimeClasspathEntry = null;

                try {
                    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().segment(0)
                                .equals(CeylonLanguageModuleContainer.CONTAINER_ID)) {
                            runtimeClasspathEntry = entry;
                            break;
                        }//from  w w  w  .  j a va2s. c o m
                    }

                    if (runtimeClasspathEntry != null) {
                        for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                            if (root.exists() && javaProject.isOnClasspath(root)
                                    && root.getRawClasspathEntry().equals(runtimeClasspathEntry)) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                File jarToSearch = null;
                try {
                    jarToSearch = returnCarFile();
                    if (jarToSearch == null) {
                        RepositoryManager repoMgr = CeylonBuilder
                                .getProjectRepositoryManager(javaProject.getProject());
                        if (repoMgr != null) {
                            jarToSearch = CeylonProjectModulesContainer.getModuleArtifact(repoMgr, this);
                        }
                    }

                    if (jarToSearch != null) {
                        IPackageFragmentRoot root = moduleManager.getJavaProject()
                                .getPackageFragmentRoot(jarToSearch.toString());
                        if (root instanceof JarPackageFragmentRoot) {
                            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
                            if (jarRoot.getJar().getName().equals(jarToSearch.getPath())) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (CoreException e) {
                    if (jarToSearch != null) {
                        System.err.println("Exception trying to get Jar file '" + jarToSearch + "' :");
                    }
                    e.printStackTrace();
                }
            }
        }
    }
    return packageFragmentRoots;
}