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

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

Introduction

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

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java

License:Apache License

public static boolean isProjectModule(IJavaProject javaProject, Module module) throws JavaModelException {
    boolean isSource = false;
    for (IPackageFragmentRoot s : javaProject.getPackageFragmentRoots()) {
        if (s.exists() && javaProject.isOnClasspath(s) && s.getKind() == IPackageFragmentRoot.K_SOURCE
                && s.getPackageFragment(module.getNameAsString()).exists()) {
            isSource = true;/*from ww w  .  ja va2s .  co m*/
            break;
        }
    }
    return isSource;
}

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;
    }/*from   w  ww  .  j a  v a2s.  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.JDTModelLoader.java

License:Open Source License

@Override
public boolean searchAgain(Module module, String name) {
    if (module instanceof JDTModule) {
        JDTModule jdtModule = (JDTModule) module;
        if (jdtModule.isCeylonBinaryArchive() || jdtModule.isJavaBinaryArchive()) {
            String classRelativePath = name.replace('.', '/');
            return jdtModule.containsClass(classRelativePath + ".class")
                    || jdtModule.containsClass(classRelativePath + "_.class");
        } else if (jdtModule.isProjectModule()) {
            int nameLength = name.length();
            int packageEnd = name.lastIndexOf('.');
            int classNameStart = packageEnd + 1;
            String packageName = packageEnd > 0 ? name.substring(0, packageEnd) : "";
            String className = classNameStart < nameLength ? name.substring(classNameStart) : "";
            boolean moduleContainsJava = false;
            for (IPackageFragmentRoot root : jdtModule.getPackageFragmentRoots()) {
                try {
                    IPackageFragment pf = root.getPackageFragment(packageName);
                    if (pf.exists() && javaProject.isOnClasspath(pf)) {
                        if (((IPackageFragment) pf).containsJavaResources()) {
                            moduleContainsJava = true;
                            break;
                        }/*from   w  w  w  . j  a v  a2s  .  co  m*/
                    }
                } catch (JavaModelException e) {
                    e.printStackTrace();
                    moduleContainsJava = true; // Just in case ...
                }
            }
            if (moduleContainsJava) {
                ModelLoaderNameEnvironment nameEnvironment = getNameEnvironment();
                if (nameEnvironment.findTypeInNameLookup(className, packageName) != null
                        || nameEnvironment.findTypeInNameLookup(className + "_", packageName) != null) {
                    return true;
                }
            }
            return false;
        }
    }
    return false;
}

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

License:Open Source License

private boolean moduleFileInProject(String moduleName, IJavaProject p) {
    try {//from w w  w.  jav  a2 s  .co m
        for (IPackageFragmentRoot sourceFolder : p.getPackageFragmentRoots()) {
            if (sourceFolder.getKind() == IPackageFragmentRoot.K_SOURCE && !sourceFolder.isArchive()
                    && 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.loader.JDTModuleManager.java

License:Open Source License

@Override
protected Module createModule(List<String> moduleName, String version) {
    JDTModule module = null;//from   w  w w.  j a v  a 2s  . c  o  m
    String moduleNameString = Util.getName(moduleName);
    List<IPackageFragmentRoot> roots = new ArrayList<IPackageFragmentRoot>();
    try {
        if (moduleNameString.equals(Module.DEFAULT_MODULE_NAME)) {
            // Add the list of source package fragment roots
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                IClasspathEntry entry = root.getResolvedClasspathEntry();
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && !root.isExternal()) {
                    roots.add(root);
                }
            }
        } else {
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                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)) {
                    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.ui.test.headless.ModelAndPhasedUnitsTests.java

License:Open Source License

@Test
public void checkJavaLibrayrUnits() throws CoreException {
    if (compilationError != null) {
        throw compilationError;
    }//from   w  w w. j  a va  2 s  .  c o  m

    IJavaProject javaProject = JavaCore.create(mainProject);

    String jarName = null;
    IClassFile javaElement = null;
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
            IPackageFragment pkg = root.getPackageFragment("java.util.logging");
            if (pkg.exists()) {
                javaElement = pkg.getClassFile("Logger.class");
                jarName = jarRoot.getJar().getName();
                break;
            }
        }
    }

    Module module = modelLoader.findModule(AbstractModelLoader.JAVA_BASE_MODULE_NAME, JDKUtils.jdk.version);

    JavaClassFile javaClass = checkDeclarationUnit(module, "java.util.logging.Logger", JavaClassFile.class,
            jarName + "!/" + "java/util/logging/Logger.class", "java/util/logging/Logger.class",
            "Logger.class");
    Assert.assertEquals("Wrong Java Element : ", javaElement, javaClass.getTypeRoot());
    Assert.assertNull("Project Resource should be null :", javaClass.getProjectResource());
    Assert.assertNull("Root Folder Resource should be null :", javaClass.getRootFolderResource());
    Assert.assertNull("File Resource should be null :", javaClass.getFileResource());
}

From source file:com.redhat.ceylon.eclipse.ui.test.headless.ModelAndPhasedUnitsTests.java

License:Open Source License

@Test
public void checkMainProjectJavaCeylonUnits() throws CoreException {
    if (compilationError != null) {
        throw compilationError;
    }//from  w  ww.ja v a2 s. co  m

    IJavaProject javaProject = JavaCore.create(mainProject);

    String rootPath = null;
    ICompilationUnit javaClassElement = null;
    ICompilationUnit javaObjectElement = null;
    ICompilationUnit javaMethodElement = null;
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        IPackageFragment pkg = root.getPackageFragment("mainModule");
        if (pkg.exists()
                && pkg.getCompilationUnit("JavaCeylonTopLevelClass_Main_Ceylon_Project.java").exists()) {
            javaClassElement = pkg.getCompilationUnit("JavaCeylonTopLevelClass_Main_Ceylon_Project.java");
            javaObjectElement = pkg.getCompilationUnit("javaCeylonTopLevelObject_Main_Ceylon_Project_.java");
            javaMethodElement = pkg.getCompilationUnit("javaCeylonTopLevelMethod_Main_Ceylon_Project_.java");
            rootPath = root.getPath().toOSString();
            break;
        }
    }

    Module module = modelLoader.findModule("mainModule", "1.0.0");
    JavaCompilationUnit javaClassCompilationUnit = checkDeclarationUnit(module,
            "mainModule.JavaCeylonTopLevelClass_Main_Ceylon_Project", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/JavaCeylonTopLevelClass_Main_Ceylon_Project.java",
            "mainModule/JavaCeylonTopLevelClass_Main_Ceylon_Project.java",
            "JavaCeylonTopLevelClass_Main_Ceylon_Project.java");
    Assert.assertEquals("Wrong Java Element for Class : ", javaClassElement,
            javaClassCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Class should not be null :",
            javaClassCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Class should not be null :",
            javaClassCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Class not be null :",
            javaClassCompilationUnit.getFileResource());

    JavaCompilationUnit javaObjectCompilationUnit = checkDeclarationUnit(module,
            "mainModule.javaCeylonTopLevelObject_Main_Ceylon_Project", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/javaCeylonTopLevelObject_Main_Ceylon_Project_.java",
            "mainModule/javaCeylonTopLevelObject_Main_Ceylon_Project_.java",
            "javaCeylonTopLevelObject_Main_Ceylon_Project_.java");
    Assert.assertEquals("Wrong Java Element for Object : ", javaObjectElement,
            javaObjectCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Object should not be null :",
            javaObjectCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Object should not be null :",
            javaObjectCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Object not be null :",
            javaObjectCompilationUnit.getFileResource());

    JavaCompilationUnit javaMethodCompilationUnit = checkDeclarationUnit(module,
            "mainModule.javaCeylonTopLevelMethod_Main_Ceylon_Project_", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/javaCeylonTopLevelMethod_Main_Ceylon_Project_.java",
            "mainModule/javaCeylonTopLevelMethod_Main_Ceylon_Project_.java",
            "javaCeylonTopLevelMethod_Main_Ceylon_Project_.java");
    Assert.assertEquals("Wrong Java Element for Method : ", javaMethodElement,
            javaMethodCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Method should not be null :",
            javaMethodCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Method should not be null :",
            javaMethodCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Method not be null :",
            javaMethodCompilationUnit.getFileResource());
}