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

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

Introduction

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

Prototype

IPackageFragmentRoot[] getAllPackageFragmentRoots() throws JavaModelException;

Source Link

Document

Returns all of the existing package fragment roots that exist on the classpath, in the order they are defined by the classpath.

Usage

From source file:org.hibernate.eclipse.console.utils.OpenMappingUtils.java

License:Open Source License

/**
 * Trying to find hibernate console config ejb3 mapping file,
 * which is corresponding to provided element.
 *   /* w w w .  jav a2  s .  co  m*/
 * @param consoleConfig
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfig, Object element) {
    IFile file = null;
    if (consoleConfig == null) {
        return file;
    }
    final ConsoleConfiguration cc2 = consoleConfig;
    List<String> documentPaths = (List<String>) consoleConfig.execute(new ExecutionContext.Command() {
        public Object execute() {
            String persistenceUnitName = cc2.getPreferences().getPersistenceUnitName();
            EntityResolver entityResolver = cc2.getConfiguration().getEntityResolver();
            IService service = cc2.getHibernateExtension().getHibernateService();
            return service.getJPAMappingFilePaths(persistenceUnitName, entityResolver);
        }
    });
    if (documentPaths == null) {
        return file;
    }
    IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
    ArrayList<IPath> pathsSrc = new ArrayList<IPath>();
    ArrayList<IPath> pathsOut = new ArrayList<IPath>();
    ArrayList<IPath> pathsFull = new ArrayList<IPath>();
    for (int i = 0; i < projs.length; i++) {
        IJavaProject proj = projs[i];
        IPath projPathFull = proj.getResource().getLocation();
        IPath projPath = proj.getPath();
        IPath projPathOut = null;
        try {
            projPathOut = proj.getOutputLocation();
            projPathOut = projPathOut.makeRelativeTo(projPath);
        } catch (JavaModelException e) {
            // just ignore
        }
        IPackageFragmentRoot[] pfrs = new IPackageFragmentRoot[0];
        try {
            pfrs = proj.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
            // just ignore
        }
        for (int j = 0; j < pfrs.length; j++) {
            // TODO: think about possibility to open resources from jar files
            if (pfrs[j].isArchive() || pfrs[j].isExternal()) {
                continue;
            }
            final IPath pathSrc = pfrs[j].getPath();
            final IPath pathOut = projPathOut;
            final IPath pathFull = projPathFull;
            pathsSrc.add(pathSrc);
            pathsOut.add(pathOut);
            pathsFull.add(pathFull);
        }
    }
    int scanSize = Math.min(pathsSrc.size(), pathsOut.size());
    scanSize = Math.min(pathsFull.size(), scanSize);
    for (int i = 0; i < scanSize && file == null; i++) {
        final IPath pathSrc = pathsSrc.get(i);
        final IPath pathOut = pathsOut.get(i);
        final IPath pathFull = pathsFull.get(i);
        Iterator<String> it = documentPaths.iterator();
        while (it.hasNext() && file == null) {
            String docPath = it.next();
            IPath path2DocFull = Path.fromOSString(docPath);
            IPath resPath = path2DocFull.makeRelativeTo(pathFull);
            if (pathOut != null) {
                resPath = resPath.makeRelativeTo(pathOut);
            }
            resPath = pathSrc.append(resPath);
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(resPath);
            if (file == null || !file.exists()) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(resPath);
            }
            if (file != null && file.exists()) {
                if (elementInFile(consoleConfig, file, element)) {
                    break;
                }
            }
            file = null;
        }
    }
    return file;
}

From source file:org.hibernate.eclipse.console.wizards.NewConfigurationWizard.java

License:Open Source License

/**
 * We will accept the selection in the workbench to see if
 * we can initialize from it./*from  www  .  j av a 2 s.  c om*/
 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
 */
public void init(IWorkbench workbench, IStructuredSelection selection) {
    this.selection = selection;
    if (!selection.isEmpty()) {
        IJavaProject jproj = null;
        if (selection.getFirstElement() instanceof IJavaProject) {
            jproj = (IJavaProject) selection.getFirstElement();
        }
        if (selection.getFirstElement() instanceof IProject) {
            try {
                if (((IProject) selection.getFirstElement()).getNature(JavaCore.NATURE_ID) != null) {
                    jproj = (IJavaProject) ((IProject) selection.getFirstElement())
                            .getNature(JavaCore.NATURE_ID);
                }
            } catch (CoreException e) {
                HibernateConsolePlugin.getDefault().log(e);
            }
        }
        if (jproj != null) {
            IPackageFragmentRoot[] roots;
            try {
                roots = jproj.getAllPackageFragmentRoots();
                if (roots.length > 0) {
                    this.selection = new StructuredSelection(roots[0]);
                }
            } catch (JavaModelException e) {
                HibernateConsolePlugin.getDefault().log(e);
            }
        }
        ;
    }
}

From source file:org.hibernate.eclipse.jdt.ui.internal.jpa.collect.CompilationUnitCollector.java

License:Open Source License

/**
 * Process object - java element to collect all it's children CompilationUnits
 * @param obj/*from   w  w w  .  j  av a  2  s. c  o m*/
 * @param bCollect
 */
public void processJavaElements(Object obj, boolean bCollect) {
    if (obj instanceof ICompilationUnit) {
        ICompilationUnit cu = (ICompilationUnit) obj;
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof File) {
        File file = (File) obj;
        if (file.getProject() != null) {
            IJavaProject javaProject = JavaCore.create(file.getProject());
            ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject, file.getFullPath());
            if (cus != null) {
                for (int i = 0; i < cus.length; i++) {
                    addCompilationUnit(cus[i], bCollect);
                }
            }
        }
    } else if (obj instanceof JavaProject) {
        JavaProject javaProject = (JavaProject) obj;
        IPackageFragmentRoot[] pfr = null;
        try {
            pfr = javaProject.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
            // just ignore it!
            //HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (pfr != null) {
            for (int i = 0; i < pfr.length; i++) {
                processJavaElements(pfr[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragment) {
        PackageFragment packageFragment = (PackageFragment) obj;
        ICompilationUnit[] cus = null;
        try {
            cus = packageFragment.getCompilationUnits();
        } catch (JavaModelException e) {
            // just ignore it!
            //HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (cus != null && cus.length > 0) {
            if (bCollect) {
                selection2UpdateList.add(obj);
                bCollect = false;
            }
            for (int i = 0; i < cus.length; i++) {
                addCompilationUnit(cus[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragmentRoot) {
        JavaElement javaElement = (JavaElement) obj;
        JavaElementInfo javaElementInfo = null;
        try {
            javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
        } catch (JavaModelException e) {
            // just ignore it!
            //HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (javaElementInfo != null) {
            IJavaElement[] je = javaElementInfo.getChildren();
            for (int i = 0; i < je.length; i++) {
                processJavaElements(je[i], true);
            }
        }
    } else if (obj instanceof JavaElement) {
        JavaElement javaElement = (JavaElement) obj;
        ICompilationUnit cu = javaElement.getCompilationUnit();
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof SourceType) {
        if (bCollect) {
            selection2UpdateList.add(obj);
            bCollect = false;
        }
        SourceType sourceType = (SourceType) obj;
        processJavaElements(sourceType.getJavaModel(), bCollect);
    } else {
        // ignore
        //System.out.println("1 Blah! " + selection); //$NON-NLS-1$
    }
}

From source file:org.hyperic.hypclipse.internal.util.HQDEJavaHelper.java

License:Open Source License

public static IPackageFragmentRoot[] getNonJRERoots(IJavaProject project) {
    ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
    try {/*from   w  w  w  .  j a  v  a 2 s  . c o m*/
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (!isJRELibrary(roots[i])) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
    }
    return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:org.j2eespider.util.ClassSearchUtil.java

License:Open Source License

/**
 * Return path of JavaSources defined in .classpath
 * @return/*from   ww  w. j ava  2  s.c o  m*/
 * @throws JavaModelException
 */
public static List<String> getJavaSources() throws JavaModelException {
    List<String> javaSources = new ArrayList<String>();

    IJavaProject javaProject = JavaCore.create(ConfigurationEditor.activeProject);
    IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
    if (roots != null && roots.length > 0) {
        for (IPackageFragmentRoot root : roots) {
            if (root.getPath().segment(0).equals(javaProject.getElementName())) {
                javaSources.add(root.getPath().removeFirstSegments(1).makeRelative().toString());
            }
        }
    }

    return javaSources;
}

From source file:org.jaml.eclipse.utils.JDTUtils.java

License:Open Source License

public static List<IType> findJavaTypes(IJavaProject javaProject) throws JavaModelException {
    List<IType> list = new LinkedList<IType>();
    findJavaTypes(list, javaProject.getAllPackageFragmentRoots());
    return list;/*  w  w  w.j  a  v  a 2 s.  c  om*/
}

From source file:org.jboss.ide.eclipse.freemarker.configuration.ProjectClassLoader.java

License:Open Source License

private static URL[] getURLSFromProject(IJavaProject project, URL[] extraUrls) throws JavaModelException {
    List<URL> list = new ArrayList<URL>();
    if (null != extraUrls) {
        for (int i = 0; i < extraUrls.length; i++) {
            list.add(extraUrls[i]);//from   w  ww.j a v  a 2s . c  o  m
        }
    }

    IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
    String installLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath();
    installLoc = installLoc.replace('\\', '/');
    if (installLoc.endsWith("/")) //$NON-NLS-1$
        installLoc = installLoc.substring(0, installLoc.length() - 1);

    for (int i = 0; i < roots.length; i++) {
        try {
            if (roots[i].isArchive()) {
                File f = new File(FileLocator
                        .resolve(roots[i].getPath().makeAbsolute().toFile().toURI().toURL()).getFile());
                if (f.exists()) {
                    list.add(FileLocator.resolve(roots[i].getPath().makeAbsolute().toFile().toURI().toURL()));
                } else {
                    String s = roots[i].getPath().toOSString().replace('\\', '/');
                    if (!s.startsWith("/")) //$NON-NLS-1$
                        s = "/" + s; //$NON-NLS-1$
                    f = new File(installLoc + s);
                    if (f.exists()) {
                        list.add(f.toURI().toURL());
                    } else {
                        f = new File("c:" + installLoc + s); //$NON-NLS-1$
                        if (f.exists()) {
                            list.add(f.toURI().toURL());
                        } else {
                            f = new File("d:" + installLoc + s); //$NON-NLS-1$
                            if (f.exists()) {
                                list.add(f.toURI().toURL());
                            }
                        }
                    }
                }
            } else {
                IPath path = roots[i].getJavaProject().getOutputLocation();
                if (path.segmentCount() > 1) {
                    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                    path = root.getFolder(path).getLocation();
                    list.add(path.toFile().toURI().toURL());
                } else {
                    path = roots[i].getJavaProject().getProject().getLocation();
                    list.add(path.toFile().toURI().toURL());
                }
            }
        } catch (Exception e) {
            Plugin.log(e);
        }
    }

    return list.toArray(new URL[list.size()]);
}

From source file:org.jboss.tools.arquillian.ui.internal.detectors.ArquillianResourceHyperlinkDetector.java

License:Open Source License

private IFile getFile(String resource, ITypeRoot javaElement) {
    IJavaProject project = javaElement.getJavaProject();
    try {//www.  ja  v a2  s . c o m
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = root.getPath();
                path = path.append(resource);
                path = path.removeFirstSegments(1);
                IFile file = project.getProject().getFile(path);
                if (file != null && file.exists()) {
                    return file;
                }
            }
        }
    } catch (JavaModelException e) {
        ArquillianUIActivator.log(e);
    }
    return null;
}

From source file:org.jboss.tools.cdi.seam.config.ui.contentassist.SeamConfigXmlCompletionProposalComputer.java

License:Open Source License

public static Map<String, IMember> findTypeNamesByPackage(IJavaProject javaProject, String packageName,
        boolean annotationsOnly, boolean classesOnly) throws JavaModelException {
    Map<String, IMember> result = new HashMap<String, IMember>();
    if (PACKAGE_EE.equals(packageName)) {
        for (String name : Util.EE_TYPES.keySet()) {
            String cls = Util.EE_TYPES.get(name);
            IType t = EclipseJavaUtil.findType(javaProject, cls);
            if (accept(t, annotationsOnly, classesOnly))
                result.put(name, t);/*from  ww w .  j a  va  2s.c  om*/
        }
        for (String name : Util.EE_TYPES_30.keySet()) {
            String cls = Util.EE_TYPES_30.get(name);
            IType t = EclipseJavaUtil.findType(javaProject, cls);
            if (accept(t, annotationsOnly, classesOnly))
                result.put(name, t);
        }
    } else if (javaProject != null) {
        IPackageFragmentRoot[] rs = javaProject.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot r : rs) {
            IPackageFragment pkg = r.getPackageFragment(packageName);
            if (pkg != null && pkg.exists()) {
                ICompilationUnit[] units = pkg.getCompilationUnits();
                for (ICompilationUnit u : units) {
                    IType[] ts = u.getTypes();
                    for (IType t : ts)
                        if (accept(t, annotationsOnly, classesOnly))
                            result.put(t.getElementName(), t);
                }
                IClassFile[] cs = pkg.getClassFiles();
                for (IClassFile cls : cs) {
                    if (accept(cls.getType(), annotationsOnly, classesOnly))
                        result.put(cls.getType().getElementName(), cls.getType());
                }
            }
        }

    }
    return result;
}

From source file:org.jboss.tools.cdi.seam.config.ui.contentassist.SeamConfigXmlCompletionProposalComputer.java

License:Open Source License

public static Set<String> getAllPackages(IJavaProject javaProject) throws JavaModelException {
    Set<String> result = new HashSet<String>();
    IPackageFragmentRoot[] rs = javaProject.getAllPackageFragmentRoots();
    for (IPackageFragmentRoot r : rs) {
        IJavaElement[] cs = r.getChildren();
        for (IJavaElement c : cs) {
            if (c instanceof IPackageFragment) {
                result.add(((IPackageFragment) c).getElementName());
            }/*from  w  w  w  .  j a  v  a2  s .  co  m*/
        }
    }
    return result;
}