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.eclipse.xtext.builder.JDTAwareEclipseResourceFileSystemAccess2.java

License:Open Source License

/**
 * @since 2.6//from ww  w .j  a  va 2s .c  o  m
 */
protected void addToSourceFolders(IContainer container) throws JavaModelException {
    IJavaProject jp = JavaCore.create(container.getProject());
    if (jp.exists() && !jp.isOnClasspath(container)) {
        IPackageFragmentRoot currentSource = jp
                .findPackageFragmentRoot(jp.getPath().append(getCurrentSource()));
        if (currentSource != null) {
            IClasspathEntry currentClasspathEntry = currentSource.getRawClasspathEntry();
            if (currentClasspathEntry != null) {
                insertClasspathEntry(container, currentClasspathEntry, jp);
                return;
            }
        }
        addClasspathEntry(container, jp);
    }
}

From source file:org.eclipse.xtext.ui.containers.JavaProjectsStateHelper.java

License:Open Source License

protected List<String> getPackageFragmentRootHandles(IJavaProject project) {
    List<String> result = Lists.newArrayList();
    List<String> binaryAndNonLocalFragments = Lists.newArrayList();
    try {/*from   w w  w .ja  v a 2s  .com*/
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        result = Lists.newArrayListWithCapacity(roots.length);
        for (IPackageFragmentRoot root : roots) {
            if (root != null && !JavaRuntime.newDefaultJREContainerPath()
                    .isPrefixOf(root.getRawClasspathEntry().getPath())) {
                if (root.getKind() == IPackageFragmentRoot.K_SOURCE && project.equals(root.getJavaProject())) {
                    // treat local sources with higher priority
                    // see Java behavior in SameClassNamesTest
                    result.add(root.getHandleIdentifier());
                } else {
                    binaryAndNonLocalFragments.add(root.getHandleIdentifier());
                }
            }
        }
    } catch (JavaModelException e) {
        if (!e.isDoesNotExist()) {
            log.error("Cannot find rootHandles in project " + project.getProject().getName(), e);
        }
    }
    result.addAll(binaryAndNonLocalFragments);
    return result;
}

From source file:org.eclipseguru.gwt.ui.java.GwtJavaUtil.java

License:Open Source License

/**
 * Indicates if this belongs to a JRE library.
 * //from ww  w  .  ja  v  a  2s. c  om
 * @param root
 * @return
 */
public static boolean isJRELibrary(final IPackageFragmentRoot root) {
    try {
        final IPath path = root.getRawClasspathEntry().getPath();
        if (path.equals(new Path(JavaRuntime.JRE_CONTAINER))
                || path.equals(new Path(JavaRuntime.JRELIB_VARIABLE)))
            return true;
    } catch (final JavaModelException e) {
    }
    return false;
}

From source file:org.grails.ide.eclipse.core.internal.GrailsResourceUtil.java

License:Open Source License

public static boolean isGrailsDependencyPackageFragmentRoot(IPackageFragmentRoot root) {
    if (root == null) {
        return false;
    }//from  w  w  w  .  j a  v  a2 s  .  c om

    try {
        return isGrailsClasspathEntry(root.getRawClasspathEntry());
    } catch (JavaModelException e) {
        GrailsCoreActivator.log(e);
        return false;
    }

}

From source file:org.grails.ide.eclipse.core.internal.GrailsResourceUtil.java

License:Open Source License

/**
 * Determines if the given folder corresponds to a Java source folder (i.e.
 * there is a source class path entry for it).
 * <p>//from w w w  .  j av a2 s .  c  om
 * The folder could be either a package fragment or a package fragment root
 * </p>
 * 
 * @param folder
 *            to check if there is a corresponding source class path entry
 *            for it
 * @return true if there is a corresponding source class path entry for it.
 *         False otherwise.
 */
public static boolean isSourceFolder(IFolder folder) {
    if (folder == null) {
        return false;
    }
    IJavaElement possiblePackageFragRoot = JavaCore.create(folder);
    if (possiblePackageFragRoot == null) {
        return false;
    }

    if (possiblePackageFragRoot instanceof IPackageFragment) {
        possiblePackageFragRoot = ((IPackageFragment) possiblePackageFragRoot).getParent();
    }

    if (possiblePackageFragRoot instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) possiblePackageFragRoot;
        try {
            if (root.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                return true;
            }
        } catch (JavaModelException e) {
            GrailsCoreActivator.log(e);
        }
    }
    return false;
}

From source file:org.grails.ide.eclipse.explorer.providers.GrailsNavigatorContentProvider.java

License:Open Source License

public Object[] getChildren(Object parentElement) {

    if (parentElement instanceof IProject && GrailsNature.isGrailsProject((IProject) parentElement)) {

        IProject project = (IProject) parentElement;

        Collection<GrailsProjectStructureTypes> types = GrailsProjectStructureManager.getInstance()
                .getAllTopLevelLogicalFolders().values();
        List<Object> topLevelFolders = new ArrayList<Object>();

        GrailsFolderElementFactory factory = getFolderElementFactory();
        for (GrailsProjectStructureTypes type : types) {
            ILogicalFolder element = factory.getElement(project,
                    project.getFolder(new Path(type.getFolderName())), type);
            if (element != null) {
                topLevelFolders.add(element);
            }/*from  www. j a  v  a2s. c  o  m*/
        }

        //Add a logical folder that contains the classpath containers
        topLevelFolders.add(new GrailsClasspathContainersFolder(project));

        // Now add the top level package fragment roots that are
        // non-dependency source folders
        IJavaProject javaProject = JavaCore.create(project);
        try {
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            if (roots != null) {
                for (IPackageFragmentRoot root : roots) {
                    if (root.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_SOURCE
                            && !GrailsResourceUtil.isGrailsDependencyPackageFragmentRoot(root)) {
                        topLevelFolders.add(root);
                    }
                }
            }
        } catch (JavaModelException e) {
            GrailsCoreActivator.log(e);
        }

        // Add the file folders that are reimaged
        Set<GrailsProjectStructureTypes> fileFolders = GrailsProjectStructureManager.getInstance()
                .getGrailsFileFolders();
        if (fileFolders != null) {
            for (GrailsProjectStructureTypes type : fileFolders) {
                IFolder folder = project.getFolder(new Path(type.getFolderName()));
                if (folder != null && folder.exists()) {
                    topLevelFolders.add(folder);
                }
            }
        }

        try {
            IResource[] children = project.members();
            if (children != null) {
                for (IResource resource : children) {
                    // Skip the linked folders that correspond to
                    // Grails dependency package fragment roots
                    if (!isLinkedDependencyPackageFragmentRoot(resource) && !isReimagedResource(resource)) {
                        topLevelFolders.add(resource);
                    }
                }
            }
        } catch (CoreException e) {
            GrailsCoreActivator.log(e);
        }

        return topLevelFolders.toArray();
    } else if (parentElement instanceof ILogicalFolder) {
        ILogicalFolder element = (ILogicalFolder) parentElement;
        List<?> children = element.getChildren();
        if (children != null) {
            return children.toArray();
        }
    } else if (parentElement instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) parentElement;
        GrailsProjectStructureTypes type = GrailsResourceUtil.getGrailsContainerType(root);
        if (type == GrailsProjectStructureTypes.CONF) {
            try {
                IJavaElement[] children = root.getChildren();
                if (children != null) {
                    List<IJavaElement> elements = new ArrayList<IJavaElement>();
                    for (IJavaElement child : children) {

                        IPackageFragment frag = (child instanceof IPackageFragment) ? (IPackageFragment) child
                                : null;

                        if (frag == null || !frag.isDefaultPackage()) {
                            elements.add(child);
                        } else {
                            IJavaElement[] defaultChildren = frag.getChildren();
                            for (IJavaElement defaultChild : defaultChildren) {
                                elements.add(defaultChild);
                            }
                        }
                    }
                    return elements.toArray();
                }
            } catch (JavaModelException e) {
                GrailsCoreActivator.log(e);
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

private void attachSource(IPackageFragmentRoot fragment, IPath newSourcePath) {
    try {//www .  j ava 2 s  .  co m
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        IPath containerPath = null;
        IJavaProject jproject = fragment.getJavaProject();
        IClasspathEntry entry = fragment.getRawClasspathEntry();
        if (entry == null) {
            entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null);
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                containerPath = entry.getPath();
                ClasspathContainerInitializer initializer = JavaCore
                        .getClasspathContainerInitializer(containerPath.segment(0));
                IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
                if (initializer == null || container == null) {
                    return;
                }
                IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject);
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                    return;
                }
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                    return;
                }
                entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath());
                if (entry == null) {
                    return;
                }
            }
        }
        IClasspathEntry entry1;
        CPListElement elem = CPListElement.createFromExisting(entry, null);
        elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath);
        entry1 = elem.getClasspathEntry();
        if (entry1.equals(entry)) {
            return;
        }
        IClasspathEntry newEntry = entry1;
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath,
                newEntry.getReferencingEntry() != null, new NullProgressMonitor());
    } catch (CoreException e) {
        // ignore
    }
}

From source file:org.jboss.tools.maven.sourcelookup.ui.internal.util.SourceLookupUtil.java

License:Open Source License

public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath,
        boolean displayDialog) {
    try {// ww  w  . java2 s.c  om
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        String value = SourceLookupActivator.getDefault().getAutoAddSourceAttachment();
        if (SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_NEVER.equals(value)) {
            return;
        }
        if (displayDialog && SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_PROMPT.equals(value)) {
            final boolean[] attach = new boolean[1];
            Display.getDefault().syncExec(new Runnable() {

                @Override
                public void run() {
                    attach[0] = promptToAddSourceAttachment(fragment.getElementName(),
                            newSourcePath.toString());
                }
            });
            if (!attach[0]) {
                return;
            }
            ;
        }
        IPath containerPath = null;
        IJavaProject jproject = fragment.getJavaProject();
        IClasspathEntry entry = fragment.getRawClasspathEntry();
        if (entry == null) {
            entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null);
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                containerPath = entry.getPath();
                ClasspathContainerInitializer initializer = JavaCore
                        .getClasspathContainerInitializer(containerPath.segment(0));
                IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
                if (initializer == null || container == null) {
                    return;
                }
                IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject);
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                    return;
                }
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                    return;
                }
                entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath());
                if (entry == null) {
                    return;
                }
            }
        }
        IClasspathEntry entry1;
        CPListElement elem = CPListElement.createFromExisting(entry, null);
        elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath);
        entry1 = elem.getClasspathEntry();
        if (entry1.equals(entry)) {
            return;
        }
        IClasspathEntry newEntry = entry1;
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath,
                newEntry.getReferencingEntry() != null, new NullProgressMonitor());
    } catch (CoreException e) {
        // ignore
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchTasks.java

License:Open Source License

public static IPackageFragmentRoot addClasspathEntry(IJavaProject javaProject, String name,
        IProgressMonitor progressMonitor)
        throws CoreException, OperationCanceledException, InterruptedException {
    IPath path = javaProject.getProject().getLocation().append("lib").addTrailingSeparator().append(name);
    if (!path.toFile().exists() || !path.toFile().canRead()) {
        LOGGER.warn("Following library does not exist or is not readable: {} ", path.toFile());
    }//from   w w w  .ja  v  a2  s. c  o  m
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(path, null, null);
    classpathEntries = (IClasspathEntry[]) ArrayUtils.add(classpathEntries, newLibraryEntry);
    javaProject.setRawClasspath(classpathEntries, progressMonitor);
    buildProject(javaProject.getProject(), progressMonitor);
    for (IPackageFragmentRoot fragment : javaProject.getAllPackageFragmentRoots()) {
        if (fragment.getRawClasspathEntry().equals(newLibraryEntry)) {
            return fragment;
        }
    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

/**
 * Remove the first referenced library those absolute path contains the given name.
 * /* w  ww.  ja v a  2s.c om*/
 * @param javaProject
 * @param name
 * @param progressMonitor
 * @throws CoreException
 * @throws InterruptedException
 * @throws OperationCanceledException
 */
public static List<IPackageFragmentRoot> removeClasspathEntry(IJavaProject javaProject, String name,
        IProgressMonitor progressMonitor)
        throws CoreException, OperationCanceledException, InterruptedException {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    int index = 0;
    List<IPackageFragmentRoot> fragments = null;
    for (IClasspathEntry entry : classpathEntries) {
        if (entry.getPath().toFile().getAbsolutePath().contains(name)) {
            fragments = new ArrayList<IPackageFragmentRoot>();
            for (IPackageFragmentRoot fragment : javaProject.getAllPackageFragmentRoots()) {
                if (fragment.getRawClasspathEntry().equals(entry)) {
                    fragments.add(fragment);
                }
            }
            break;
        }
        index++;
    }
    if (index < classpathEntries.length) {
        classpathEntries = (IClasspathEntry[]) ArrayUtils.remove(classpathEntries, index);
        javaProject.setRawClasspath(classpathEntries, progressMonitor);
    }
    WorkbenchTasks.buildProject(javaProject.getProject(), progressMonitor);
    return fragments;
}