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:org.eclim.plugin.jdt.project.JavaProjectManager.java

License:Open Source License

/**
 * Merge the supplied project's classpath with entries found in the specified
 * build file./*from   w  w  w  .  ja v  a  2 s. com*/
 *
 * @param project The project.
 * @param buildfile The path to the build file (pom.xml, ivy.xml, etc)
 * @return The classpath entries.
 */
protected IClasspathEntry[] mergeWithBuildfile(IJavaProject project, String buildfile) throws Exception {
    String filename = FileUtils.getBaseName(buildfile);
    Parser parser = PARSERS.get(filename);
    String var = parser.getClasspathVar();
    Dependency[] dependencies = parser.parse(buildfile);

    IWorkspaceRoot root = project.getProject().getWorkspace().getRoot();
    ArrayList<IClasspathEntry> results = new ArrayList<IClasspathEntry>();

    // load the results with all the non library entries.
    IClasspathEntry[] entries = project.getRawClasspath();
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY
                && entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE) {
            results.add(entry);
        } else {
            IPath path = entry.getPath();
            String prefix = path != null ? path.segment(0) : null;
            if ((!var.equals(prefix)) || preserve(entry)) {
                results.add(entry);
            }
        }
    }

    // merge the dependencies with the classpath entires.
    for (int ii = 0; ii < dependencies.length; ii++) {
        IClasspathEntry match = null;
        for (int jj = 0; jj < entries.length; jj++) {
            if (entries[jj].getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    || entries[jj].getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                String path = entries[jj].getPath().toOSString();
                String pattern = dependencies[ii].getName() + Dependency.VERSION_SEPARATOR;

                // exact match
                if (path.endsWith(dependencies[ii].toString())) {
                    match = entries[jj];
                    results.add(entries[jj]);
                    break;

                    // different version match
                } else if (path.indexOf(pattern) != -1) {
                    break;
                }
            } else if (entries[jj].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                String path = entries[jj].getPath().toOSString();
                if (path.endsWith(dependencies[ii].getName())) {
                    match = entries[jj];
                    break;
                }
            }
        }

        if (match == null) {
            IClasspathEntry entry = createEntry(root, project, dependencies[ii]);
            results.add(entry);
        } else {
            match = null;
        }
    }

    return (IClasspathEntry[]) results.toArray(new IClasspathEntry[results.size()]);
}

From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java

License:Open Source License

/**
 * Recursively collects classpath entries from the current and dependent
 * projects.//from ww w. j a v  a  2s.c  o m
 */
private static void collect(IJavaProject javaProject, List<String> paths, Set<IJavaProject> visited,
        boolean isFirstProject) throws Exception {
    if (visited.contains(javaProject)) {
        return;
    }
    visited.add(javaProject);

    try {
        IPath out = javaProject.getOutputLocation();
        paths.add(ProjectUtils.getFilePath(javaProject.getProject(), out.addTrailingSeparator().toOSString()));
    } catch (JavaModelException ignore) {
        // ignore... just signals that no output dir was configured.
    }

    IProject project = javaProject.getProject();
    String name = project.getName();

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException jme) {
        // this may or may not be a problem.
        logger.warn("Unable to retrieve resolved classpath for project: " + name, jme);
        return;
    }

    final List<IJavaProject> nextProjects = new ArrayList<IJavaProject>();
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_VARIABLE:
            String path = entry.getPath().toOSString().replace('\\', '/');
            if (path.startsWith("/" + name + "/")) {
                path = ProjectUtils.getFilePath(project, path);
            }
            paths.add(path);
            break;
        case IClasspathEntry.CPE_PROJECT:
            if (isFirstProject || entry.isExported()) {
                javaProject = JavaUtils.getJavaProject(entry.getPath().segment(0));
                if (javaProject != null) {
                    // breadth first, not depth first, to preserve dependency ordering
                    nextProjects.add(javaProject);
                }
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            IPath out = entry.getOutputLocation();
            if (out != null) {
                paths.add(ProjectUtils.getFilePath(javaProject.getProject(),
                        out.addTrailingSeparator().toOSString()));
            }
            break;
        }
    }
    // depth second
    for (final IJavaProject nextProject : nextProjects) {
        collect(nextProject, paths, visited, false);
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Checks to see if an entry is already on the aspect path
 *///from   w w  w .j  a  va 2 s. com
public static boolean isOnAspectpath(IProject project, String path) {
    IJavaProject jp = JavaCore.create(project);
    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            if ((cp[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_VARIABLE)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_PROJECT)) {
                IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(cp[i]);
                if (resolvedClasspathEntry != null) {
                    String entry = resolvedClasspathEntry.getPath().toPortableString();
                    if (entry.equals(path)) {
                        if (isOnAspectpath(cp[i])) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return false;
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static List<IClasspathEntry> resolveClasspathContainer(IClasspathEntry classpathContainerEntry,
        IProject thisProject) throws JavaModelException {
    IJavaProject thisJavaProject = JavaCore.create(thisProject);
    IClasspathContainer container = JavaCore.getClasspathContainer(classpathContainerEntry.getPath(),
            thisJavaProject);//from  w w w.ja va 2 s . c o m
    if (container != null) {
        List<IClasspathEntry> actualEntries = new ArrayList<IClasspathEntry>();
        IClasspathEntry[] containerEntries = container.getClasspathEntries();
        for (int i = 0; i < containerEntries.length; i++) {
            // projects must be resolved specially since the AspectJ doesn't understand the 
            // concept of project
            switch (containerEntries[i].getEntryKind()) {
            case IClasspathEntry.CPE_PROJECT:
                IProject requiredProj = thisProject.getWorkspace().getRoot()
                        .getProject(containerEntries[i].getPath().makeRelative().toPortableString());
                if (!requiredProj.getName().equals(thisProject.getName()) && requiredProj.exists()) {
                    actualEntries.addAll(resolveDependentProjectClasspath(containerEntries[i], requiredProj));
                }
                break;

            case IClasspathEntry.CPE_VARIABLE:
                IClasspathEntry resolvedClasspathEntry = JavaCore
                        .getResolvedClasspathEntry(containerEntries[i]);
                if (resolvedClasspathEntry != null) {
                    actualEntries.add(resolvedClasspathEntry);
                }
                break;

            case IClasspathEntry.CPE_CONTAINER:
                // not sure if we can have this, but try anyway
                actualEntries.addAll(resolveClasspathContainer(containerEntries[i], thisProject));
                break;
            case IClasspathEntry.CPE_LIBRARY:
                actualEntries.add(containerEntries[i]);
                break;
            default:
                // do nothing
            }
        }
        return actualEntries;
    } else {
        return Collections.emptyList();
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Resolves a single classpath entry/*w  w  w  .  j ava2 s . c  o m*/
 * @param entry the classpath entry to resolve
 * @param thisProject the java project that has this entry
 * @return the resolved list of classpath entries
 * @throws JavaModelException 
 */
public static List<IClasspathEntry> resolveClasspath(IClasspathEntry entry, IProject thisProject)
        throws JavaModelException {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_CONTAINER:
        return resolveClasspathContainer(entry, thisProject);

    case IClasspathEntry.CPE_LIBRARY:
        return Collections.singletonList(entry);

    case IClasspathEntry.CPE_PROJECT:
        IProject containedProj = thisProject.getWorkspace().getRoot()
                .getProject(entry.getPath().makeRelative().toPortableString());
        if (!containedProj.getName().equals(thisProject.getName()) && containedProj.exists()) {
            return resolveDependentProjectClasspath(entry, containedProj);
        } else {
            return Collections.emptyList();
        }

    case IClasspathEntry.CPE_VARIABLE:
        IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(entry);
        if (resolvedClasspathEntry != null) {
            return Collections.singletonList(resolvedClasspathEntry);
        } else {
            return Collections.emptyList();
        }
    default:
        return Collections.emptyList();
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
  * Checks to see if an entry is already on the Inpath
  *///from   w w w. j a va  2  s  .  c o  m
public static boolean isOnInpath(IProject project, String jarPath) {
    IJavaProject jp = JavaCore.create(project);
    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            if ((cp[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_VARIABLE)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                    || (cp[i].getEntryKind() == IClasspathEntry.CPE_PROJECT)) {
                IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(cp[i]);
                if (resolvedClasspathEntry != null) {
                    String entry = resolvedClasspathEntry.getPath().toPortableString();
                    if (entry.equals(jarPath)) {
                        if (isOnInpath(cp[i])) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return false;
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Firstly, add library to the Java build path if it's not there already,
 * then mark the entry as being on the aspect path
 * @param project//from  w  w  w  .ja v a2 s.  c  o  m
 * @param path
 */
private static void addAttribute(IProject project, String jarPath, int eKind, IClasspathAttribute attribute) {
    IJavaProject jp = JavaCore.create(project);

    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        int cpIndex = getIndexInBuildPathEntry(cp, jarPath);
        if (cpIndex >= 0) { // already on classpath
            // add attribute to classpath entry
            // if it doesn't already exist
            IClasspathEntry pathAdd = cp[cpIndex];
            // only add attribute if this element is not already on the path
            if (isAspectPathAttribute(attribute) ? !isOnAspectpath(pathAdd) : !isOnInpath(pathAdd)) {
                IClasspathAttribute[] attributes = pathAdd.getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length + 1];
                System.arraycopy(attributes, 0, newattrib, 0, attributes.length);
                newattrib[attributes.length] = attribute;
                switch (pathAdd.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    pathAdd = JavaCore.newLibraryEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    pathAdd = JavaCore.newVariableEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    pathAdd = JavaCore.newContainerEntry(pathAdd.getPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    pathAdd = JavaCore.newProjectEntry(pathAdd.getPath(), pathAdd.getAccessRules(), true,
                            newattrib, pathAdd.isExported());
                    break;
                }

                cp[cpIndex] = pathAdd;
                jp.setRawClasspath(cp, null);
            }
        } else {
            addEntryToJavaBuildPath(jp, attribute, jarPath, eKind);
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

private static void addAttribute(IJavaProject jp, IClasspathEntry entry, IClasspathAttribute attr) {
    try {//from  w w w  .j a v a2s .  com
        IClasspathEntry[] cp = jp.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            if (cp[i].equals(entry)) {
                IClasspathAttribute[] attributes = cp[i].getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length + 1];
                System.arraycopy(attributes, 0, newattrib, 0, attributes.length);
                newattrib[attributes.length] = attr;
                switch (cp[i].getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    cp[i] = JavaCore.newLibraryEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    cp[i] = JavaCore.newVariableEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    cp[i] = JavaCore.newContainerEntry(cp[i].getPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    cp[i] = JavaCore.newProjectEntry(cp[i].getPath(), cp[i].getAccessRules(), true, newattrib,
                            cp[i].isExported());
                    break;

                }
            }
        }
        jp.setRawClasspath(cp, null);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static void removeAttribute(IJavaProject jp, IClasspathEntry entry, IClasspathAttribute attr) {
    try {/*from  w  w  w  .  ja  va 2 s . c o m*/
        IClasspathEntry[] cp = jp.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            if (cp[i].equals(entry)) {
                IClasspathAttribute[] attributes = cp[i].getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length - 1];
                int count = 0;
                for (int j = 0; j < attributes.length; j++) {
                    if (!attributes[j].getName().equals(attr.getName())) {
                        newattrib[count++] = attributes[j];
                    }
                }
                switch (cp[i].getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    cp[i] = JavaCore.newLibraryEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    cp[i] = JavaCore.newVariableEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    cp[i] = JavaCore.newContainerEntry(cp[i].getPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    cp[i] = JavaCore.newProjectEntry(cp[i].getPath(), cp[i].getAccessRules(), true, newattrib,
                            cp[i].isExported());
                    break;
                }
            }
        }
        jp.setRawClasspath(cp, null);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
* Remove all occurrences of an attribute
* @param javaProject//from ww  w.j  av a2 s.  co m
* @param attribute
*/
private static void removeAttribute(IJavaProject javaProject, IClasspathAttribute attribute) {
    try {
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        boolean changed = false;
        for (int i = 0; i < cp.length; i++) {
            IClasspathAttribute[] attributes = cp[i].getExtraAttributes();
            boolean found = false;
            for (int j = 0; !found && (j < attributes.length); j++) {
                if (attributes[j].getName().equals(attribute.getName())) {
                    found = true;
                }
            }
            if (found) {
                changed = true;
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length - 1];
                int count = 0;
                for (int j = 0; j < attributes.length; j++) {
                    if (!attributes[j].getName().equals(attribute.getName())) {
                        newattrib[count++] = attributes[j];
                    }
                }
                switch (cp[i].getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    cp[i] = JavaCore.newLibraryEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    cp[i] = JavaCore.newVariableEntry(cp[i].getPath(), cp[i].getSourceAttachmentPath(),
                            cp[i].getSourceAttachmentRootPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    cp[i] = JavaCore.newContainerEntry(cp[i].getPath(), cp[i].getAccessRules(), newattrib,
                            cp[i].isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    cp[i] = JavaCore.newProjectEntry(cp[i].getPath(), cp[i].getAccessRules(), true, newattrib,
                            cp[i].isExported());
                    break;
                }
            }
        }
        if (changed) {
            javaProject.setRawClasspath(cp, null);
        }
    } catch (JavaModelException e) {
    }
}