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.ajdt.core.AspectJCorePreferences.java

License:Open Source License

private static Set<String> findContainerRestrictions(IClasspathEntry containerEntry,
        boolean isAspectPathAttribute) {
    if (containerEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
        return Collections.emptySet();
    }//from w w  w  .ja  v a2 s.c  o  m
    Set<String> restrictionPaths = new HashSet<String>();
    String restrictions = getRestriction(containerEntry,
            isAspectPathAttribute ? ASPECTPATH_RESTRICTION_ATTRIBUTE_NAME : INPATH_RESTRICTION_ATTRIBUTE_NAME);
    if (restrictions != null) {
        String[] restrictionsArr = restrictions.split(",");
        for (int j = 0; j < restrictionsArr.length; j++) {
            restrictionPaths.add(restrictionsArr[j].trim());
        }
        return restrictionPaths;
    } else {
        return null;
    }
}

From source file:org.eclipse.ajdt.core.builder.AJBuilder.java

License:Open Source License

/**
 * returns a list of fully qualified names of entries on the classpath
 * that have been rebuilt since last build
 * @return//from w  w w  .  ja  v  a  2 s . c o  m
 */
private List /*String*/ getChangedRequiredProjects(long lastBuildTimestamp) {
    try {
        // first find all the projects that have changed since last build
        IProject[] projectsOnClasspath = getRequiredProjects(getProject(), true);
        List /*IProject*/ changedProjects = new ArrayList();
        for (int i = 0; i < projectsOnClasspath.length; i++) {
            IProject project = projectsOnClasspath[i];

            // get timestamp of last build for this project
            long otherTimestamp = -1;
            if (AspectJPlugin.isAJProject(project)) {
                AjCompiler compiler = AspectJPlugin.getDefault().getCompilerFactory()
                        .getCompilerForProject(project);
                otherTimestamp = getLastBuildTimeStamp(compiler);
            } else if (project.hasNature(JavaCore.NATURE_ID)) {
                Object s = JavaModelManager.getJavaModelManager().getLastBuiltState(project, null);
                if (s != null && s instanceof State) {
                    State state = (State) s;
                    // need to use reflection to get at the last build time
                    otherTimestamp = getLastBuildTime(state);
                }
            } else {
                otherTimestamp = -1;
            }
            if (lastBuildTimestamp <= otherTimestamp) {
                changedProjects.add(project);
            }
        }
        List /*String*/ changedEntries = new ArrayList();
        Set /*String*/ noDups = new HashSet(); // used to ensure there are no dups

        // now that we have all the projects, need to find out what they contribute to
        // this project's path.  could be itself, a jar, or a class folder
        if (changedProjects.size() > 0) {
            IClasspathEntry[] thisClasspath = JavaCore.create(getProject()).getResolvedClasspath(true);
            for (Iterator projIter = changedProjects.iterator(); projIter.hasNext();) {
                IProject changedProject = (IProject) projIter.next();
                for (int i = 0; i < thisClasspath.length; i++) {
                    IClasspathEntry classpathEntry = thisClasspath[i];
                    switch (classpathEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_PROJECT:
                        if (changedProject.getFullPath().equals(classpathEntry.getPath())) {
                            // resolve project and add all entries
                            List toAdd = listOfClassPathEntriesToListOfString(AspectJCorePreferences
                                    .resolveDependentProjectClasspath(classpathEntry, changedProject));
                            for (Iterator pathIter = toAdd.iterator(); pathIter.hasNext();) {
                                String pathStr = (String) pathIter.next();
                                if (!noDups.contains(pathStr)) {
                                    changedEntries.add(pathStr);
                                    noDups.add(pathStr);
                                }
                            }
                        }
                        break;
                    case IClasspathEntry.CPE_LIBRARY:
                        if (changedProject.getFullPath().isPrefixOf(classpathEntry.getPath())) {
                            // only add if this path exists
                            IWorkspaceRoot root = getProject().getWorkspace().getRoot();
                            IFile onPath = root.getFile(classpathEntry.getPath());
                            if (onPath.exists() || root.getFolder(onPath.getFullPath()).exists()) { // may be a folder
                                String pathStr = onPath.getLocation().toPortableString();
                                if (!noDups.contains(pathStr)) {
                                    changedEntries.add(pathStr);
                                    noDups.add(pathStr);
                                }
                            }
                        }
                    }
                }
            }
        }
        // if all else went well, also add the inpath to the list of changed projects.
        // Adding the inpath always is just a conservative estimate of what has changed.
        // 
        // For Java projects, we only know the last structural build time.  Usually this is 
        // fine, but if the Java project is on the inpath, then we care about the last 
        // build of any kind, which we can't be sure of.  
        // (Actually, we need to know this for Aspect path projects, but aspectj can give us
        // precise time of the last build.
        // 
        // So, as a conservative estimate, put all inpath entries onto the list.
        Set inPathFiles = CoreCompilerConfiguration.getCompilerConfigurationForProject(getProject())
                .getInpath();
        if (inPathFiles != null) {
            for (Iterator fileIter = inPathFiles.iterator(); fileIter.hasNext();) {
                File inpathFile = (File) fileIter.next();
                Path path = new Path(inpathFile.getAbsolutePath());
                String pathStr = path.toPortableString();
                if (!noDups.contains(pathStr)) {
                    changedEntries.add(pathStr);
                    noDups.add(pathStr);
                }
            }
        }
        return changedEntries;
    } catch (Exception e) {
        // something went wrong.
        // return null to imply everything's changed
        AspectJPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, AspectJPlugin.PLUGIN_ID,
                "Error determining list of entries on classpath that have changed.", e));
        return null;
    }
}

From source file:org.eclipse.ajdt.core.builder.AJBuilder.java

License:Open Source License

/**
 * This is taken straight from the JavaBuilder - and is what is returned
 * from the build method//  ww  w. jav  a  2 s.  co m
 */
private IProject[] getRequiredProjects(IProject project, boolean includeBinaryPrerequisites) {

    JavaProject javaProject = (JavaProject) JavaCore.create(project);
    IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();

    if (javaProject == null || workspaceRoot == null)
        return new IProject[0];

    ArrayList<IProject> projects = new ArrayList<IProject>();
    try {
        IClasspathEntry[] entries = javaProject.getExpandedClasspath();
        for (int i = 0, l = entries.length; i < l; i++) {
            IClasspathEntry entry = entries[i];
            IPath path = entry.getPath();
            IProject p = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_PROJECT:
                // missing projects are considered too
                p = workspaceRoot.getProject(path.lastSegment());
                break;
            case IClasspathEntry.CPE_LIBRARY:
                if (includeBinaryPrerequisites && path.segmentCount() > 1) {
                    // some binary resources on the class path can come from
                    // projects that are not included in the project
                    // references
                    IResource resource = workspaceRoot.findMember(path.segment(0));
                    if (resource instanceof IProject)
                        p = (IProject) resource;
                }
            }
            if (p != null && !projects.contains(p))
                projects.add(p);
        }
    } catch (JavaModelException e) {
        return new IProject[0];
    }
    IProject[] result = new IProject[projects.size()];
    projects.toArray(result);
    return result;
}

From source file:org.eclipse.ajdt.core.builder.AJBuilder.java

License:Open Source License

/**
 * Returns the CPE_SOURCE classpath entries for the given IJavaProject
 * //from  w w  w  .  jav a 2  s .c o  m
 * @param IJavaProject
 */
private IClasspathEntry[] getSrcClasspathEntry(IJavaProject javaProject) throws JavaModelException {
    List<IClasspathEntry> srcEntries = new ArrayList<IClasspathEntry>();
    if (javaProject == null) {
        return new IClasspathEntry[0];
    }
    IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
    for (int j = 0; j < cpEntry.length; j++) {
        IClasspathEntry entry = cpEntry[j];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            srcEntries.add(entry);
        }
    }
    return (IClasspathEntry[]) srcEntries.toArray(new IClasspathEntry[srcEntries.size()]);
}

From source file:org.eclipse.ajdt.core.buildpath.BuildConfigurationUtils.java

License:Open Source License

public static void saveBuildConfiguration(IFile ifile) {
    File file = ifile.getLocation().toFile();
    IProject project = ifile.getProject();
    try {//  www  .j  a v a 2  s. c  o  m
        IJavaProject jp = JavaCore.create(project);
        IClasspathEntry[] entries = jp.getRawClasspath();
        List srcIncludes = new ArrayList();
        List srcExcludes = new ArrayList();
        List srcInclusionpatterns = new ArrayList();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcpath = entry.getPath();
                srcpath = srcpath.removeFirstSegments(1);
                String path = srcpath.toString().trim();
                if (!path.endsWith("/")) { //$NON-NLS-1$
                    path = path + "/"; //$NON-NLS-1$
                }
                srcIncludes.add(path);
                IPath[] inclusions = entry.getInclusionPatterns();
                for (int j = 0; j < inclusions.length; j++) {
                    srcInclusionpatterns.add((path.length() > 1 ? path : "") + inclusions[j]); //$NON-NLS-1$   
                }
                IPath[] exclusions = entry.getExclusionPatterns();
                for (int j = 0; j < exclusions.length; j++) {
                    srcExcludes.add((path.length() > 1 ? path : "") + exclusions[j]); //$NON-NLS-1$
                }
            }
        }
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            printProperties(bw, "src.includes", srcIncludes); //$NON-NLS-1$
            printProperties(bw, "src.excludes", srcExcludes); //$NON-NLS-1$
            printProperties(bw, "src.inclusionpatterns", srcInclusionpatterns); //$NON-NLS-1$   
        } catch (IOException e) {
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                }
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.buildpath.BuildConfigurationUtils.java

License:Open Source License

public static void applyBuildConfiguration(IFile ifile) {
    File file = ifile.getLocation().toFile();
    BufferedReader br = null;//w w w.java 2s  .  c  om
    try {
        IJavaProject project = JavaCore.create(ifile.getProject());
        List classpathEntries = new ArrayList();
        IClasspathEntry[] originalEntries = project.getRawClasspath();
        for (int i = 0; i < originalEntries.length; i++) {
            IClasspathEntry entry = originalEntries[i];
            if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                classpathEntries.add(entry);
            }
        }
        List srcFolders = new ArrayList();
        Map srcFoldersToIncludes = new HashMap();
        Map srcFoldersToExcludes = new HashMap();
        br = new BufferedReader(new FileReader(file));
        Properties properties = new Properties();
        properties.load(ifile.getContents());
        Enumeration iter = properties.keys();

        // first stage - find any source folders
        while (iter.hasMoreElements()) {
            String name = iter.nextElement().toString();
            String value = properties.get(name).toString();
            String[] values = value.split(","); //$NON-NLS-1$
            if (name.equals("src.includes")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String inc = values[i];
                    if (inc.equals("/")) { //$NON-NLS-1$
                        srcFolders.add(inc);
                    } else if (inc.indexOf("/") == inc.length() - 1) { //$NON-NLS-1$
                        if (project.getProject().getFolder(inc) != null
                                && project.getProject().getFolder(inc).exists()) {
                            srcFolders.add(inc);
                        }
                    }
                }
            }
        }

        // second stage - identify include and exclude filters
        iter = properties.keys();
        if (srcFolders.isEmpty()) {
            srcFolders.add(""); //$NON-NLS-1$
        }
        while (iter.hasMoreElements()) {
            String name = iter.nextElement().toString();
            String value = properties.get(name).toString();
            String[] values = value.split(","); //$NON-NLS-1$
            if (name.equals("src.inclusionpatterns")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String inc = values[i];
                    for (Iterator iterator = srcFolders.iterator(); iterator.hasNext();) {
                        String srcFolder = (String) iterator.next();
                        if (inc.startsWith(srcFolder)) {
                            List incs = (List) srcFoldersToIncludes.get(srcFolder);
                            if (incs == null) {
                                incs = new ArrayList();
                            }
                            incs.add(inc);
                            srcFoldersToIncludes.put(srcFolder, incs);
                        }
                    }
                }
            } else if (name.equals("src.excludes")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String exc = values[i];
                    for (Iterator iterator = srcFolders.iterator(); iterator.hasNext();) {
                        String srcFolder = (String) iterator.next();
                        if (srcFolder.equals("/") || exc.startsWith(srcFolder)) { //$NON-NLS-1$
                            List excs = (List) srcFoldersToExcludes.get(srcFolder);
                            if (excs == null) {
                                excs = new ArrayList();
                            }
                            excs.add(exc);
                            srcFoldersToExcludes.put(srcFolder, excs);
                        }
                    }
                }
            }
        }

        // third stage - create classpath entries
        IClasspathEntry[] entries = new IClasspathEntry[srcFolders.size() + classpathEntries.size()];
        for (int i = 0; i < entries.length; i++) {
            if (srcFolders.size() > i) {
                String srcFolder = (String) srcFolders.get(i);
                IPath path = project.getPath().append(stripSlash(srcFolder));
                List exclusions = (List) srcFoldersToExcludes.get(srcFolder);
                if (exclusions == null) {
                    exclusions = Collections.EMPTY_LIST;
                }
                List inclusions = (List) srcFoldersToIncludes.get(srcFolder);
                if (inclusions == null) {
                    inclusions = Collections.EMPTY_LIST;
                }
                IPath[] exclusionPatterns = new IPath[exclusions.size()];
                for (int j = 0; j < exclusionPatterns.length; j++) {
                    String exclusionPathStr = (String) exclusions.get(j);
                    if (exclusionPathStr.startsWith(srcFolder)) {
                        exclusionPathStr = exclusionPathStr.substring(srcFolder.length());
                    }
                    IPath exclusionPath = new Path(exclusionPathStr);
                    exclusionPatterns[j] = exclusionPath;

                }
                IPath[] inclusionPatterns = new IPath[inclusions.size()];
                for (int j = 0; j < inclusionPatterns.length; j++) {
                    String inclusionPathStr = (String) inclusions.get(j);
                    if (inclusionPathStr.startsWith(srcFolder)) {
                        inclusionPathStr = inclusionPathStr.substring(srcFolder.length());
                    }
                    IPath inclusionPath = new Path(inclusionPathStr);
                    inclusionPatterns[j] = inclusionPath;

                }
                IClasspathEntry classpathEntry = JavaCore.newSourceEntry(path, exclusionPatterns);
                //new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_SOURCE, path, ClasspathEntry.INCLUDE_ALL, exclusionPatterns, null, null, null, true, ClasspathEntry.NO_ACCESS_RULES, false, ClasspathEntry.NO_EXTRA_ATTRIBUTES);
                entries[i] = classpathEntry;
            } else {
                entries[i] = (IClasspathEntry) classpathEntries.get(i - srcFolders.size());
            }
        }
        project.setRawClasspath(entries, null);
    } catch (FileNotFoundException e) {
    } catch (JavaModelException e) {
    } catch (IOException e) {
    } catch (CoreException e) {
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
            }
        }
    }
}

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

License:Open Source License

/**
 * Get all projects within the workspace who have a dependency on the given
 * project - this can either be a class folder dependency or on a library
 * which the project exports.// www  . j  a  v a  2s.c  om
 * 
 * @param IProject
 *            project
 * @return List of two IProject[] where the first is all the class folder
 *         depending projects, and the second is all the exported library
 *         dependent projects
 */
public static List<IProject[]> getDependingProjects(IProject project) {
    List<IProject[]> projects = new ArrayList<IProject[]>();

    IProject[] projectsInWorkspace = AspectJPlugin.getWorkspace().getRoot().getProjects();
    List<IPath> outputLocationPaths = getOutputLocationPaths(project);
    IClasspathEntry[] exportedEntries = getExportedEntries(project);
    List<IProject> classFolderDependingProjects = new ArrayList<IProject>();
    List<IProject> exportedLibraryDependingProjects = new ArrayList<IProject>();

    workThroughProjects: for (int i = 0; i < projectsInWorkspace.length; i++) {
        if (projectsInWorkspace[i].equals(project) || !(projectsInWorkspace[i].isOpen()))
            continue workThroughProjects;
        try {
            if (projectsInWorkspace[i].hasNature(JavaCore.NATURE_ID)) {
                JavaProject javaProject = (JavaProject) JavaCore.create(projectsInWorkspace[i]);
                if (javaProject == null)
                    continue workThroughProjects;

                try {
                    IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
                    for (int j = 0; j < cpEntry.length; j++) {
                        IClasspathEntry entry = cpEntry[j];
                        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            for (Iterator<IPath> iter = outputLocationPaths.iterator(); iter.hasNext();) {
                                IPath path = iter.next();
                                if (entry.getPath().equals(path)) {
                                    classFolderDependingProjects.add(projectsInWorkspace[i]);
                                    continue workThroughProjects;
                                }
                            }
                            for (int k = 0; k < exportedEntries.length; k++) {
                                if (entry.getPath().equals(exportedEntries[k].getPath())) {
                                    exportedLibraryDependingProjects.add(projectsInWorkspace[i]);
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    continue workThroughProjects;
                }
            }
        } catch (CoreException e) {
        }
    }
    projects.add(0, classFolderDependingProjects.toArray(new IProject[] {}));
    projects.add(1, exportedLibraryDependingProjects.toArray(new IProject[] {}));
    return projects;
}

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

License:Open Source License

private static IClasspathEntry[] getExportedEntries(IProject project) {
    List<IClasspathEntry> exportedEntries = new ArrayList<IClasspathEntry>();

    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return new IClasspathEntry[0];
    }//from ww w  .  ja  v a2 s .c  o  m

    try {
        IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
        for (int j = 0; j < cpEntry.length; j++) {
            IClasspathEntry entry = cpEntry[j];
            if (entry.isExported()) {
                // we don't want to export it in the new classpath.
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IClasspathEntry nonExportedEntry = JavaCore.newLibraryEntry(entry.getPath(), null, null);
                    exportedEntries.add(nonExportedEntry);
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return (IClasspathEntry[]) exportedEntries.toArray(new IClasspathEntry[exportedEntries.size()]);
}

From source file:org.eclipse.ajdt.core.javaelements.AJCompilationUnitManager.java

License:Open Source License

private void addProjectToList(IProject project, List l) {
    if (AspectJPlugin.isAJProject(project)) {
        try {/* www .  jav  a2s.  com*/
            IJavaProject jp = JavaCore.create(project);
            IClasspathEntry[] cpes = jp.getRawClasspath();
            for (int i = 0; i < cpes.length; i++) {
                IClasspathEntry entry = cpes[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath p = entry.getPath();
                    if (p.segmentCount() == 1)
                        addAllAJFilesInFolder(project, l);
                    else
                        addAllAJFilesInFolder(project.getFolder(p.removeFirstSegments(1)), l);
                }
            }
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.ajdt.core.tests.AJDTCoreTestCase.java

License:Open Source License

private IPackageFragmentRoot createDefaultSourceFolder(IJavaProject javaProject) throws CoreException {
    IProject project = javaProject.getProject();
    IFolder folder = project.getFolder("src");
    if (!folder.exists())
        ensureExists(folder);// w w w .  j  ava  2s . co  m

    // if already exists, do nothing
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
    final IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(folder.getFullPath())) {
            return root;
        }
    }

    // else, remove old source folders and add this new one
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    List<IClasspathEntry> oldEntriesList = new ArrayList<IClasspathEntry>();
    oldEntriesList.add(JavaCore.newSourceEntry(root.getPath()));
    for (IClasspathEntry entry : oldEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            oldEntriesList.add(entry);
        }
    }

    IClasspathEntry[] newEntries = oldEntriesList.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newEntries, null);
    return root;
}