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

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

Introduction

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

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

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;
    }/*w  ww .j a v a  2  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 .ja v a  2s .co 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;
}

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

License:Open Source License

private Set<String> listPackages() {
    Set<String> packageList = new TreeSet<String>();
    String name = getNameAsString();
    if (JDKUtils.isJDKModule(name)) {
        packageList.addAll(JDKUtils.getJDKPackagesByModule(name));
    } else if (JDKUtils.isOracleJDKModule(name)) {
        packageList.addAll(JDKUtils.getOracleJDKPackagesByModule(name));
    } else if (isJava() || true) {
        for (IPackageFragmentRoot fragmentRoot : getPackageFragmentRoots()) {
            if (!fragmentRoot.exists())
                continue;
            IParent parent = fragmentRoot;
            listPackages(packageList, parent);
        }/*from   w w  w.  ja va 2s.c om*/
    }
    return packageList;
}

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

License:Open Source License

private static boolean moduleFileInProject(String moduleName, IJavaProject p) {
    if (p == null) {
        return false;
    }/* w  w w  .j  a  va2  s  .  c om*/
    try {
        for (IPackageFragmentRoot sourceFolder : p.getPackageFragmentRoots()) {
            if (!sourceFolder.isArchive() && sourceFolder.exists()
                    && sourceFolder.getKind() == IPackageFragmentRoot.K_SOURCE
                    && sourceFolder.getPackageFragment(moduleName).exists()) {
                return true;
            }
            /*IPath moduleFile = sourceFolder.append(moduleName.replace('.', '/') + 
                "/module.ceylon").makeRelativeTo(p.getFullPath());
            if (p.getFile(moduleFile).exists()) {
            return true;
            }*/
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return false;
}

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

License:Open Source License

@Override
protected JDTModule createModule(List<String> moduleName, String version) {
    JDTModule module = null;/*  w  w w .  j av  a  2  s  .  c  om*/
    String moduleNameString = Util.getName(moduleName);
    List<IPackageFragmentRoot> roots = new ArrayList<IPackageFragmentRoot>();
    if (javaProject != null) {
        try {
            if (moduleNameString.equals(Module.DEFAULT_MODULE_NAME)) {
                // Add the list of source package fragment roots
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.exists() && javaProject.isOnClasspath(root)) {
                        IClasspathEntry entry = root.getResolvedClasspathEntry();
                        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && !root.isExternal()) {
                            roots.add(root);
                        }
                    }
                }
            } else {
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.exists() && javaProject.isOnClasspath(root)) {
                        if (JDKUtils.isJDKModule(moduleNameString)) {
                            // find the first package that exists in this root
                            for (String pkg : JDKUtils.getJDKPackagesByModule(moduleNameString)) {
                                if (root.getPackageFragment(pkg).exists()) {
                                    roots.add(root);
                                    break;
                                }
                            }
                        } else if (JDKUtils.isOracleJDKModule(moduleNameString)) {
                            // find the first package that exists in this root
                            for (String pkg : JDKUtils.getOracleJDKPackagesByModule(moduleNameString)) {
                                if (root.getPackageFragment(pkg).exists()) {
                                    roots.add(root);
                                    break;
                                }
                            }
                        } else if (!(root instanceof JarPackageFragmentRoot)
                                && !CeylonBuilder.isInCeylonClassesOutputFolder(root.getPath())) {
                            String packageToSearch = moduleNameString;
                            if (root.getPackageFragment(packageToSearch).exists()) {
                                roots.add(root);
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }

    module = new JDTModule(this, roots);
    module.setName(moduleName);
    module.setVersion(version);
    setupIfJDKModule(module);
    return module;
}

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

License:Open Source License

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

    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())
                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()) {
                    if (!loadDeclarations) {
                        // we found the package
                        return true;
                    } else {
                        try {
                            for (IClassFile classFile : packageFragment.getClassFiles()) {
                                // skip removed class files
                                if (!classFile.exists())
                                    continue;
                                IType type = classFile.getType();
                                if (type.exists() && !type.isMember()
                                        && !sourceDeclarations.containsKey(
                                                getQualifiedName(type.getPackageFragment().getElementName(),
                                                        type.getTypeQualifiedName()))) {
                                    convertToDeclaration(type.getFullyQualifiedName(), DeclarationType.VALUE);
                                }
                            }
                            for (org.eclipse.jdt.core.ICompilationUnit compilationUnit : packageFragment
                                    .getCompilationUnits()) {
                                // skip removed CUs
                                if (!compilationUnit.exists())
                                    continue;
                                for (IType type : compilationUnit.getTypes()) {
                                    if (type.exists() && !type.isMember()
                                            && !sourceDeclarations.containsKey(
                                                    getQualifiedName(type.getPackageFragment().getElementName(),
                                                            type.getTypeQualifiedName()))) {
                                        convertToDeclaration(type.getFullyQualifiedName(),
                                                DeclarationType.VALUE);
                                    }
                                }
                            }
                        } catch (JavaModelException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

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

License:Open Source License

private void loadAllPackages() {
    Set<String> packageList = new TreeSet<String>();
    String name = getNameAsString();
    if (JDKUtils.isJDKModule(name)) {
        packageList.addAll(JDKUtils.getJDKPackagesByModule(name));
    } else if (JDKUtils.isOracleJDKModule(name)) {
        packageList.addAll(JDKUtils.getOracleJDKPackagesByModule(name));
    } else if (isJava()) {
        for (IPackageFragmentRoot fragmentRoot : getPackageFragmentRoots()) {
            if (!fragmentRoot.exists())
                continue;
            IParent parent = fragmentRoot;
            listPackages(packageList, parent);
        }//from  w  ww.jav  a  2 s.c om
    }
    for (String packageName : packageList) {
        getPackage(packageName);
    }
}

From source file:com.siteview.mde.internal.core.ClasspathComputer.java

License:Open Source License

private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet paths, ArrayList result)
        throws CoreException {
    String[] folders = buildEntry.getTokens();
    for (int j = 0; j < folders.length; j++) {
        String folder = folders[j];
        IPath path = project.getFullPath().append(folder);
        if (paths.add(path)) {
            if (project.findMember(folder) == null) {
                CoreUtility.createFolder(project.getFolder(folder));
            } else {
                IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(path.toString());
                if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
                    result.add(root.getRawClasspathEntry());
                    continue;
                }//from w w  w . ja v a2  s .  c  om
            }
            result.add(JavaCore.newSourceEntry(path));
        }
    }
}

From source file:com.siteview.mde.internal.core.ClasspathComputer.java

License:Open Source License

private static void addLibraryEntry(IProject project, IMonitorLibrary library, IPath sourceAttachment,
        IClasspathAttribute[] attrs, ArrayList result) throws JavaModelException {
    String name = ClasspathUtilCore.expandLibraryName(library.getName());
    IResource jarFile = project.findMember(name);
    if (jarFile == null)
        return;//from   w  w w .  jav  a  2  s .  c o  m

    IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile);
    if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
        IClasspathEntry oldEntry = root.getRawClasspathEntry();
        // If we have the same binary root but new or different source, we should recreate the entry 
        if ((sourceAttachment == null && oldEntry.getSourceAttachmentPath() != null)
                || (sourceAttachment != null && sourceAttachment.equals(oldEntry.getSourceAttachmentPath()))) {
            if (!result.contains(oldEntry)) {
                result.add(oldEntry);
                return;
            }
        }
    }

    IClasspathEntry entry = createClasspathEntry(project, jarFile, name, sourceAttachment, attrs,
            library.isExported());
    if (!result.contains(entry))
        result.add(entry);
}

From source file:com.siteview.mde.internal.core.util.ManifestUtils.java

License:Open Source License

public static IPackageFragmentRoot[] findPackageFragmentRoots(IManifestHeader header, IProject project) {
    IJavaProject javaProject = JavaCore.create(project);

    String[] libs;//from  w  w w .j av a 2s .c  om
    if (header == null || header.getValue() == null)
        libs = new String[] { "." }; //$NON-NLS-1$
    else
        libs = header.getValue().split(","); //$NON-NLS-1$

    IBuild build = getBuild(project);
    if (build == null) {
        try {
            return javaProject.getPackageFragmentRoots();
        } catch (JavaModelException e) {
            return new IPackageFragmentRoot[0];
        }
    }
    List pkgFragRoots = new LinkedList();
    for (int j = 0; j < libs.length; j++) {
        String lib = libs[j];
        //https://bugs.eclipse.org/bugs/show_bug.cgi?id=230469           
        IPackageFragmentRoot root = null;
        if (!lib.equals(".")) { //$NON-NLS-1$
            try {
                root = javaProject.getPackageFragmentRoot(project.getFile(lib));
            } catch (IllegalArgumentException e) {
                return new IPackageFragmentRoot[0];
            }
        }
        if (root != null && root.exists()) {
            pkgFragRoots.add(root);
        } else {
            IBuildEntry entry = build.getEntry("source." + lib); //$NON-NLS-1$
            if (entry == null)
                continue;
            String[] tokens = entry.getTokens();
            for (int i = 0; i < tokens.length; i++) {
                IResource resource = project.findMember(tokens[i]);
                if (resource == null)
                    continue;
                root = javaProject.getPackageFragmentRoot(resource);
                if (root != null && root.exists())
                    pkgFragRoots.add(root);
            }
        }
    }
    return (IPackageFragmentRoot[]) pkgFragRoots.toArray(new IPackageFragmentRoot[pkgFragRoots.size()]);
}