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:org.python.pydev.editor.codecompletion.revisited.javaintegration.JavaProjectModulesManager.java

License:Open Source License

/**
 * @param dontSearchInit: not applicable for this method (ignored)
 * @return the module that corresponds to the passed name.
 *//*  w ww. ja va2 s.c  o  m*/
public IModule getModuleInDirectManager(String name, IPythonNature nature, boolean dontSearchInit) {
    if (DEBUG_GET_MODULE) {
        System.out.println("Trying to get module in java project modules manager: " + name);
    }
    if (name.startsWith(".")) { //this happens when looking for a relative import
        return null;
    }
    try {
        IJavaElement javaElement = this.javaProject.findType(name);
        if (javaElement == null) {
            javaElement = this.javaProject.findElement(new Path(name.replace('.', '/')));
        }

        if (DEBUG_GET_MODULE) {
            System.out.println("Found: " + javaElement);
        }

        if (javaElement != null) {

            //now, there's a catch here, we'll find any class in the project classpath, even if it's in the
            //global classpath (e.g.: rt.jar), and this shouldn't be treated in this project modules manager
            //(that's treated in the Jython system manager)
            IJavaElement ancestor = javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            if (ancestor instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) ancestor;
                IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
                if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    return null;
                }
            }
            return new JavaModuleInProject(name, this.javaProject);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:org.seasar.s2daoplugin.util.JavaProjectUtil.java

License:Apache License

public static IPackageFragmentRoot[] findPackageFragmentRootsSharedOutputLocation(IPackageFragmentRoot root) {
    if (root == null) {
        return new IPackageFragmentRoot[0];
    }/*ww  w .ja  v a2s .  c o  m*/
    IJavaProject project = root.getJavaProject();
    Set result = new HashSet();
    try {
        IPath output1 = root.getRawClasspathEntry().getOutputLocation();
        if (output1 == null) {
            output1 = project.getOutputLocation();
        }
        IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_BINARY) {
                continue;
            }
            IPath output2 = roots[i].getRawClasspathEntry().getOutputLocation();
            if (output2 == null) {
                output2 = project.getOutputLocation();
            }
            if (output1.equals(output2)) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
        S2DaoPlugin.log(e);
    }
    return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:org.springframework.ide.eclipse.beans.ui.properties.NonJavaResourceContentProvider.java

License:Open Source License

protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen()) {
        return NO_CHILDREN;
    }//from w  ww.  j av  a  2 s  . co  m

    List<IJavaElement> list = new ArrayList<IJavaElement>();
    for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
        if (!JVM_CLASSPATH_CONTAINER.isPrefixOf(root.getRawClasspathEntry().getPath())) {
            if (isProjectPackageFragmentRoot(root)) {
                for (IJavaElement element : root.getChildren()) {
                    list.add(element);
                }
            } else if (hasChildren(root)) {
                list.add(root);
            }
        }
    }
    return concatenate(list.toArray(), project.getNonJavaResources());
}

From source file:org.springframework.ide.eclipse.boot.dash.cloudfoundry.JavaPackageFragmentRootHandler.java

License:Open Source License

/**
 *
 * Determines if the given package fragment root corresponds to the class
 * path entry path.//  w ww.  jav  a 2 s.  c om
 * <p/>
 * Note that different package fragment roots may point to the same class
 * path entry.
 * <p/>
 * Example:
 * <p/>
 * A Java project may have the following package fragment roots:
 * <p/>
 * - src/main/java
 * <p/>
 * - src/main/resources
 * <p/>
 * Both may be using the same output folder:
 * <p/>
 * target/classes.
 * <p/>
 * In this case, the output folder will have a class path entry -
 * target/classes - and it will be the same for both roots, and this method
 * will return true for both roots if passed the entry for target/classes
 *
 * @param root
 *            to check if it corresponds to the given class path entry path
 * @param entry
 * @return true if root is at the given entry
 */
private static boolean isRootAtEntry(IPackageFragmentRoot root, IPath entry) {
    try {
        IClasspathEntry cpe = root.getRawClasspathEntry();
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = cpe.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = root.getJavaProject().getOutputLocation();
            }

            IPath location = ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
            if (entry.equals(location)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
        BootDashActivator.log(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) {
        return true;
    }

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) {
        return true;
    }

    return false;
}

From source file:org.springframework.ide.eclipse.boot.templates.BootJavaContext.java

License:Open Source License

public boolean isTestContext() {
    //True if the current file is in a src folder that has a segment with name 'test' in its path.
    try {//w  w w  .j a  v  a  2s.co  m
        ICompilationUnit cu = getCompilationUnit();
        if (cu != null) {
            IPackageFragmentRoot pfr = (IPackageFragmentRoot) cu
                    .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            if (pfr != null) {
                IClasspathEntry cpe = pfr.getRawClasspathEntry();
                if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath sourcePath = cpe.getPath().removeFirstSegments(1); //remove first... it doesn't count if project is called 'test'.
                    if (sourcePath != null) {
                        for (String segment : sourcePath.segments()) {
                            if (segment.equals("test")) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return false;
}

From source file:org.springframework.ide.eclipse.core.io.EclipsePathMatchingResourcePatternResolver.java

License:Open Source License

private Resource processClassResource(String path, String fileName, String typeName, IPackageFragmentRoot root,
        IJavaElement[] children) throws JavaModelException {
    for (IJavaElement je : children) {
        if (je instanceof ICompilationUnit) {
            for (IType type : ((ICompilationUnit) je).getAllTypes()) {
                if (type.getFullyQualifiedName('$').equals(typeName)) {
                    if (root.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                        IPath outputLocation = root.getRawClasspathEntry().getOutputLocation();
                        if (outputLocation == null) {
                            outputLocation = root.getJavaProject().getOutputLocation();
                        }/*from   w w  w .  j a v a2  s  . c o  m*/
                        IResource classResource = ResourcesPlugin.getWorkspace().getRoot()
                                .findMember(outputLocation.append(path));
                        if (classResource != null) {
                            return new FileResource((IFile) classResource);
                        }
                    }
                }
            }
        } else if (je instanceof IClassFile) {
            if (((IClassFile) je).getElementName().equals(fileName)) {
                // Workspace jar or resource
                if (root.getResource() != null) {
                    return new StorageResource(new ZipEntryStorage((IFile) root.getResource(), path), project);
                }
                // Workspace external jar
                else {
                    File jarFile = root.getPath().toFile();
                    return new ExternalFile(jarFile, path, project);
                }
            }
        }
    }
    return null;
}

From source file:org.springsource.ide.eclipse.commons.frameworks.ui.internal.contentassist.JavaProjectPackageCompletionProcessor.java

License:Open Source License

public ICompletionProposal[] computeCompletionProposals(
        IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) {
    if (project == null) {
        return null;
    }/*from   w ww . j  a v  a  2 s .com*/
    String input = contentAssistSubjectControl.getDocument().get();
    ICompletionProposal[] foundProposals = null;

    IPackageFragmentRoot[] roots;
    try {
        roots = project.getAllPackageFragmentRoots();
        if (roots == null) {
            return null;
        }
    } catch (JavaModelException e1) {
        return null;
    }

    List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    String prefix = input.substring(0, documentOffset);
    for (IPackageFragmentRoot root : roots) {
        try {
            // To filter out all other package fragments from dependencies,
            // ONLY check for source root types. A similar thing is done
            // for the Java new type wizard. If this needs to be changed
            // comment out the CPE_SOURCE check below.
            IClasspathEntry entry = root.getRawClasspathEntry();
            if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            IJavaElement[] children = root.getChildren();
            for (IJavaElement child : children) {
                // Check for duplicates
                if (child instanceof IPackageFragment) {
                    IPackageFragment packageFragment = (IPackageFragment) child;
                    String packName = packageFragment.getElementName();
                    if (packName.startsWith(prefix)) {
                        Image image = labelProvider.getImage(packageFragment);
                        JavaCompletionProposal proposal = new JavaCompletionProposal(packName, 0,
                                input.length(), image, labelProvider.getText(packageFragment), 0);
                        proposals.add(proposal);
                    }
                }

            }
        } catch (JavaModelException e) {
            // no proposals for this package fragment root, skip to next
        }
    }

    foundProposals = (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);

    Arrays.sort(foundProposals, proposalComparator);
    return foundProposals;
}

From source file:org.whole.lang.ui.controls.JavaSourceFolderFilter.java

License:Open Source License

public boolean select(Viewer viewer, Object parent, Object element) {
    // filter any non container resource
    if (!(element instanceof IContainer))
        return false;

    IContainer container = (IContainer) element;
    IProject proj = container.getProject();

    try {/*w ww .j  a va2  s  . c om*/
        if (!proj.hasNature(JavaCore.NATURE_ID))
            return false;

        IJavaProject jProject = JavaCore.create(container.getProject());
        if (jProject == null || !jProject.exists())
            return false;

        // get any project container
        if (container == proj)
            return true;

        IPackageFragmentRoot sourceLocation = jProject.getPackageFragmentRoot(container);
        IClasspathEntry cpentry = sourceLocation.getRawClasspathEntry();

        if (cpentry != null && cpentry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            return true;
        } else {
            return false;
        }
    } catch (CoreException ex) {
        return false;
    }
}

From source file:org.whole.lang.ui.controls.JavaSourceFolderValidator.java

License:Open Source License

private boolean isJavaSourceFolder(Object element) {
    if (!(element instanceof IContainer))
        return false;

    IContainer container = (IContainer) element;
    IProject proj = container.getProject();

    try {//w w w. j  a  v a 2 s.c  om
        if (!proj.hasNature(JavaCore.NATURE_ID))
            return false;

        IJavaProject jProject = JavaCore.create(container.getProject());
        if (jProject == null || !jProject.exists())
            return false;

        IPackageFragmentRoot sourceLocation = jProject.getPackageFragmentRoot(container);
        IClasspathEntry cpentry = sourceLocation.getRawClasspathEntry();

        if (cpentry != null && cpentry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            return true;
        } else {
            return false;
        }
    } catch (CoreException ex) {
        return false;
    }
}

From source file:org.wso2.developerstudio.eclipse.artifact.axis2.ui.action.WSDLGenerationAction.java

License:Open Source License

/**
 * Method for adding class path locations to a list
 *
 * @param classpathLocation  output location to be added
 * @param classpathLocations List that contains class path locations
 * @param project            Axis2 service project
 * @throws org.eclipse.jdt.core.JavaModelException
 *///from  w w w.j av a 2  s. com
private void updateClassPathLocations(File classpathLocation, List<File> classpathLocations, IProject project)
        throws JavaModelException {
    classpathLocations.add(classpathLocation);
    IPackageFragmentRoot[] referencedLibrariesForProject = JavaUtils.getReferencedLibrariesForProject(project);
    for (IPackageFragmentRoot packageFragmentRoot : referencedLibrariesForProject) {
        IClasspathEntry resolvedClasspathEntry = JavaCore
                .getResolvedClasspathEntry(packageFragmentRoot.getRawClasspathEntry());
        IPath path = resolvedClasspathEntry.getPath();
        File libPath = path.toFile();
        if (!libPath.exists()) {
            libPath = project.getLocation().append(path.removeFirstSegments(1)).toFile();
        }
        classpathLocations.add(libPath);
    }
}