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

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

Introduction

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

Prototype

int CPE_VARIABLE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_VARIABLE.

Click Source Link

Document

Entry kind constant describing a classpath entry defined using a path that begins with a classpath variable reference.

Usage

From source file:com.android.ide.eclipse.editors.resources.manager.ProjectClassLoader.java

License:Open Source License

/**
 * Returns an array of external jar files used by the project.
 * @return an array of OS-specific absolute file paths
 *//*  ww w .ja  va  2s.  c  o m*/
private final URL[] getExternalJars() {
    // get a java project from it
    IJavaProject javaProject = JavaCore.create(mJavaProject.getProject());

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

    ArrayList<URL> oslibraryList = new ArrayList<URL>();
    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    || e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                // if this is a classpath variable reference, we resolve it.
                if (e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                    e = JavaCore.getResolvedClasspathEntry(e);
                }

                // get the IPath
                IPath path = e.getPath();

                // check the name ends with .jar
                if (AndroidConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) {
                    boolean local = false;
                    IResource resource = wsRoot.findMember(path);
                    if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
                        local = true;
                        try {
                            oslibraryList.add(new File(resource.getLocation().toOSString()).toURL());
                        } catch (MalformedURLException mue) {
                            // pass
                        }
                    }

                    if (local == false) {
                        // if the jar path doesn't match a workspace resource,
                        // then we get an OSString and check if this links to a valid file.
                        String osFullPath = path.toOSString();

                        File f = new File(osFullPath);
                        if (f.exists()) {
                            try {
                                oslibraryList.add(f.toURL());
                            } catch (MalformedURLException mue) {
                                // pass
                            }
                        }
                    }
                }
            }
        }
    }

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

From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java

License:Open Source License

/**
 * Returns the kind of a <code>PackageFragmentRoot</code> from its <code>String</code> form.
 *///from ww  w  .j ava  2s. c  om
static int kindFromString(String kindStr) {

    if (kindStr.equalsIgnoreCase("prj")) //$NON-NLS-1$
        return IClasspathEntry.CPE_PROJECT;
    if (kindStr.equalsIgnoreCase("var")) //$NON-NLS-1$
        return IClasspathEntry.CPE_VARIABLE;
    if (kindStr.equalsIgnoreCase("con")) //$NON-NLS-1$
        return IClasspathEntry.CPE_CONTAINER;
    if (kindStr.equalsIgnoreCase("src")) //$NON-NLS-1$
        return IClasspathEntry.CPE_SOURCE;
    if (kindStr.equalsIgnoreCase("lib")) //$NON-NLS-1$
        return IClasspathEntry.CPE_LIBRARY;
    if (kindStr.equalsIgnoreCase("output")) //$NON-NLS-1$
        return ClasspathEntry.K_OUTPUT;
    return -1;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java

License:Open Source License

/**
 * Returns a <code>String</code> for the kind of a class path entry.
 *///  www . j  a  v a2  s.c o m
static String kindToString(int kind) {

    switch (kind) {
    case IClasspathEntry.CPE_PROJECT:
        return "src"; // backward compatibility //$NON-NLS-1$
    case IClasspathEntry.CPE_SOURCE:
        return "src"; //$NON-NLS-1$
    case IClasspathEntry.CPE_LIBRARY:
        return "lib"; //$NON-NLS-1$
    case IClasspathEntry.CPE_VARIABLE:
        return "var"; //$NON-NLS-1$
    case IClasspathEntry.CPE_CONTAINER:
        return "con"; //$NON-NLS-1$
    case ClasspathEntry.K_OUTPUT:
        return "output"; //$NON-NLS-1$
    default:
        return "unknown"; //$NON-NLS-1$
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java

License:Open Source License

/**
 * Returns a printable representation of this classpath entry.
 *//* www.  j  a v  a 2  s . co  m*/
public String toString() {
    StringBuffer buffer = new StringBuffer();
    Object target = JavaModel.getTarget(getPath(), true);
    if (target instanceof File)
        buffer.append(getPath().toOSString());
    else
        buffer.append(String.valueOf(getPath()));
    buffer.append('[');
    switch (getEntryKind()) {
    case IClasspathEntry.CPE_LIBRARY:
        buffer.append("CPE_LIBRARY"); //$NON-NLS-1$
        break;
    case IClasspathEntry.CPE_PROJECT:
        buffer.append("CPE_PROJECT"); //$NON-NLS-1$
        break;
    case IClasspathEntry.CPE_SOURCE:
        buffer.append("CPE_SOURCE"); //$NON-NLS-1$
        break;
    case IClasspathEntry.CPE_VARIABLE:
        buffer.append("CPE_VARIABLE"); //$NON-NLS-1$
        break;
    case IClasspathEntry.CPE_CONTAINER:
        buffer.append("CPE_CONTAINER"); //$NON-NLS-1$
        break;
    }
    buffer.append("]["); //$NON-NLS-1$
    switch (getContentKind()) {
    case IPackageFragmentRoot.K_BINARY:
        buffer.append("K_BINARY"); //$NON-NLS-1$
        break;
    case IPackageFragmentRoot.K_SOURCE:
        buffer.append("K_SOURCE"); //$NON-NLS-1$
        break;
    case ClasspathEntry.K_OUTPUT:
        buffer.append("K_OUTPUT"); //$NON-NLS-1$
        break;
    }
    buffer.append(']');
    if (getSourceAttachmentPath() != null) {
        buffer.append("[sourcePath:"); //$NON-NLS-1$
        buffer.append(getSourceAttachmentPath());
        buffer.append(']');
    }
    if (getSourceAttachmentRootPath() != null) {
        buffer.append("[rootPath:"); //$NON-NLS-1$
        buffer.append(getSourceAttachmentRootPath());
        buffer.append(']');
    }
    buffer.append("[isExported:"); //$NON-NLS-1$
    buffer.append(this.isExported);
    buffer.append(']');
    IPath[] patterns = this.inclusionPatterns;
    int length;
    if ((length = patterns == null ? 0 : patterns.length) > 0) {
        buffer.append("[including:"); //$NON-NLS-1$
        for (int i = 0; i < length; i++) {
            buffer.append(patterns[i]);
            if (i != length - 1) {
                buffer.append('|');
            }
        }
        buffer.append(']');
    }
    patterns = this.exclusionPatterns;
    if ((length = patterns == null ? 0 : patterns.length) > 0) {
        buffer.append("[excluding:"); //$NON-NLS-1$
        for (int i = 0; i < length; i++) {
            buffer.append(patterns[i]);
            if (i != length - 1) {
                buffer.append('|');
            }
        }
        buffer.append(']');
    }
    if (this.accessRuleSet != null) {
        buffer.append('[');
        buffer.append(this.accessRuleSet.toString(false/*on one line*/));
        buffer.append(']');
    }
    if (this.entryKind == CPE_PROJECT) {
        buffer.append("[combine access rules:"); //$NON-NLS-1$
        buffer.append(this.combineAccessRules);
        buffer.append(']');
    }
    if (getOutputLocation() != null) {
        buffer.append("[output:"); //$NON-NLS-1$
        buffer.append(getOutputLocation());
        buffer.append(']');
    }
    if ((length = this.extraAttributes == null ? 0 : this.extraAttributes.length) > 0) {
        buffer.append("[attributes:"); //$NON-NLS-1$
        for (int i = 0; i < length; i++) {
            buffer.append(this.extraAttributes[i]);
            if (i != length - 1) {
                buffer.append(',');
            }
        }
        buffer.append(']');
    }
    return buffer.toString();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java

License:Open Source License

/**
 * Answers an ID which is used to distinguish entries during package
 * fragment root computations/*ww  w. j  a v a2 s.  c o  m*/
 */
public String rootID() {

    if (this.rootID == null) {
        switch (this.entryKind) {
        case IClasspathEntry.CPE_LIBRARY:
            this.rootID = "[LIB]" + this.path; //$NON-NLS-1$
            break;
        case IClasspathEntry.CPE_PROJECT:
            this.rootID = "[PRJ]" + this.path; //$NON-NLS-1$
            break;
        case IClasspathEntry.CPE_SOURCE:
            this.rootID = "[SRC]" + this.path; //$NON-NLS-1$
            break;
        case IClasspathEntry.CPE_VARIABLE:
            this.rootID = "[VAR]" + this.path; //$NON-NLS-1$
            break;
        case IClasspathEntry.CPE_CONTAINER:
            this.rootID = "[CON]" + this.path; //$NON-NLS-1$
            break;
        default:
            this.rootID = ""; //$NON-NLS-1$
            break;
        }
    }
    return this.rootID;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClasspathEntry.java

License:Open Source License

/**
 * This function computes the URL of the index location for this classpath entry. It returns null if the URL is
 * invalid./*from w  w  w . jav a  2s  .co m*/
 */
public URL getLibraryIndexLocation() {
    switch (getEntryKind()) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
        break;
    default:
        return null;
    }
    if (this.extraAttributes == null)
        return null;
    for (int i = 0; i < this.extraAttributes.length; i++) {
        IClasspathAttribute attrib = this.extraAttributes[i];
        if (IClasspathAttribute.INDEX_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
            String value = attrib.getValue();
            try {
                return new URL(value);
            } catch (MalformedURLException e) {
                return null;
            }
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaElement.java

License:Open Source License

protected static URL getLibraryJavadocLocation(IClasspathEntry entry) throws JavaModelException {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
        break;//from  w w  w.  j  ava2 s. c  om
    default:
        throw new IllegalArgumentException("Entry must be of kind CPE_LIBRARY or CPE_VARIABLE"); //$NON-NLS-1$
    }

    IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
    for (int i = 0; i < extraAttributes.length; i++) {
        IClasspathAttribute attrib = extraAttributes[i];
        if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
            String value = attrib.getValue();
            try {
                return new URL(value);
            } catch (MalformedURLException e) {
                throw new JavaModelException(
                        new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, value));
            }
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaElement.java

License:Open Source License

protected URL getJavadocBaseLocation() throws JavaModelException {
    IPackageFragmentRoot root = (IPackageFragmentRoot) getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (root == null) {
        return null;
    }/*from  w ww .  j a  v a  2  s. c om*/

    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
        IClasspathEntry entry = null;
        try {
            entry = root.getResolvedClasspathEntry();
            URL url = getLibraryJavadocLocation(entry);
            if (url != null) {
                return url;
            }
        } catch (JavaModelException jme) {
            // Proceed with raw classpath
        }

        entry = root.getRawClasspathEntry();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
            return getLibraryJavadocLocation(entry);
        default:
            return null;
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.JavaSearchScope.java

License:Open Source License

/**
 * Add a path to current java search scope or all project fragment roots if null.
 * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
 * Recurse if dependent projects are found.
 *
 * @param javaProject//from   w  w w  .j a va 2  s . c o m
 *         Project used to get resolved classpath entries
 * @param pathToAdd
 *         Path to add in case of single element or null if user want to add all project package fragment roots
 * @param includeMask
 *         Mask to apply on classpath entries
 * @param projectsToBeAdded
 *         Set to avoid infinite recursion
 * @param visitedProjects
 *         Set to avoid adding twice the same project
 * @param referringEntry
 *         Project raw entry in referring project classpath
 * @throws org.eclipse.jdt.core.JavaModelException
 *         May happen while getting java model info
 */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded,
        HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    //        IProject project = javaProject.getProject();
    //        if (!project.isAccessible() || !visitedProjects.add(project)) return;

    IPath projectPath = javaProject.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);

    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    //        IJavaModel model = javaProject.getJavaModel();
    //        JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Add only exported entries.
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
            //            cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            IClasspathEntry rawEntry = null;
            //                    Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
            //                    if (rootPathToRawEntries != null) {
            //                        rawEntry = (IClasspathEntry)rootPathToRawEntries.get(entry.getPath());
            //                    }
            //                    if (rawEntry == null) break;
            rawKind: switch (cpEntry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_VARIABLE:
                if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        //                                    Object target = JavaModel.getTarget(path, false/*don't check existence*/);
                        //                                    if (target instanceof IFolder) // case of an external folder
                        //                                        path = ((IFolder)target).getFullPath();
                        String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                        add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                        addEnclosingProjectOrJar(entry.getPath());
                    }
                }
                break;
            case IClasspathEntry.CPE_CONTAINER:
                IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                if (container == null)
                    break;
                switch (container.getKind()) {
                case IClasspathContainer.K_APPLICATION:
                    if ((includeMask & APPLICATION_LIBRARIES) == 0)
                        break rawKind;
                    break;
                case IClasspathContainer.K_SYSTEM:
                case IClasspathContainer.K_DEFAULT_SYSTEM:
                    if ((includeMask & SYSTEM_LIBRARIES) == 0)
                        break rawKind;
                    break;
                default:
                    break rawKind;
                }
                IPath path = entry.getPath();
                if (pathToAdd == null || pathToAdd.equals(path)) {
                    Object target = JavaModel.getTarget(path, false/*don't check existence*/);
                    if (target instanceof IFolder) // case of an external folder
                        path = ((IFolder) target).getFullPath();
                    String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                    add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                    addEnclosingProjectOrJar(entry.getPath());
                }
                break;
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            //                    if ((includeMask & REFERENCED_PROJECTS) != 0) {
            //                        IPath path = entry.getPath();
            //                        if (pathToAdd == null || pathToAdd.equals(path)) {
            //                            JavaProject referencedProject = (JavaProject)model.getJavaProject(path.lastSegment());
            //                            if (!projectsToBeAdded
            //                                    .contains(referencedProject)) { // do not recurse if depending project was used to create the scope
            //                                add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
            //                            }
            //                        }
            //                    }
            break;
        case IClasspathEntry.CPE_SOURCE:
            if ((includeMask & SOURCES) != 0) {
                IPath path = entry.getPath();
                if (pathToAdd == null || pathToAdd.equals(path)) {
                    add(projectPath.toString(), Util.relativePath(path, 1/*remove project segment*/),
                            projectPathString, false/*not a package*/, access);
                }
            }
            break;
        }
    }
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Get the class path from Java project.
 *
 * @param javaProject//from  www . j av a  2  s. c om
 * @param getReferencedProjectClasspath
 * @param binaryDirectory
 * @return the class path as a list of string locations.
 */
public static List<String> getClasspath(IJavaProject javaProject, boolean getReferencedProjectClasspath,
        boolean binaryDirectory) {

    List<String> paths = new ArrayList<String>();
    try {
        if (javaProject != null) {

            // Get the raw class path
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                switch (entry.getEntryKind()) {

                case IClasspathEntry.CPE_PROJECT:
                    if (!getReferencedProjectClasspath)
                        break;

                    String projectName = entry.getPath().toString();
                    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                    IJavaProject jProject = JavaCore.create(project);

                    List<String> subPaths = getClasspath(jProject, true, binaryDirectory);
                    paths.addAll(subPaths);
                    break;

                case IClasspathEntry.CPE_LIBRARY:
                    IPath path = entry.getPath();
                    paths.add(path.toString());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    entry = JavaCore.getResolvedClasspathEntry(entry);
                    if (entry != null) {
                        path = entry.getPath();
                        paths.add(path.toString());
                    }
                    break;

                }
            }

            // Add the "bin" directory?
            if (binaryDirectory && javaProject.getOutputLocation() != null) {
                IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation();
                path = path.append(javaProject.getOutputLocation());
                paths.add(path.toString());
            }
        }

    } catch (JavaModelException e) {
        PetalsCommonPlugin.log(e, IStatus.ERROR);
    }

    return paths;
}