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

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

Introduction

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

Prototype

IPackageFragmentRoot[] findPackageFragmentRoots(IClasspathEntry entry);

Source Link

Document

Returns the existing package fragment roots identified by the given entry.

Usage

From source file:org.jboss.tools.arquillian.core.internal.builder.ArquillianBuilder.java

License:Open Source License

private List<ICompilationUnit> getCompilationUnits() throws JavaModelException {
    List<ICompilationUnit> units = new ArrayList<ICompilationUnit>();
    IJavaProject javaProject = JavaCore.create(currentProject);
    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                for (IPackageFragmentRoot root : roots) {
                    if (root.isArchive()) {
                        continue;
                    }//from w ww.  j a v a 2  s .c  o m
                    IJavaElement[] children = root.getChildren();
                    for (IJavaElement child : children) {
                        if (child instanceof IPackageFragment) {
                            IPackageFragment packageFragment = (IPackageFragment) child;
                            ICompilationUnit[] cus = packageFragment.getCompilationUnits();
                            for (ICompilationUnit cu : cus) {
                                if (!units.contains(cu)
                                        && ArquillianSearchEngine.isArquillianJUnitTest(cu, false, false)) {
                                    units.add(cu);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return units;
}

From source file:org.jboss.tools.arquillian.core.internal.util.ArquillianSearchEngine.java

License:Open Source License

private static IFile getFile(IJavaProject javaProject, String fileName) throws JavaModelException {
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }/*from   w  w  w.j  a  va2 s  . co m*/
            for (IPackageFragmentRoot root : roots) {
                IPath path = root.getPath();
                path = path.append(fileName);
                IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
                IFile file = wsRoot.getFile(path);
                if (file != null && file.exists()) {
                    return file;
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getFile(IJavaProject javaProject, String fileName) throws JavaModelException {
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }/*from   w ww.  ja  v a 2s .c o  m*/
            for (IPackageFragmentRoot root : roots) {
                Object[] resources = root.getNonJavaResources();
                int segments = root.getPath().segmentCount();
                for (Object resource : resources) {
                    if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        IPath filePath = file.getProjectRelativePath();
                        IPath relativePath = filePath.removeFirstSegments(segments - 1);
                        if (fileName.equals(relativePath.toString())) {
                            return file;
                        }
                    }
                }

            }
        }
    }
    return null;
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getNewFile(IJavaProject javaProject, String arquillianProperties) throws CoreException {
    IProject project = javaProject.getProject();
    if (project.hasNature(IMavenConstants.NATURE_ID)) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                new NullProgressMonitor());
        MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
        Build build = mavenProject.getBuild();
        String testDirectory = null;
        List<Resource> testResources = build.getTestResources();
        if (testResources != null && testResources.size() > 0) {
            testDirectory = testResources.get(0).getDirectory();
        } else {/*from w ww.j  av  a 2s  .  c  o m*/
            testDirectory = build.getTestSourceDirectory();
        }
        File testDir = new File(testDirectory);
        if (testDir.isDirectory()) {
            File arquillianFile = new File(testDir, arquillianProperties);
            IPath path = new Path(arquillianFile.getAbsolutePath());
            IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
            if (!iFile.getParent().exists()) {
                IPath projectPath = javaProject.getProject().getLocation();
                IPath iFilePath = iFile.getLocation();
                if (iFilePath.toString().startsWith(projectPath.toString())) {
                    String s = iFilePath.toString().substring(projectPath.toString().length());
                    path = new Path(s);
                    return javaProject.getProject().getFile(path);
                }
            }
            return iFile;
        }
    }
    IPath path = null;
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }
            for (IPackageFragmentRoot root : roots) {
                path = root.getPath();
                break;
            }
        }
    }
    if (path == null) {
        throw new CoreException(new Status(IStatus.ERROR, ArquillianUIActivator.PLUGIN_ID, "Invalid project"));
    }
    IFolder folder = javaProject.getProject().getFolder(path);
    if (!folder.exists()) {
        IPath projectPath = javaProject.getPath();
        path = path.makeRelativeTo(projectPath);
        folder = javaProject.getProject().getFolder(path);
    }
    return folder.getFile(arquillianProperties);
}

From source file:org.jboss.tools.pde.sourcelookup.core.internal.utils.ClasspathUtils.java

License:Open Source License

public static IPackageFragmentRoot[] getPluginContainerEntries(IProject project) {
    if (!ProjectUtils.isPluginProject(project)) {
        return new IPackageFragmentRoot[0];
    }/* w  w w .  ja  v a 2s  .  com*/
    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry pluginContainer = null;
    try {
        pluginContainer = Stream.of(javaProject.getRawClasspath()).filter(cpe -> isPluginContainer(cpe))
                .findFirst().orElse(null);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    if (pluginContainer == null) {
        return new IPackageFragmentRoot[0];
    }
    IPackageFragmentRoot[] pfr = javaProject.findPackageFragmentRoots(pluginContainer);
    return pfr;
}

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

@NotNull
private static List<File> getFileByEntry(@NotNull IClasspathEntry entry, @NotNull IJavaProject javaProject) {
    List<File> files = Lists.newArrayList();

    IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(entry);
    if (packageFragmentRoots.length > 0) {
        for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
            IResource resource = packageFragmentRoot.getResource();
            if (resource != null) {
                files.add(resource.getLocation().toFile());
            } else { // This can be if resource is external
                files.add(packageFragmentRoot.getPath().toFile());
            }//from w  w  w .jav  a  2  s  .  c  o  m
        }
    } else {
        File file = entry.getPath().toFile();
        if (file.exists()) {
            files.add(file);
        }
    }

    return files;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Checks if the given {@link IResource} is or is contained in a source folder of its project.
 * @param resource The {@link IResource} to check.
 * @return {@code true} is source folder of its project or contained in a source folder of its project, {@code false} is somewhere else.
 * @throws JavaModelException Occurred Exception.
 *//*from   w w w. j av  a2s.  com*/
public static boolean isInSourceFolder(IResource resource) throws JavaModelException {
    boolean inSourceFolder = false;
    if (resource != null) {
        IJavaProject javaProject = getJavaProject(resource.getProject());
        if (javaProject != null && javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            int i = 0;
            while (!inSourceFolder && i < entries.length) {
                if (entries[i].getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entries[i]);
                    int j = 0;
                    while (!inSourceFolder && j < roots.length) {
                        IResource rootResource = roots[j].getResource();
                        if (rootResource != null && rootResource.contains(resource)) {
                            inSourceFolder = true;
                        }
                        j++;
                    }
                }
                i++;
            }
        }
    }
    return inSourceFolder;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns all source {@link IPackageFragmentRoot}s.
 * @param javaProject The {@link IJavaProject} to read source {@link IPackageFragmentRoot}s from.
 * @return The found {@link IPackageFragmentRoot}s.
 * @throws JavaModelException Occurred Exception.
 *//*from  w w w . j a va2s  . c  o m*/
public static List<IPackageFragmentRoot> getSourcePackageFragmentRoots(IJavaProject javaProject)
        throws JavaModelException {
    List<IPackageFragmentRoot> result = new LinkedList<IPackageFragmentRoot>();
    if (javaProject != null && javaProject.exists()) {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                    && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                CollectionUtil.addAll(result, roots);
            }
        }
    }
    return result;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns the {@link IResource}s of the given {@link IClasspathEntry}.
 * @param javaProject The actual {@link IJavaProject} that provides the {@link IClasspathEntry}.
 * @param entry The given {@link IClasspathEntry}.
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found {@link IResource}s.
 * @throws JavaModelException //from  ww  w  .  j  ava 2 s. c o m
 */
private static List<IResource> getResourceFor(IJavaProject javaProject, IClasspathEntry entry, int expectedKind,
        Set<IProject> alreadyHandledProjects) throws JavaModelException {
    if (entry != null) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                || entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                || entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            List<IResource> result = new LinkedList<IResource>();
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            for (IPackageFragmentRoot root : roots) {
                if (root.getKind() == expectedKind) {
                    if (root.getResource() != null) {
                        if (root.getResource().getLocationURI() != null) {
                            result.add(root.getResource());
                        }
                    } else if (root.getPath() != null) {
                        IResource resource = ResourcesPlugin.getWorkspace().getRoot()
                                .findMember(root.getPath());
                        if (resource != null && resource.exists()) {
                            result.add(resource);
                        }
                    }
                }
            }
            return result; // Ignore containers
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            Assert.isNotNull(entry.getPath());
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            Assert.isTrue(project instanceof IProject);
            if (!alreadyHandledProjects.contains(project)) {
                return getSourceResources((IProject) project, alreadyHandledProjects);
            } else {
                return null; // Project was already analyzed, no need to do it again.
            }
        } else {
            Assert.isTrue(false, "Unknown content kind \"" + entry.getContentKind()
                    + "\" of class path entry \"" + entry + "\".");
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns the locations of the given {@link IClasspathEntry}.
 * @param javaProject The actual {@link IJavaProject} that provides the {@link IClasspathEntry}.
 * @param entry The given {@link IClasspathEntry}.
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found locations./*from   w ww . j  ava  2 s . c om*/
 * @throws JavaModelException 
 */
private static List<File> getLocationFor(IJavaProject javaProject, IClasspathEntry entry, int expectedKind,
        Set<IProject> alreadyHandledProjects) throws JavaModelException {
    if (entry != null) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                || entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                || entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            List<File> result = new LinkedList<File>();
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            for (IPackageFragmentRoot root : roots) {
                if (root.getKind() == expectedKind) {
                    if (root.getResource() != null) {
                        if (root.getResource().getLocationURI() != null) {
                            result.add(ResourceUtil.getLocation(root.getResource()));
                        }
                    } else if (root.getPath() != null) {
                        File location = new File(root.getPath().toString());
                        if (location.exists()) {
                            result.add(location);
                        }
                    }
                }
            }
            return result; // Ignore containers
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            Assert.isNotNull(entry.getPath());
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            Assert.isTrue(project instanceof IProject);
            if (!alreadyHandledProjects.contains(project)) {
                return getSourceLocations((IProject) project, alreadyHandledProjects);
            } else {
                return null; // Project was already analyzed, no need to do it again.
            }
        } else {
            Assert.isTrue(false, "Unknown content kind \"" + entry.getContentKind()
                    + "\" of class path entry \"" + entry + "\".");
            return null;
        }
    } else {
        return null;
    }
}