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

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

Introduction

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

Prototype

int getEntryKind();

Source Link

Document

Returns the kind of this classpath entry.

Usage

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

License:Open Source License

@Override
public File getDevJar() throws SdkException /* throws Exception */, JavaModelException {

    IStringVariableManager variableManager = getVariableManager();
    IValueVariable valueVariable = variableManager.getValueVariable("gwt_devjar");
    if (valueVariable != null) {
        String value = valueVariable.getValue();
        if (value != null) {
            IPath path = new Path(value);
            File file = path.toFile();
            if (!file.exists()) {
                throw new SdkException("gwt_devjar Run/Debug variable points to a non-existent jar: " + value);
            }//  w  ww.  j  a  v  a 2  s  . co m

            return file;
        }
    }

    // We're going to have to search down the trunk to find the built gwt-dev
    // .jar. This assumes that the user has built the trunk at least once

    // TODO: can we remove the check for gwt.devjar from applicationCreator?

    IProject userProject = ResourcesPlugin.getWorkspace().getRoot().getProject(GWT_USER_PROJECT_NAME);
    if (!userProject.exists()) {
        throw new SdkException("The project ' " + userProject.getName() + "' does not exist in the workspace.");
    }

    IJavaProject javaUserProject = JavaCore.create(userProject);

    if (!javaUserProject.exists()) {
        throw new SdkException("The project ' " + userProject.getName() + "' is not a Java project.");
    }

    IClasspathEntry[] rawClasspaths = javaUserProject.getRawClasspath();

    File stagingDir = null;
    IPath stagingPathLocation = null;

    for (IClasspathEntry rawClasspath : rawClasspaths) {
        if (rawClasspath.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

            IPath sourcePathLocation = getAbsoluteLocation(rawClasspath.getPath(), userProject);
            stagingPathLocation = sourcePathLocation.removeLastSegments(2)
                    .append(STAGING_FOLDER_RELATIVE_LOCATION);

            stagingDir = stagingPathLocation.toFile();
            if (stagingDir.exists()) {
                break;
            }
        }
    }

    if (stagingPathLocation == null) {
        throw new SdkException("Contributor SDK build directory not found; Project '" + userProject.getName()
                + "' does not have any source folders.");
    }

    if (stagingDir == null || !stagingDir.exists()) {
        throw new SdkException("Contributor SDK build directory not found (expected at "
                + stagingPathLocation.toString() + ")");
    }

    // Find the staging output directory: gwt-<platform>-<version>
    final File[] buildDirs = stagingDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            return (file.isDirectory() && file.getName().startsWith("gwt-"));
        }
    });
    if (buildDirs.length == 0) {
        throw new SdkException("Contributor SDK build directory not found (expected at " + stagingDir.toString()
                + File.separator + "gwt-<platform>-<version>)");
    }

    // Find the gwt-dev .jar
    File[] gwtDevJars = buildDirs[0].listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            String name = file.getName();
            IPath sdkLocationPath = Path.fromOSString(buildDirs[0].getAbsolutePath());
            return (name.equalsIgnoreCase(Util.getDevJarName(sdkLocationPath)));
        }
    });
    if (gwtDevJars.length == 0) {
        throw new SdkException("Contributor SDK build directory missing required JAR files");
    }

    return gwtDevJars[0];
}

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

License:Open Source License

/**
 * TODO: We need to revisit this test. Since we don't allow container updates
 * right now, it is not clear that the test is sufficiently strong.
 *//*from  w  ww .j av a  2s. c o m*/
public void testRequestClasspathContainerUpdate() throws CoreException {
    IJavaProject testProject = getTestProject();

    IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);

    final List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>();
    IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
    for (IClasspathEntry classpathEntry : classpathEntries) {

        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IClasspathAttribute[] extraAttributes = classpathEntry.getExtraAttributes();
            List<IClasspathAttribute> newAttributes = new ArrayList<IClasspathAttribute>();
            for (IClasspathAttribute extraAttribute : extraAttributes) {
                String attributeName = extraAttribute.getName();
                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attributeName)) {
                    String attributeValue = extraAttribute.getValue() + "modified";
                    extraAttribute = JavaCore.newClasspathAttribute(attributeName, attributeValue);
                }

                newAttributes.add(extraAttribute);
            }

            IPath sourceAttachmentPath = new Path("/sourceAttachmentPath");
            IPath sourceAttachmentRootPath = new Path("sourceAttachmentRootPath");

            classpathEntry = JavaCore.newLibraryEntry(classpathEntry.getPath(), sourceAttachmentPath,
                    sourceAttachmentRootPath, classpathEntry.getAccessRules(),
                    newAttributes.toArray(new IClasspathAttribute[0]), classpathEntry.isExported());
        }

        newClasspathEntries.add(classpathEntry);
    }

    // Update the classpath container
    initializer.requestClasspathContainerUpdate(defaultRuntimePath, testProject,
            new ClasspathContainerAdapter(classpathContainer) {
                @Override
                public IClasspathEntry[] getClasspathEntries() {
                    return newClasspathEntries.toArray(new IClasspathEntry[0]);
                }
            });

    // Check that the modifications took effect
    IClasspathContainer updatedContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);
    for (IClasspathEntry classpathEntry : updatedContainer.getClasspathEntries()) {
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
            // Ignore all non-library entries
            continue;
        }

        for (IClasspathAttribute attribute : classpathEntry.getExtraAttributes()) {
            if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attribute.getName())) {
                String value = attribute.getValue();
                assertTrue(value.endsWith("modified"));
            }
        }

        IPath sourceAttachmentPath = classpathEntry.getSourceAttachmentPath();
        assertEquals(new Path("/sourceAttachmentPath"), sourceAttachmentPath);

        IPath sourceAttachmentRootPath = classpathEntry.getSourceAttachmentRootPath();
        assertEquals(new Path("sourceAttachmentRootPath"), sourceAttachmentRootPath);
    }
}

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

License:Open Source License

/**
 * Given a list of IClasspathEntry, produce an array of IRuntimeClasspathEntry based on that list.
 *//*from   www. j a v  a 2s .  c  om*/
private IRuntimeClasspathEntry[] resolveClasspathEntries(List<IClasspathEntry> classpathEntries)
        throws CoreException {
    LinkedHashSet<IRuntimeClasspathEntry> runtimeClasspathEntries = new LinkedHashSet<IRuntimeClasspathEntry>();

    for (IClasspathEntry classpathEntry : classpathEntries) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String projectName = classpathEntry.getPath().lastSegment();
            IJavaProject theproject = JavaProjectUtilities.findJavaProject(projectName);

            IRuntimeClasspathEntry projectEntry = JavaRuntime.newProjectRuntimeClasspathEntry(theproject);
            runtimeClasspathEntries.add(projectEntry);
            runtimeClasspathEntries.addAll(dependenciesForProject(theproject));
        } else {
            runtimeClasspathEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(classpathEntry.getPath()));
        }
    }

    return runtimeClasspathEntries.toArray(NO_ENTRIES);
}

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

License:Open Source License

private static String computeClasspath(GWTRuntime gwtRuntime, String[] extraClassPath) throws CoreException {
    List<String> cpPaths = new ArrayList<String>();
    for (IClasspathEntry c : gwtRuntime.getClasspathEntries()) {
        if (c.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IJavaProject javaProject = JavaProjectUtilities.findJavaProject(c.getPath().toOSString());
            IRuntimeClasspathEntry projectRuntimeEntry = JavaRuntime
                    .newDefaultProjectClasspathEntry(javaProject);
            IRuntimeClasspathEntry[] resolvedEntries = JavaRuntime
                    .resolveRuntimeClasspathEntry(projectRuntimeEntry, javaProject);
            for (IRuntimeClasspathEntry resolvedEntry : resolvedEntries) {
                cpPaths.add(resolvedEntry.getLocation());
            }//w ww .  j av a  2s  . c  om
        } else {
            cpPaths.add(c.getPath().toFile().getAbsolutePath());
        }
    }
    if (extraClassPath != null) {
        cpPaths.addAll(Arrays.asList(extraClassPath));
    }
    return ProcessUtilities.buildClasspathString(cpPaths);
}

From source file:com.google.gwt.eclipse.core.validators.java.JsniJavaRef.java

License:Open Source License

public IJavaElement resolveJavaElement(IJavaProject project) throws UnresolvedJsniJavaRefException {
    IJavaElement element = null;/*w w  w.  ja v a  2 s . co  m*/

    // 0. Ignore the magic null reference
    if (className().equals("null")) {
        throw new UnresolvedJsniJavaRefException(null, this);
    }

    // 1. Try to find the type in the project's classpath
    IType type = JavaModelSearch.findType(project, dottedClassName());
    if (type == null) {
        throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_UNRESOLVED_TYPE, this);
    }

    // TODO: remove this check once we can validate against super source

    // 1A. Do not validate JRE types (they could contain references to members
    // which are only defined in the emulated versions in super source)
    if (type.isBinary()) {
        IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) type
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        try {
            IClasspathEntry cpEntry = pkgRoot.getRawClasspathEntry();
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && cpEntry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
                throw new UnresolvedJsniJavaRefException(null, this);
            }
        } catch (JavaModelException e) {
            GWTPluginLog.logError(e);
            throw new UnresolvedJsniJavaRefException(null, this);
        }
    }

    // 2. Create a super-type hierarchy for the type, which we'll use for
    // finding its super classes and implemented interfaces
    ITypeHierarchy hierarchy;
    try {
        hierarchy = type.newSupertypeHierarchy(null);
    } catch (JavaModelException e) {
        GWTPluginLog.logError(e, "Error creating type hierarchy for " + className());
        throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_UNRESOLVED_TYPE, this);
    }

    if (isMethod()) {
        if (isConstructor()) {
            if (matchesAnyOverload()) {
                element = JavaModelSearch.findFirstCtor(type);
            } else {
                String[] ctorParamTypes = paramTypes();
                try {
                    /*
                     * If the constructor is on a non-static inner class, the reference
                     * will have an extra parameter that specifies the enclosing type.
                     * We'll need to check that type and then remove the parameter
                     * before using JavaModelSearch to find the constructor.
                     */
                    IJavaElement typeParent = type.getParent();
                    if (typeParent.getElementType() == IJavaElement.TYPE && !Flags.isStatic(type.getFlags())) {

                        // Make sure we do have the enclosing type as the first parameter
                        if (ctorParamTypes.length == 0) {
                            throw new UnresolvedJsniJavaRefException(
                                    GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR, this);
                        }

                        // Now verify that the type of the first parameter is actually the
                        // the same type as the enclosing type
                        IType parentType = (IType) typeParent;
                        String enclosingTypeName = parentType.getFullyQualifiedName('.');
                        String enclosingTypeParam = JavaModelSearch.getQualifiedTypeName(ctorParamTypes[0]);
                        if (!enclosingTypeName.equals(enclosingTypeParam)) {
                            throw new UnresolvedJsniJavaRefException(
                                    GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR, this);
                        }

                        // Drop the first parameter (the enclosing type)
                        ctorParamTypes = new String[paramTypes().length - 1];
                        System.arraycopy(paramTypes(), 1, ctorParamTypes, 0, ctorParamTypes.length);
                    }
                } catch (JavaModelException e) {
                    GWTPluginLog.logError(e);
                    // Continue on and try to resolve the reference anyway
                }

                // 3A. Find a constructor for this type with matching parameter types
                element = JavaModelSearch.findCtorInHierarchy(hierarchy, type, ctorParamTypes);
                if (element == null) {
                    throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR,
                            this);
                }
            }
        } else {
            // 3B. Find a method for this type with the same name
            element = JavaModelSearch.findMethodInHierarchy(hierarchy, type, memberName());
            if (element == null) {
                try {
                    if (type.isEnum()) {
                        // Ignore the magic Enum::values() method
                        if (memberName().equals("values") && !matchesAnyOverload()
                                && paramTypes().length == 0) {
                            // Throwing this exception with a null GWTProblemType indicates
                            // that the ref doesn't resolve, but can be ignored anyway.
                            throw new UnresolvedJsniJavaRefException(null, this);
                        }
                    }
                } catch (JavaModelException e) {
                    GWTPluginLog.logError(e);
                }

                throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_MISSING_METHOD, this);
            }

            if (!matchesAnyOverload()) {
                // Now try to match the method's parameter types
                element = JavaModelSearch.findMethodInHierarchy(hierarchy, type, memberName(), paramTypes());
                if (element == null) {
                    try {
                        if (type.isEnum()) {
                            // Ignore the synthetic Enum::valueOf(String) method.
                            // Note that valueOf(Class,String) is not synthetic.
                            if (memberName().equals("valueOf") && paramTypes().length == 1
                                    && paramTypes()[0].equals("Ljava/lang/String;")) {
                                // Throwing this exception with a null GWTProblemType
                                // indicates that the ref doesn't resolve, but can be ignored
                                // anyway.
                                throw new UnresolvedJsniJavaRefException(null, this);
                            }
                        }
                    } catch (JavaModelException e) {
                        GWTPluginLog.logError(e);
                    }

                    throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_METHOD,
                            this);
                }
            }
        }

    } else {
        // 3C. Find a field with the same name
        assert (isField());
        element = JavaModelSearch.findFieldInHierarchy(hierarchy, type, memberName());
        if (element == null) {
            throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_MISSING_FIELD, this);
        }
    }

    assert (element != null);
    return element;
}

From source file:com.google.inject.tools.ideplugin.eclipse.EclipseJavaProject.java

License:Apache License

private List<String> expandClasspath(IClasspathEntry[] entries, String projectName, String projectLocation)
        throws Exception {
    final List<String> args = new ArrayList<String>();
    IResource presource;//from   w ww  . j  a  v  a 2 s .  c om
    String resourceLocation;
    String path;
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
            args.addAll(expandClasspath(container.getClasspathEntries(), projectName, projectLocation));
            break;
        case IClasspathEntry.CPE_SOURCE:
            IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            path = resource.getLocation().makeAbsolute().toOSString();
            if (path.startsWith("/" + projectName)) {
                args.add(path.replaceFirst("/" + projectName, projectLocation));
            } else {
                args.add(path);
            }
            break;
        case IClasspathEntry.CPE_LIBRARY:
            path = entry.getPath().makeAbsolute().toOSString();
            if (path.startsWith("/" + projectName)) {
                args.add(path.replaceFirst("/" + projectName, projectLocation));
            } else {
                args.add(path);
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            presource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            resourceLocation = presource.getLocation().makeAbsolute().toOSString();
            String outputLocation = resourceLocation;
            args.add(outputLocation.replaceFirst(presource.getName(), resourceLocation));
            break;
        case IClasspathEntry.CPE_VARIABLE:
            break;
        default:
            //never happens
        }
    }
    return args;
}

From source file:com.halware.nakedide.eclipse.core.util.JavaCoreUtils.java

License:Open Source License

public final static List<IClasspathEntry> getSourceFolders(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(false);
    List<IClasspathEntry> sourceFolders = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry classpathEntry : classpathEntries) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            sourceFolders.add(classpathEntry);
        }//from ww  w .  j  a  v  a  2 s. c o m
    }
    return sourceFolders;
}

From source file:com.ibm.research.tours.content.url.delegates.ClassFileTextRegionURLTourElementDelegate.java

License:Open Source License

private void init() {
    IPackageFragmentRoot root = null;//from   ww w .  j  a v a2s.com
    root = JavaModelUtil.getPackageFragmentRoot(fFile);

    IClasspathEntry entry = null;
    try {
        entry = root.getRawClasspathEntry();
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
        IPath path = entry.getPath().makeRelative();
    }
}

From source file:com.ibm.wala.ide.util.JavaEclipseProjectPath.java

License:Open Source License

@Override
protected void resolveClasspathEntry(IJavaProject project, IClasspathEntry entry, ILoader loader,
        boolean includeSource, boolean cpeFromMainProject) {
    entry = JavaCore.getResolvedClasspathEntry(entry);
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE: {
        resolveSourcePathEntry(includeSource ? JavaSourceLoader.SOURCE : Loader.APPLICATION, includeSource,
                cpeFromMainProject, entry.getPath(), entry.getOutputLocation(), entry.getExclusionPatterns(),
                "java");
        break;/*  w w w  . j  av a2s. c om*/
    }
    case IClasspathEntry.CPE_LIBRARY: {
        resolveLibraryPathEntry(loader, entry.getPath());
        break;
    }
    case IClasspathEntry.CPE_PROJECT: {
        resolveProjectPathEntry(loader, includeSource, entry.getPath());
        break;
    }
    case IClasspathEntry.CPE_CONTAINER: {
        try {
            IClasspathContainer cont = JavaCore.getClasspathContainer(entry.getPath(), project);
            IClasspathEntry[] entries = cont.getClasspathEntries();
            resolveClasspathEntries(project, Arrays.asList(entries),
                    cont.getKind() == IClasspathContainer.K_APPLICATION ? loader : Loader.PRIMORDIAL,
                    includeSource, false);
        } catch (CoreException e) {
            System.err.println(e);
            Assertions.UNREACHABLE();
        }
    }
    }
}

From source file:com.ifedorenko.m2e.sourcelookup.internal.JavaProjectSources.java

License:Open Source License

private void addJavaProject(IJavaProject project) throws CoreException {
    if (project != null) {
        final Map<File, IPackageFragmentRoot> classpath = new LinkedHashMap<File, IPackageFragmentRoot>();
        for (IPackageFragmentRoot fragment : project.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_BINARY
                    && fragment.getSourceAttachmentPath() != null) {
                File classpathLocation;
                if (fragment.isExternal()) {
                    classpathLocation = fragment.getPath().toFile();
                } else {
                    classpathLocation = toFile(fragment.getPath());
                }//from   www  .j  a va  2s .  c  o m
                if (classpathLocation != null) {
                    classpath.put(classpathLocation, fragment);
                }
            }
        }

        final JavaProjectInfo projectInfo = new JavaProjectInfo(project, classpath);

        final Set<File> projectLocations = new HashSet<File>();

        final String jarLocation = project.getProject().getPersistentProperty(BinaryProjectPlugin.QNAME_JAR);
        if (jarLocation != null) {
            // maven binary project
            projectLocations.add(new File(jarLocation));
        } else {
            // regular project
            projectLocations.add(toFile(project.getOutputLocation()));
            for (IClasspathEntry cpe : project.getRawClasspath()) {
                if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    projectLocations.add(toFile(cpe.getOutputLocation()));
                }
            }
        }

        synchronized (lock) {
            projects.put(project, projectLocations);
            for (File projectLocation : projectLocations) {
                locations.put(projectLocation, projectInfo);
            }
        }
    }
}