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:org.eclipse.acceleo.common.internal.utils.workspace.AcceleoWorkspaceUtil.java

License:Open Source License

/**
 * This will return the set of output folders name for the given (java) project.
 * <p>//from  ww  w. j  av  a2 s.c  o m
 * For example, if a project has a source folder "src" with its output folder set as "bin" and a source
 * folder "src-gen" with its output folder set as "bin-gen", this will return a LinkedHashSet containing
 * both "bin" and "bin-gen".
 * </p>
 * 
 * @param project
 *            The project we seek the output folders of.
 * @return The set of output folders name for the given (java) project.
 */
private static Set<String> getOutputFolders(IProject project) {
    final Set<String> classpathEntries = new CompactLinkedHashSet<String>();
    final IJavaProject javaProject = JavaCore.create(project);
    try {
        for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                final IPath output = entry.getOutputLocation();
                if (output != null) {
                    classpathEntries.add(output.removeFirstSegments(1).toString());
                }
            }
        }
        /*
         * Add the default output location to the classpath anyway since source folders are not required
         * to have their own
         */
        final IPath output = javaProject.getOutputLocation();
        classpathEntries.add(output.removeFirstSegments(1).toString());
    } catch (JavaModelException e) {
        AcceleoCommonPlugin.log(e, false);
    }
    return classpathEntries;
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Constructor./* w  w w.  j ava  2 s .  com*/
 * 
 * @param project
 *            is the project
 */
public AcceleoProject(IProject project) {
    if (!registryInitialized) {
        registryInitialized = true;
        registerPackages();
    }
    this.project = project;
    this.sourceFolders = new ArrayList<IPath>();
    final IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            this.sourceFolders.add(entry.getPath());
        }
    }
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Computes the URIs of all the accessible output files (EMTL) in the dependencies of the current project.
 * It browses the resolved classpath of the java project, and keeps each entry of type
 * 'IClasspathEntry.CPE_PROJECT'./*w w  w.j a va 2  s. c o m*/
 * 
 * @param outputURIs
 *            is an output parameter with all the URIs
 * @param aProject
 *            is the current project
 */
private void computeAccessibleOutputFilesWithProjectDependencies(List<URI> outputURIs, IProject aProject) {
    final IJavaProject javaProject = JavaCore.create(aProject);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        AcceleoUIActivator.getDefault().getLog().log(e1.getStatus());
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().toString());
            if (requiredProject != null && requiredProject.exists()) {
                computeAccessibleOutputFilesInFolder(outputURIs, getOutputFolder(requiredProject));
            }
        }
    }
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Computes all the accessible projects.
 * /*w w w .j  a va2 s  .c  o  m*/
 * @param accessibleProjects
 *            is the output list that will contain all the accessible projects
 * @param current
 *            is the current project
 */
private void computeAccessibleProjects(List<IProject> accessibleProjects, IProject current) {
    if (!accessibleProjects.contains(current)) {
        accessibleProjects.add(current);
        IPluginModelBase plugin = PluginRegistry.findModel(current);
        if (plugin != null && plugin.getBundleDescription() != null) {
            BundleDescription[] requiredPlugins = plugin.getBundleDescription().getResolvedRequires();
            for (int i = 0; i < requiredPlugins.length; i++) {
                String requiredSymbolicName = requiredPlugins[i].getSymbolicName();
                IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(requiredSymbolicName);
                if (requiredProject != null && requiredProject.isAccessible()) {
                    computeAccessibleProjects(accessibleProjects, requiredProject);
                }
            }
        }
        final IJavaProject javaProject = JavaCore.create(current);
        IClasspathEntry[] entries;
        try {
            entries = javaProject.getResolvedClasspath(true);
        } catch (JavaModelException e1) {
            AcceleoUIActivator.getDefault().getLog().log(e1.getStatus());
            entries = new IClasspathEntry[] {};
        }
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject requiredProject = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entry.getPath().toString());
                if (requiredProject != null && requiredProject.exists()) {
                    computeAccessibleProjects(accessibleProjects, requiredProject);
                }
            }
        }
    }
}

From source file:org.eclipse.acceleo.internal.compatibility.parser.mt.ast.core.ProjectParser.java

License:Open Source License

/**
 * Creates the templates for the existing '.mt' files.
 * /* w  ww  .j ava 2  s.c  o  m*/
 * @param projects
 *            the projects to browse
 * @param root
 *            the root element of the model to create
 * @return a map to attach an '.mt' file and its model representation
 * @throws CoreException
 *             if an issue occurs, it contains a status object describing the cause of the exception
 */
private static Map<IFile, Template> createTemplates(IProject[] projects, ResourceSet root)
        throws CoreException {
    Map<IFile, Template> files = new HashMap<IFile, Template>();
    Set<IFolder> done = new CompactHashSet<IFolder>();
    for (int i = 0; i < projects.length; i++) {
        IProject project = projects[i];
        final IJavaProject javaProject = JavaCore.create(project);
        final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int j = 0; j < entries.length; j++) {
            final IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 1) {
                final IFolder inputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
                if (!done.contains(inputFolder) && inputFolder.exists()) {
                    done.add(inputFolder);
                    createTemplates(files, inputFolder.getFullPath(), inputFolder, root);
                }
            }
        }
    }
    return files;
}

From source file:org.eclipse.acceleo.internal.ide.ui.builders.AcceleoBuilder.java

License:Open Source License

/**
 * Computes the classpath for the given java project.
 * /*from   w w  w .ja  v a2  s.  co m*/
 * @param javaProject
 *            The Java project
 * @return The classpath entries
 */
private Set<AcceleoProjectClasspathEntry> computeProjectClassPath(IJavaProject javaProject) {
    Set<AcceleoProjectClasspathEntry> classpathEntries = new LinkedHashSet<AcceleoProjectClasspathEntry>();

    // Compute the classpath of the acceleo project
    IClasspathEntry[] rawClasspath;
    try {
        rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : rawClasspath) {
            int entryKind = iClasspathEntry.getEntryKind();
            if (IClasspathEntry.CPE_SOURCE == entryKind) {
                // We have the source folders of the project.
                IPath inputFolderPath = iClasspathEntry.getPath();
                IPath outputFolderPath = iClasspathEntry.getOutputLocation();

                if (outputFolderPath == null) {
                    outputFolderPath = javaProject.getOutputLocation();
                }

                IProject project = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(inputFolderPath.lastSegment());
                if (!(project != null && project.exists() && project.equals(javaProject.getProject()))) {
                    IContainer inputContainer = ResourcesPlugin.getWorkspace().getRoot()
                            .getFolder(inputFolderPath);
                    IContainer outputContainer = ResourcesPlugin.getWorkspace().getRoot()
                            .getFolder(outputFolderPath);

                    if (inputContainer != null && outputContainer != null) {
                        File inputDirectory = inputContainer.getLocation().toFile();
                        File outputDirectory = outputContainer.getLocation().toFile();
                        AcceleoProjectClasspathEntry entry = new AcceleoProjectClasspathEntry(inputDirectory,
                                outputDirectory);
                        classpathEntries.add(entry);

                        this.outputFolders.add(outputDirectory);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        AcceleoUIActivator.log(e, true);
    }
    return classpathEntries;
}

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

License:Open Source License

public static List<IClasspathEntry> resolveDependentProjectClasspath(IClasspathEntry projEntry,
        IProject requiredProj) {/*from   w w  w.j  a v  a2s .c o  m*/
    // add all output locations and exported classpath entities
    // AspectJ compiler doesn't understand the concept of a java project
    List<IClasspathEntry> actualEntries = new ArrayList<IClasspathEntry>();

    try {
        JavaProject requiredJavaProj = (JavaProject) JavaCore.create(requiredProj);
        // bug 288395 Do not use the default mechanism for resolving classpath here
        // this will look into jar files at the Classpath header in the jar's manifest
        // and include jar files that are potentially missing, but have no effect on
        // the build.
        Object resolvedClasspath = requiredJavaProj.resolveClasspath(requiredJavaProj.getRawClasspath(), true,
                false);
        IClasspathEntry[] requiredEntries = extractRequiredEntries(resolvedClasspath);
        for (int i = 0; i < requiredEntries.length; i++) {
            IClasspathEntry requiredEntry = requiredEntries[i];
            if (requiredEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // always add source entries even if not explicitly exported
                // don't add the source folder itself, but instead add the outfolder
                IPath outputLocation = requiredEntry.getOutputLocation();
                if (outputLocation != null) {
                    IAccessRule[] rules = projEntry.getAccessRules();
                    IClasspathAttribute[] attributes = projEntry.getExtraAttributes();

                    // only add the out folder if it already exists
                    if (requiredProj.getFolder(outputLocation.removeFirstSegments(1)).exists()) {
                        IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation,
                                requiredEntry.getPath(), requiredProj.getFullPath(), rules, attributes,
                                projEntry.isExported());
                        actualEntries.add(outFolder);
                    }
                }
            } else if (requiredEntry.isExported()) {
                // must recur through this entry and add entries that it contains
                actualEntries.addAll(resolveClasspath(requiredEntry, requiredProj));

            }
        } // for (int i = 0; i < requiredEntries.length; i++)

        IPath outputLocation = requiredJavaProj.getOutputLocation();
        // Output location may not exist.  Do not put output location of required project
        // on path unless it exists
        boolean exists = false;
        // bug 244330 check to see if the project folder is also the output folder
        if (outputLocation.segmentCount() == 1) {
            exists = true;
        } else {
            if (requiredProj.getWorkspace().getRoot().getFolder(outputLocation).exists()) {
                exists = true;
            }
        }

        if (exists) {
            IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation, null,
                    requiredProj.getFullPath());
            actualEntries.add(outFolder);
        }
    } catch (JavaModelException e) {
    }
    return actualEntries;
}

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

License:Open Source License

/**
 * Resolves a single classpath entry// w  w w  . ja v  a 2 s.co  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

/**
 * 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//w w  w.j av 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 String[] internalGetProjectPath(IProject project, IClasspathAttribute attribute,
        boolean useResolvedPath) {
    if (isAspectPathAttribute(attribute)) {
        if (shouldCheckOldStylePath(project, ASPECTPATH)) {
            String[] old = getOldProjectPath(project, true);
            if (old != null) {
                AJLog.log("Migrating aspect path settings for project " + project.getName()); //$NON-NLS-1$
                setProjectAspectPath(project, old[0], old[1], old[2]);
            }//from   ww w . j av  a2 s.com
            markOldStylePathAsRead(project, ASPECTPATH);
        }
    } else { // INPATH_ATTRIBUTE
        if (shouldCheckOldStylePath(project, INPATH)) {
            String[] old = getOldProjectPath(project, false);
            if (old != null) {
                AJLog.log("Migrating aspect path settings for project " + project.getName()); //$NON-NLS-1$
                setProjectInPath(project, old[0], old[1], old[2]);
            }
            markOldStylePathAsRead(project, INPATH);
        }
    }
    String pathString = ""; //$NON-NLS-1$
    String contentString = ""; //$NON-NLS-1$
    String entryString = ""; //$NON-NLS-1$

    IJavaProject javaProject = JavaCore.create(project);
    try {
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            IClasspathAttribute[] attributes = cp[i].getExtraAttributes();
            boolean attributeFound = false;
            for (int j = 0; j < attributes.length; j++) {
                if (attributes[j].getName().equals(attribute.getName())) {
                    attributeFound = true;
                    List<IClasspathEntry> actualEntries = new ArrayList<IClasspathEntry>();

                    if (useResolvedPath) {
                        // this entry is on the path.  must resolve it
                        if (cp[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                            List<IClasspathEntry> containerEntries = resolveClasspathContainer(cp[i], project);
                            // Bug 273770 - look for the XXXPATH_RESTRICTION_ATTRIBUTE_NAME classpath attribute
                            Set<String> extraPathElements = findContainerRestrictions(cp[i],
                                    isAspectPathAttribute(attribute));
                            if (extraPathElements != null && extraPathElements.size() > 0) {
                                // must filter
                                for (Iterator<IClasspathEntry> cpIter = containerEntries.iterator(); cpIter
                                        .hasNext();) {
                                    IClasspathEntry containerEntry = cpIter.next();
                                    if (!containsAsPathFragment(extraPathElements, containerEntry)) {
                                        cpIter.remove();
                                    }
                                }
                            }
                            actualEntries.addAll(containerEntries);
                        } else if (cp[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                            IProject requiredProj = project.getWorkspace().getRoot()
                                    .getProject(cp[i].getPath().makeRelative().toPortableString());
                            if (!requiredProj.getName().equals(project.getName()) && requiredProj.exists()) {
                                actualEntries.addAll(resolveDependentProjectClasspath(cp[i], requiredProj));
                            }
                        } else { // resolve the classpath variable
                            IClasspathEntry resolved = JavaCore.getResolvedClasspathEntry(cp[i]);
                            if (resolved != null) {
                                if (resolved.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                                    // must resolve the project
                                    actualEntries.addAll(
                                            resolveDependentProjectClasspath(resolved, project.getWorkspace()
                                                    .getRoot().getProject(resolved.getPath().toString())));
                                } else {
                                    actualEntries.add(resolved);
                                }
                            }
                        } // cp[i].getEntryKind()
                    } else {
                        actualEntries.add(cp[i]);
                    } // useResolvedEntry

                    for (IClasspathEntry actualEntry : actualEntries) {
                        // we can get null for actualEntry if the raw entry corresponds to 
                        // an unbound classpath variable
                        if (actualEntry != null) {
                            pathString += actualEntry.getPath().toPortableString() + File.pathSeparator;
                            contentString += actualEntry.getContentKind() + File.pathSeparator;
                            entryString += actualEntry.getEntryKind() + File.pathSeparator;
                        }
                    }
                } // attributes[j].equals(attribute)
            } // for (int j = 0; j < attributes.length; j++)

            // there is a special case that we must look inside the classpath container for entries with
            // attributes if we are returning the resolved path and the container itself isn't already
            // on the path.
            if (!attributeFound && useResolvedPath && cp[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                List<IClasspathEntry> containerEntries = resolveClasspathContainer(cp[i], project);

                for (IClasspathEntry containerEntry : containerEntries) {
                    if (isOnPath(containerEntry, isAspectPathAttribute(attribute))) {
                        pathString += containerEntry.getPath().toPortableString() + File.pathSeparator;
                        contentString += containerEntry.getContentKind() + File.pathSeparator;
                        entryString += containerEntry.getEntryKind() + File.pathSeparator;
                    }
                } // for (Iterator cpIter = containerEntries.iterator(); cpIter.hasNext(); ) 
            } // !attributeFound && useResolvedPath && cp[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER
        } // for (int i = 0; i < cp.length; i++)
    } catch (JavaModelException e) {
    }
    return new String[] { pathString, contentString, entryString };
}