Example usage for org.eclipse.jdt.core IClasspathEntry getContentKind

List of usage examples for org.eclipse.jdt.core IClasspathEntry getContentKind

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getContentKind.

Prototype

int getContentKind();

Source Link

Document

Returns the kind of files found in the package fragments identified by this classpath entry.

Usage

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ProjectSetupBuilder.java

License:Open Source License

@NonNull
private static List<IJavaProject> getReferencedProjects(IJavaProject javaProject)
        throws JavaModelException, InternalException {

    List<IJavaProject> projects = Lists.newArrayList();

    IClasspathEntry entries[] = javaProject.getRawClasspath();
    for (IClasspathEntry classpathEntry : entries) {
        if (classpathEntry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                && classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // found required project on build path
            String subProjectRoot = classpathEntry.getPath().toString();
            IJavaProject subProject = getJavaProject(subProjectRoot);
            // is project available in workspace?
            if (subProject != null) {
                projects.add(subProject);
            } else {
                throw new InternalException(String.format(
                        "Project '%s' is missing project dependency '%s' in Eclipse workspace.\n"
                                + "Make sure all dependencies are opened.",
                        javaProject.getProject().getName(), classpathEntry.getPath().toString()));
            }//www . j  av a2 s  .  c o  m
        }
    }

    return projects;
}

From source file:com.appnativa.studio.builder.J2ObjCHelper.java

static String getSourcePath(IProject p, StringBuilder sb) {
    try {// w ww  .  j a v a  2s.c o  m
        sb.setLength(0);

        IJavaProject javaProject = JavaCore.create(p);
        IClasspathEntry[] classpathEntries = null;

        classpathEntries = javaProject.getResolvedClasspath(true);

        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

        for (int i = 0; i < classpathEntries.length; i++) {
            IClasspathEntry entry = classpathEntries[i];

            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                IResource res = root.findMember(path);

                if (res != null) {
                    String srcPath = res.getLocation().toOSString();

                    sb.append(srcPath).append(File.pathSeparator);
                }
            }
        }

        if (sb.charAt(sb.length() - 1) == File.pathSeparatorChar) {
            sb.setLength(sb.length() - 1);

            return sb.toString();
        }
    } catch (Exception ignore) {
        ignore.printStackTrace();
    }

    return "";
}

From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java

License:Open Source License

public void testGetClasspathEntries() throws Exception {
    IClasspathEntry[] cpEntries = runtime.getClasspathEntries();

    // Look for the gwt-specific classpath entries
    List<IClasspathEntry> gwtCpEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry cpEntry : cpEntries) {
        if (isGWTJar(runtime, cpEntry.getPath().lastSegment())) {
            gwtCpEntries.add(cpEntry);//  w w w.ja  v a  2s  .co m
        }
    }

    // Make sure that there are two of them
    assertEquals(3, gwtCpEntries.size());

    for (int i = 0; i < gwtCpEntries.size(); i++) {
        IClasspathEntry gwtClasspathEntry = gwtCpEntries.get(i);
        assertEquals(IClasspathEntry.CPE_LIBRARY, gwtClasspathEntry.getEntryKind());
        assertEquals(IPackageFragmentRoot.K_BINARY, gwtClasspathEntry.getContentKind());

        // Verify that our classpath entries point at the GWT javadoc.
        IClasspathAttribute[] extraAttributes = gwtClasspathEntry.getExtraAttributes();
        assertTrue("No extra attributes seen for classpath entry: " + gwtClasspathEntry,
                extraAttributes.length > 0);
        assertEquals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, extraAttributes[0].getName());

        /*
         * Entries should have their javadoc location point at a directory with "index.html".
         * Strangely, the values of these classpath attributes are specified as "file://" urls.
         */
        File jdLocation = new File(new URL(extraAttributes[0].getValue()).getFile());
        assertTrue("Javadoc file does not exist", jdLocation.exists());
        List<String> files1 = Arrays.asList(jdLocation.list());
        assertTrue("Javadoc file is not an index.html file.", files1.contains("index.html"));
    }
}

From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java

License:Open Source License

public void testGetValidationClasspathEntries() throws Exception {
    IClasspathEntry[] cpEntries = runtime.getClasspathEntries();

    // Look for the validation-specific classpath entries
    List<IClasspathEntry> validationCpEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry cpEntry : cpEntries) {
        if (cpEntry.getPath().lastSegment().startsWith(GWTRuntime.VALIDATION_API_JAR_PREFIX)) {
            validationCpEntries.add(cpEntry);
        }/*  w  ww  .  j  av a 2s.c o  m*/
    }

    if (validationCpEntries.size() == 0) {
        String sdkVersion = runtime.getVersion();

        // Can't be an internal version, because it would have the
        // validation jars
        assertFalse(SdkUtils.isInternal(sdkVersion));

        // Sdk version must be pre-GWT 2.3.0
        assertTrue(SdkUtils.compareVersionStrings(sdkVersion, "2.3.0") < 0);

        return;
    }

    // Make sure that there are at least two of them
    assertEquals(2, validationCpEntries.size());

    IClasspathEntry sourcesEntry = null;
    IClasspathEntry binaryEntry = null;

    for (IClasspathEntry validationClasspathEntry : validationCpEntries) {
        // Verify the entry types
        assertEquals(IClasspathEntry.CPE_LIBRARY, validationClasspathEntry.getEntryKind());
        assertEquals(IPackageFragmentRoot.K_BINARY, validationClasspathEntry.getContentKind());

        if (validationClasspathEntry.getPath().lastSegment().contains("sources")) {
            sourcesEntry = validationClasspathEntry;
        } else {
            binaryEntry = validationClasspathEntry;
        }
    }

    // Verify that the sources and binary entries correspond to each other
    assertTrue(Util.findSourcesJarForClassesJar(binaryEntry.getPath()).equals(sourcesEntry.getPath()));

    // Verify that the source attachment path has been set for the binary
    // entry
    assertTrue(binaryEntry.getSourceAttachmentPath().equals(sourcesEntry.getPath()));
}

From source file:com.jaspersoft.studio.backward.ConsoleExecuter.java

License:Open Source License

/**
 * Get all the resolved classpath entries for a specific project. The entries 
 * with ID JRClasspathContainer.ID and JavaRuntime.JRE_CONTAINER are not resolved
 * or included in the result. At also add the source and output folder provided with the 
 * project/* ww  w.  j a va2s  .c o  m*/
 * 
 * @param project the project where the file to compile is contained, must be not null
 * @return a not null list of string that contains the classpath to include in the compilation project
 */
private List<String> getClasspaths(IProject project) {
    IJavaProject jprj = JavaCore.create(project);
    List<String> classpath = new ArrayList<String>();
    IWorkspaceRoot wsRoot = project.getWorkspace().getRoot();
    if (jprj != null) {
        try {
            IClasspathEntry[] entries = jprj.getRawClasspath();

            //Add the default output folder if any
            IPath defaultLocationPath = jprj.getOutputLocation();
            if (defaultLocationPath != null) {
                IFolder entryOutputFolder = wsRoot.getFolder(defaultLocationPath);
                classpath.add(entryOutputFolder.getLocation().toOSString() + File.separator);
            }

            for (IClasspathEntry en : entries) {
                if (en.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    String containerPath = en.getPath().toString();
                    //Don't add the eclipse runtime and the classpath extension defined in the exclusion list
                    if (!containerPath.startsWith(JavaRuntime.JRE_CONTAINER)
                            && !classpathExclusionSet.contains(containerPath)) {
                        addEntries(JavaCore.getClasspathContainer(en.getPath(), jprj).getClasspathEntries(),
                                classpath, jprj);
                    }
                } else if (en.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    classpath.add(wsRoot.findMember(en.getPath()).getLocation().toOSString() + File.separator);
                } else if (en.getEntryKind() == IClasspathEntry.CPE_SOURCE
                        && en.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    //check if is a source folder and if it has a custom output folder to add them also to the classpath
                    IPath entryOutputLocation = en.getOutputLocation();
                    if (entryOutputLocation != null) {
                        IFolder entryOutputFolder = wsRoot.getFolder(entryOutputLocation);
                        classpath.add(entryOutputFolder.getLocation().toOSString() + File.separator);
                    }
                } else {
                    //It is a jar check if it is internal to the workspace of external
                    IPath location = wsRoot.getFile(en.getPath()).getLocation();
                    if (location == null) {
                        //The location could not be resolved from the root of the workspace, it is external
                        classpath.add(en.getPath().toOSString());
                    } else {
                        //The location has been resolved from the root of the workspace, it is internal
                        classpath.add(location.toOSString());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return classpath;
}

From source file:com.javapathfinder.vjp.DefaultProperties.java

License:Open Source License

/**
 * append all relevant paths from the target project settings to the vm.classpath 
 *//*  w ww.j a v  a  2s .co  m*/
private static void appendProjectClassPaths(IJavaProject project, StringBuilder cp) {
    try {
        // we need to maintain order
        LinkedHashSet<IPath> paths = new LinkedHashSet<IPath>();

        // append the default output folder
        IPath defOutputFolder = project.getOutputLocation();
        if (defOutputFolder != null) {
            paths.add(defOutputFolder);
        }
        // look for libraries and source root specific output folders
        for (IClasspathEntry e : project.getResolvedClasspath(true)) {
            IPath ePath = null;

            switch (e.getContentKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                ePath = e.getPath();
                break;
            case IClasspathEntry.CPE_SOURCE:
                ePath = e.getOutputLocation();
                break;
            }

            if (ePath != null && !paths.contains(ePath)) {

                paths.add(ePath);
            }
        }

        for (IPath path : paths) {
            String absPath = getAbsolutePath(project, path).toOSString();

            //        if (cp.length() > 0) {
            //          cp.append(Config.LIST_SEPARATOR);
            //        }
            // only add classes that are found in bin
            if (absPath.contains("/bin"))
                cp.append(absPath);
        }
        System.out.println("cp is" + cp);
    } catch (JavaModelException jme) {
        VJP.logError("Could not append Project classpath", jme);
    }
}

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  w w.  j a  v  a2  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.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  w w  w  . j ava2  s. co  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.siteview.mde.internal.ui.properties.SelfHostingPropertyPage.java

License:Open Source License

private String[] getOutputFolders() {
    IProject project = (IProject) getElement().getAdapter(IProject.class);
    ArrayList list = new ArrayList();
    try {/*from  ww  w. jav a2  s . co m*/
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject jProject = JavaCore.create(project);
            list.add(jProject.getOutputLocation().toString());
            IClasspathEntry[] entries = jProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                        && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPath path = entry.getOutputLocation();
                    if (path != null)
                        list.add(path.toString());
                }
            }
        }
    } catch (JavaModelException e) {
    } catch (CoreException e) {
    }
    return (String[]) list.toArray(new String[list.size()]);
}

From source file:de.lynorics.eclipse.jangaroo.ui.wizard.NewAS3FileWizardPage.java

License:Open Source License

public static List<IPath> getProjectSourceDirectories(IProject project) {
    List<IPath> paths = new ArrayList<IPath>();
    try {/*from   w w  w . j  a  va 2 s  . c  o m*/
        if (project.isOpen() && project.getNature(JangarooProjectHelper.NATURE_ID) != null) {
            IJavaProject javaProject = JavaCore.create(project);
            IClasspathEntry[] classpathEntries = null;
            classpathEntries = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < classpathEntries.length; i++) {
                IClasspathEntry entry = classpathEntries[i];
                if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    paths.add(entry.getPath());
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return paths;
}