Example usage for org.eclipse.jdt.internal.core ClasspathEntry getPath

List of usage examples for org.eclipse.jdt.internal.core ClasspathEntry getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core ClasspathEntry getPath.

Prototype

@Override
public IPath getPath() 

Source Link

Usage

From source file:net.sf.j2s.core.builder.NameEnvironment.java

License:Open Source License

private void computeClasspathLocations(IWorkspaceRoot root, JavaProject javaProject,
        SimpleLookupTable binaryLocationsPerProject) throws CoreException {

    /* Update cycle marker */
    IMarker cycleMarker = javaProject.getCycleMarker();
    if (cycleMarker != null) {
        int severity = JavaCore.ERROR.equals(javaProject.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))
                ? IMarker.SEVERITY_ERROR
                : IMarker.SEVERITY_WARNING;
        if (severity != cycleMarker.getAttribute(IMarker.SEVERITY, severity))
            cycleMarker.setAttribute(IMarker.SEVERITY, severity);
    }/*  w w  w. j  ava  2 s .  c  o m*/

    IClasspathEntry[] classpathEntries = javaProject.getExpandedClasspath();
    ArrayList sLocations = new ArrayList(classpathEntries.length);
    ArrayList bLocations = new ArrayList(classpathEntries.length);
    nextEntry: for (int i = 0, l = classpathEntries.length; i < l; i++) {
        ClasspathEntry entry = (ClasspathEntry) classpathEntries[i];
        IPath path = entry.getPath();
        Object target = JavaModel.getTarget(path, true);
        if (target == null)
            continue nextEntry;

        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!(target instanceof IContainer))
                continue nextEntry;
            IPath outputPath = entry.getOutputLocation() != null ? entry.getOutputLocation()
                    : javaProject.getOutputLocation();
            IContainer outputFolder;
            if (outputPath.segmentCount() == 1) {
                outputFolder = javaProject.getProject();
            } else {
                outputFolder = root.getFolder(outputPath);
                if (!outputFolder.exists())
                    createOutputFolder(outputFolder);
            }
            sLocations.add(ClasspathLocation.forSourceFolder((IContainer) target, outputFolder,
                    entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars(),
                    entry.ignoreOptionalProblems()));
            continue nextEntry;

        case IClasspathEntry.CPE_PROJECT:
            if (!(target instanceof IProject))
                continue nextEntry;
            IProject prereqProject = (IProject) target;
            if (!JavaProject.hasJavaNature(prereqProject))
                continue nextEntry; // if project doesn't have java nature or is not accessible

            JavaProject prereqJavaProject = (JavaProject) JavaCore.create(prereqProject);
            IClasspathEntry[] prereqClasspathEntries = prereqJavaProject.getRawClasspath();
            ArrayList seen = new ArrayList();
            nextPrereqEntry: for (int j = 0, m = prereqClasspathEntries.length; j < m; j++) {
                IClasspathEntry prereqEntry = prereqClasspathEntries[j];
                if (prereqEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    Object prereqTarget = JavaModel.getTarget(prereqEntry.getPath(), true);
                    if (!(prereqTarget instanceof IContainer))
                        continue nextPrereqEntry;
                    IPath prereqOutputPath = prereqEntry.getOutputLocation() != null
                            ? prereqEntry.getOutputLocation()
                            : prereqJavaProject.getOutputLocation();
                    IContainer binaryFolder = prereqOutputPath.segmentCount() == 1 ? (IContainer) prereqProject
                            : (IContainer) root.getFolder(prereqOutputPath);
                    if (binaryFolder.exists() && !seen.contains(binaryFolder)) {
                        seen.add(binaryFolder);
                        ClasspathLocation bLocation = ClasspathLocation.forBinaryFolder(binaryFolder, true,
                                entry.getAccessRuleSet());
                        bLocations.add(bLocation);
                        if (binaryLocationsPerProject != null) { // normal builder mode
                            ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                                    .get(prereqProject);
                            if (existingLocations == null) {
                                existingLocations = new ClasspathLocation[] { bLocation };
                            } else {
                                int size = existingLocations.length;
                                System.arraycopy(existingLocations, 0,
                                        existingLocations = new ClasspathLocation[size + 1], 0, size);
                                existingLocations[size] = bLocation;
                            }
                            binaryLocationsPerProject.put(prereqProject, existingLocations);
                        }
                    }
                }
            }
            continue nextEntry;

        case IClasspathEntry.CPE_LIBRARY:
            if (target instanceof IResource) {
                IResource resource = (IResource) target;
                ClasspathLocation bLocation = null;
                if (resource instanceof IFile) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forLibrary((IFile) resource, accessRuleSet);
                } else if (resource instanceof IContainer) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forBinaryFolder((IContainer) target, false, accessRuleSet); // is library folder not output folder
                }
                bLocations.add(bLocation);
                if (binaryLocationsPerProject != null) { // normal builder mode
                    IProject p = resource.getProject(); // can be the project being built
                    ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                            .get(p);
                    if (existingLocations == null) {
                        existingLocations = new ClasspathLocation[] { bLocation };
                    } else {
                        int size = existingLocations.length;
                        System.arraycopy(existingLocations, 0,
                                existingLocations = new ClasspathLocation[size + 1], 0, size);
                        existingLocations[size] = bLocation;
                    }
                    binaryLocationsPerProject.put(p, existingLocations);
                }
            } else if (target instanceof File) {
                AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                        .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                        && JavaCore.IGNORE.equals(
                                javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true))) ? null
                                        : entry.getAccessRuleSet();
                bLocations.add(ClasspathLocation.forLibrary(path.toString(), accessRuleSet));
            }
            continue nextEntry;
        }
    }

    // now split the classpath locations... place the output folders ahead of the other .class file folders & jars
    ArrayList outputFolders = new ArrayList(1);
    this.sourceLocations = new ClasspathMultiDirectory[sLocations.size()];
    if (!sLocations.isEmpty()) {
        sLocations.toArray(this.sourceLocations);

        // collect the output folders, skipping duplicates
        next: for (int i = 0, l = this.sourceLocations.length; i < l; i++) {
            ClasspathMultiDirectory md = this.sourceLocations[i];
            IPath outputPath = md.binaryFolder.getFullPath();
            for (int j = 0; j < i; j++) { // compare against previously walked source folders
                if (outputPath.equals(this.sourceLocations[j].binaryFolder.getFullPath())) {
                    md.hasIndependentOutputFolder = this.sourceLocations[j].hasIndependentOutputFolder;
                    continue next;
                }
            }
            outputFolders.add(md);

            // also tag each source folder whose output folder is an independent folder & is not also a source folder
            for (int j = 0, m = this.sourceLocations.length; j < m; j++)
                if (outputPath.equals(this.sourceLocations[j].sourceFolder.getFullPath()))
                    continue next;
            md.hasIndependentOutputFolder = true;
        }
    }

    // combine the output folders with the binary folders & jars... place the output folders before other .class file folders & jars
    this.binaryLocations = new ClasspathLocation[outputFolders.size() + bLocations.size()];
    int index = 0;
    for (int i = 0, l = outputFolders.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    for (int i = 0, l = bLocations.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);
}

From source file:org.eclipse.ajdt.internal.core.builder.BuildClasspathResolver.java

License:Open Source License

private void computeClasspathLocations(IWorkspaceRoot root, JavaProject javaProject,
        SimpleLookupTable binaryLocationsPerProject) throws CoreException {

    /* Update cycle marker */
    IMarker cycleMarker = javaProject.getCycleMarker();
    if (cycleMarker != null) {
        int severity = JavaCore.ERROR.equals(javaProject.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))
                ? IMarker.SEVERITY_ERROR
                : IMarker.SEVERITY_WARNING;
        if (severity != ((Integer) cycleMarker.getAttribute(IMarker.SEVERITY)).intValue())
            cycleMarker.setAttribute(IMarker.SEVERITY, severity);
    }/*from  www  . j  a  va  2s  .co m*/

    IClasspathEntry[] classpathEntries = javaProject.getExpandedClasspath();
    ArrayList sLocations = new ArrayList(classpathEntries.length);
    ArrayList bLocations = new ArrayList(classpathEntries.length);
    nextEntry: for (int i = 0, l = classpathEntries.length; i < l; i++) {
        ClasspathEntry entry = (ClasspathEntry) classpathEntries[i];
        IPath path = entry.getPath();
        Object target = JavaModel.getTarget(path, true);
        if (target == null)
            continue nextEntry;

        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!(target instanceof IContainer))
                continue nextEntry;
            IPath outputPath = entry.getOutputLocation() != null ? entry.getOutputLocation()
                    : javaProject.getOutputLocation();
            IContainer outputFolder;
            if (outputPath.segmentCount() == 1) {
                outputFolder = javaProject.getProject();
            } else {
                outputFolder = root.getFolder(outputPath);
                // AspectJ Change Begin
                // This method can be executing on the wrong thread, where createFolder() will hang, so don't do it!
                // if (!outputFolder.exists())
                //    createFolder(outputFolder);
                // AspectJ Change End
            }
            sLocations.add(ClasspathLocation.forSourceFolder((IContainer) target, outputFolder,
                    entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars()));
            continue nextEntry;

        case IClasspathEntry.CPE_PROJECT:
            if (!(target instanceof IProject))
                continue nextEntry;
            IProject prereqProject = (IProject) target;
            if (!JavaProject.hasJavaNature(prereqProject))
                continue nextEntry; // if project doesn't have java nature or is not accessible

            JavaProject prereqJavaProject = (JavaProject) JavaCore.create(prereqProject);
            IClasspathEntry[] prereqClasspathEntries = prereqJavaProject.getRawClasspath();
            ArrayList seen = new ArrayList();
            nextPrereqEntry: for (int j = 0, m = prereqClasspathEntries.length; j < m; j++) {
                IClasspathEntry prereqEntry = prereqClasspathEntries[j];
                if (prereqEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    Object prereqTarget = JavaModel.getTarget(prereqEntry.getPath(), true);
                    if (!(prereqTarget instanceof IContainer))
                        continue nextPrereqEntry;
                    IPath prereqOutputPath = prereqEntry.getOutputLocation() != null
                            ? prereqEntry.getOutputLocation()
                            : prereqJavaProject.getOutputLocation();
                    IContainer binaryFolder = prereqOutputPath.segmentCount() == 1 ? (IContainer) prereqProject
                            : (IContainer) root.getFolder(prereqOutputPath);
                    if (binaryFolder.exists() && !seen.contains(binaryFolder)) {
                        seen.add(binaryFolder);
                        ClasspathLocation bLocation = ClasspathLocation.forBinaryFolder(binaryFolder, true,
                                entry.getAccessRuleSet());
                        bLocations.add(bLocation);
                        if (binaryLocationsPerProject != null) { // normal builder mode
                            ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                                    .get(prereqProject);
                            if (existingLocations == null) {
                                existingLocations = new ClasspathLocation[] { bLocation };
                            } else {
                                int size = existingLocations.length;
                                System.arraycopy(existingLocations, 0,
                                        existingLocations = new ClasspathLocation[size + 1], 0, size);
                                existingLocations[size] = bLocation;
                            }
                            binaryLocationsPerProject.put(prereqProject, existingLocations);
                        }
                    }
                }
            }
            continue nextEntry;

        case IClasspathEntry.CPE_LIBRARY:
            if (target instanceof IResource) {
                IResource resource = (IResource) target;
                ClasspathLocation bLocation = null;
                if (resource instanceof IFile) {
                    if (!(org.eclipse.jdt.internal.compiler.util.Util
                            .isPotentialZipArchive(path.lastSegment())))
                        continue nextEntry;
                    AccessRuleSet accessRuleSet = JavaCore.IGNORE.equals(
                            javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null
                                    : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forLibrary((IFile) resource, accessRuleSet);
                } else if (resource instanceof IContainer) {
                    AccessRuleSet accessRuleSet = JavaCore.IGNORE.equals(
                            javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null
                                    : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forBinaryFolder((IContainer) target, false, accessRuleSet); // is library folder not output folder
                }
                bLocations.add(bLocation);
                if (binaryLocationsPerProject != null) { // normal builder mode
                    IProject p = resource.getProject(); // can be the project being built
                    ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                            .get(p);
                    if (existingLocations == null) {
                        existingLocations = new ClasspathLocation[] { bLocation };
                    } else {
                        int size = existingLocations.length;
                        System.arraycopy(existingLocations, 0,
                                existingLocations = new ClasspathLocation[size + 1], 0, size);
                        existingLocations[size] = bLocation;
                    }
                    binaryLocationsPerProject.put(p, existingLocations);
                }
            } else if (target instanceof File) {
                if (!(org.eclipse.jdt.internal.compiler.util.Util.isPotentialZipArchive(path.lastSegment())))
                    continue nextEntry;
                AccessRuleSet accessRuleSet = JavaCore.IGNORE
                        .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) ? null
                                : entry.getAccessRuleSet();
                bLocations.add(ClasspathLocation.forLibrary(path.toString(), accessRuleSet));
            }
            continue nextEntry;
        }
    }

    // now split the classpath locations... place the output folders ahead of the other .class file folders & jars
    ArrayList outputFolders = new ArrayList(1);
    this.sourceLocations = new ClasspathMultiDirectory[sLocations.size()];
    if (!sLocations.isEmpty()) {
        sLocations.toArray(this.sourceLocations);

        // collect the output folders, skipping duplicates
        next: for (int i = 0, l = sourceLocations.length; i < l; i++) {
            ClasspathMultiDirectory md = sourceLocations[i];
            IPath outputPath = md.binaryFolder.getFullPath();
            for (int j = 0; j < i; j++) { // compare against previously walked source folders
                if (outputPath.equals(sourceLocations[j].binaryFolder.getFullPath())) {
                    md.hasIndependentOutputFolder = sourceLocations[j].hasIndependentOutputFolder;
                    continue next;
                }
            }
            outputFolders.add(md);

            // also tag each source folder whose output folder is an independent folder & is not also a source folder
            for (int j = 0, m = sourceLocations.length; j < m; j++)
                if (outputPath.equals(sourceLocations[j].sourceFolder.getFullPath()))
                    continue next;
            md.hasIndependentOutputFolder = true;
        }
    }

    // combine the output folders with the binary folders & jars... place the output folders before other .class file folders & jars
    this.binaryLocations = new ClasspathLocation[outputFolders.size() + bLocations.size()];
    int index = 0;
    for (int i = 0, l = outputFolders.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    for (int i = 0, l = bLocations.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);
}

From source file:org.eclipse.ajdt.internal.ui.wizards.PathBlock.java

License:Open Source License

/**
 * Checks for duplicate entries on the inpath compared to the Java build
 * path//www  .  j av  a 2 s  . co  m
 * 
 * This checks to make sure that duplicate entries are not being referred
 * to. For example, it is possible for the JUnit jar to be referred to
 * through a classpath variable as well as a classpath container. This
 * method checks for such duplicates
 * 
 * 1. if an inpath entry is on the build path, then remove it from checking
 * 2. resolve the remaining inpath entries 3. resolve the build path 4.
 * there should be no overlap
 */
private IJavaModelStatus checkForDuplicates(IJavaProject currJProject, IClasspathEntry[] entries) {
    try {
        Map<String, IClasspathEntry> allEntries = new HashMap<String, IClasspathEntry>(entries.length, 1.0f);
        for (int i = 0; i < entries.length; i++) {
            // ignore entries that are inside of a container
            if (getClasspathContainer(entries[i]) == null) {
                allEntries.put(entries[i].getPath().toPortableString(), entries[i]);
            }
        }

        IClasspathEntry[] rawProjectClasspath = currJProject.getRawClasspath();
        for (int i = 0; i < rawProjectClasspath.length; i++) {
            allEntries.remove(rawProjectClasspath[i].getPath().toPortableString());
        }

        IClasspathEntry[] resolvedProjectClasspath = currJProject.getResolvedClasspath(true);
        Map<String, IClasspathEntry> resolvedEntries = new HashMap<String, IClasspathEntry>();
        Iterator<IClasspathEntry> allEntriesIter = allEntries.values().iterator();
        while (allEntriesIter.hasNext()) {
            ClasspathEntry rawEntry = (ClasspathEntry) allEntriesIter.next();
            switch (rawEntry.entryKind) {
            case IClasspathEntry.CPE_SOURCE:
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_VARIABLE:
                IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry);
                resolvedEntries.put(resolvedEntry.getPath().toPortableString(), resolvedEntry);
                break;
            case IClasspathEntry.CPE_CONTAINER:
                List containerEntries = AspectJCorePreferences.resolveClasspathContainer(rawEntry,
                        currJProject.getProject());

                for (Iterator containerIter = containerEntries.iterator(); containerIter.hasNext();) {
                    IClasspathEntry containerEntry = (IClasspathEntry) containerIter.next();
                    resolvedEntries.put(containerEntry.getPath().toPortableString(), containerEntry);
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                IProject thisProject = currJProject.getProject();
                IProject requiredProj = thisProject.getWorkspace().getRoot()
                        .getProject(rawEntry.getPath().makeRelative().toPortableString());
                if (!requiredProj.getName().equals(thisProject.getName()) && requiredProj.exists()) {
                    List containerEntries2 = AspectJCorePreferences.resolveDependentProjectClasspath(rawEntry,
                            requiredProj);
                    for (Iterator containerIter = containerEntries2.iterator(); containerIter.hasNext();) {
                        IClasspathEntry containerEntry = (IClasspathEntry) containerIter.next();
                        resolvedEntries.put(containerEntry.getPath().toPortableString(), containerEntry);
                    }

                }
                break;
            }
        }

        for (int i = 0; i < resolvedProjectClasspath.length; i++) {
            if (resolvedEntries.containsKey(resolvedProjectClasspath[i].getPath().toPortableString())) {
                // duplicate found.
                return new JavaModelStatus(IStatus.WARNING, IStatus.WARNING, currJProject,
                        currJProject.getPath(),
                        UIMessages.InPathBlock_DuplicateBuildEntry + resolvedProjectClasspath[i].getPath());
            }
        }

        return JavaModelStatus.VERIFIED_OK;
    } catch (JavaModelException e) {
        return new JavaModelStatus(e);
    }

}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Internal computation of an expanded classpath. It will eliminate duplicates, and produce copies
 * of exported or restricted classpath entries to avoid possible side-effects ever after.
 *//* w w  w .  j a  v  a 2s  . c o m*/
private void computeExpandedClasspath(ClasspathEntry referringEntry, HashSet rootIDs,
        ObjectVector accumulatedEntries) throws JavaModelException {

    String projectRootId = rootID();
    if (rootIDs.contains(projectRootId)) {
        return; // break cycles if any
    }
    rootIDs.add(projectRootId);

    IClasspathEntry[] resolvedClasspath = getResolvedClasspath();

    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    boolean isInitialProject = referringEntry == null;
    for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
        ClasspathEntry entry = (ClasspathEntry) resolvedClasspath[i];
        if (isInitialProject || entry.isExported()) {
            String rootID = entry.rootID();
            if (rootIDs.contains(rootID)) {
                continue;
            }
            // combine restrictions along the project chain
            ClasspathEntry combinedEntry = entry.combineWith(referringEntry);
            accumulatedEntries.add(combinedEntry);

            // recurse in project to get all its indirect exports (only consider exported entries from there on)
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IResource member = workspaceRoot.findMember(entry.getPath());
                if (member != null && member.getType() == IResource.PROJECT) { // double check if bound to project (23977)
                    IProject projRsc = (IProject) member;
                    if (JavaProject.hasJavaNature(projRsc)) {
                        JavaProject javaProject = (JavaProject) JavaCore.create(projRsc);
                        javaProject.computeExpandedClasspath(combinedEntry, rootIDs, accumulatedEntries);
                    }
                }
            } else {
                rootIDs.add(rootID);
            }
        }
    }
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

public ResolvedClasspath resolveClasspath(IClasspathEntry[] rawClasspath, IClasspathEntry[] referencedEntries,
        boolean usePreviousSession, boolean resolveChainedLibraries) throws JavaModelException {
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    ExternalFoldersManager externalFoldersManager = JavaModelManager.getExternalManager();
    ResolvedClasspath result = new ResolvedClasspath();
    Map knownDrives = new HashMap();

    Map referencedEntriesMap = new HashMap();
    List rawLibrariesPath = new ArrayList();
    LinkedHashSet resolvedEntries = new LinkedHashSet();

    if (resolveChainedLibraries) {
        for (int index = 0; index < rawClasspath.length; index++) {
            IClasspathEntry currentEntry = rawClasspath[index];
            if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                rawLibrariesPath/*from   w w w. jav a2s . co  m*/
                        .add(ClasspathEntry.resolveDotDot(getProject().getLocation(), currentEntry.getPath()));
            }
        }
        if (referencedEntries != null) {
            // The Set is required to keep the order intact while the referencedEntriesMap (Map)
            // is used to map the referenced entries with path
            LinkedHashSet referencedEntriesSet = new LinkedHashSet();
            for (int index = 0; index < referencedEntries.length; index++) {
                IPath path = referencedEntries[index].getPath();
                if (!rawLibrariesPath.contains(path) && referencedEntriesMap.get(path) == null) {
                    referencedEntriesMap.put(path, referencedEntries[index]);
                    referencedEntriesSet.add(referencedEntries[index]);
                }
            }
            if (referencedEntriesSet.size() > 0) {
                result.referencedEntries = new IClasspathEntry[referencedEntriesSet.size()];
                referencedEntriesSet.toArray(result.referencedEntries);
            }
        }
    }

    int length = rawClasspath.length;
    for (int i = 0; i < length; i++) {

        IClasspathEntry rawEntry = rawClasspath[i];
        IClasspathEntry resolvedEntry = rawEntry;

        switch (rawEntry.getEntryKind()) {

        case IClasspathEntry.CPE_VARIABLE:
            try {
                resolvedEntry = manager.resolveVariableEntry(rawEntry, usePreviousSession);
            } catch (ClasspathEntry.AssertionFailedException e) {
                // Catch the assertion failure and set status instead
                // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=55992
                result.unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH,
                        e.getMessage());
                break;
            }
            if (resolvedEntry == null) {
                result.unresolvedEntryStatus = new JavaModelStatus(
                        IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND, this, rawEntry.getPath());
            } else {
                // If the entry is already present in the rawReversetMap, it means the entry and the chained libraries
                // have already been processed. So, skip it.
                if (resolveChainedLibraries && resolvedEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        && result.rawReverseMap.get(resolvedEntry.getPath()) == null) {
                    // resolve Class-Path: in manifest
                    ClasspathEntry[] extraEntries = ((ClasspathEntry) resolvedEntry).resolvedChainedLibraries();
                    for (int j = 0, length2 = extraEntries.length; j < length2; j++) {
                        if (!rawLibrariesPath.contains(extraEntries[j].getPath())) {
                            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
                            // referenced entries for variable entries could also be persisted with extra attributes, so addAsChainedEntry = true
                            addToResult(rawEntry, extraEntries[j], result, resolvedEntries,
                                    externalFoldersManager, referencedEntriesMap, true, knownDrives);
                        }
                    }
                }
                addToResult(rawEntry, resolvedEntry, result, resolvedEntries, externalFoldersManager,
                        referencedEntriesMap, false, knownDrives);
            }
            break;

        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = usePreviousSession
                    ? manager.getPreviousSessionContainer(rawEntry.getPath(), this)
                    : JavaCore.getClasspathContainer(rawEntry.getPath(), this);
            if (container == null) {
                result.unresolvedEntryStatus = new JavaModelStatus(
                        IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND, this, rawEntry.getPath());
                break;
            }

            IClasspathEntry[] containerEntries = container.getClasspathEntries();
            if (containerEntries == null) {
                if (JavaModelManager.CP_RESOLVE_VERBOSE || JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                    JavaModelManager.getJavaModelManager().verbose_missbehaving_container_null_entries(this,
                            rawEntry.getPath());
                }
                break;
            }

            // container was bound
            for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++) {
                ClasspathEntry cEntry = (ClasspathEntry) containerEntries[j];
                if (cEntry == null) {
                    if (JavaModelManager.CP_RESOLVE_VERBOSE || JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                        JavaModelManager.getJavaModelManager().verbose_missbehaving_container(this,
                                rawEntry.getPath(), containerEntries);
                    }
                    break;
                }
                // if container is exported or restricted, then its nested entries must in turn be exported  (21749) and/or propagate restrictions
                cEntry = cEntry.combineWith((ClasspathEntry) rawEntry);

                if (cEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    // resolve ".." in library path
                    cEntry = cEntry.resolvedDotDot(getProject().getLocation());
                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=313965
                    // Do not resolve if the system attribute is set to false   
                    if (resolveChainedLibraries
                            && JavaModelManager.getJavaModelManager().resolveReferencedLibrariesForContainers
                            && result.rawReverseMap.get(cEntry.getPath()) == null) {
                        // resolve Class-Path: in manifest
                        ClasspathEntry[] extraEntries = cEntry.resolvedChainedLibraries();
                        for (int k = 0, length2 = extraEntries.length; k < length2; k++) {
                            if (!rawLibrariesPath.contains(extraEntries[k].getPath())) {
                                addToResult(rawEntry, extraEntries[k], result, resolvedEntries,
                                        externalFoldersManager, referencedEntriesMap, false, knownDrives);
                            }
                        }
                    }
                }
                addToResult(rawEntry, cEntry, result, resolvedEntries, externalFoldersManager,
                        referencedEntriesMap, false, knownDrives);
            }
            break;

        case IClasspathEntry.CPE_LIBRARY:
            // resolve ".." in library path
            resolvedEntry = ((ClasspathEntry) rawEntry).resolvedDotDot(getProject().getLocation());

            if (resolveChainedLibraries && result.rawReverseMap.get(resolvedEntry.getPath()) == null) {
                // resolve Class-Path: in manifest
                ClasspathEntry[] extraEntries = ((ClasspathEntry) resolvedEntry).resolvedChainedLibraries();
                for (int k = 0, length2 = extraEntries.length; k < length2; k++) {
                    if (!rawLibrariesPath.contains(extraEntries[k].getPath())) {
                        addToResult(rawEntry, extraEntries[k], result, resolvedEntries, externalFoldersManager,
                                referencedEntriesMap, true, knownDrives);
                    }
                }
            }

            addToResult(rawEntry, resolvedEntry, result, resolvedEntries, externalFoldersManager,
                    referencedEntriesMap, false, knownDrives);
            break;
        default:
            addToResult(rawEntry, resolvedEntry, result, resolvedEntries, externalFoldersManager,
                    referencedEntriesMap, false, knownDrives);
            break;
        }
    }
    result.resolvedClasspath = new IClasspathEntry[resolvedEntries.size()];
    resolvedEntries.toArray(result.resolvedClasspath);
    return result;
}

From source file:org.jboss.tools.arquillian.core.internal.compiler.ArquillianNameEnvironment.java

License:Open Source License

private void computeClasspathLocations(IWorkspaceRoot root, JavaProject javaProject,
        SimpleLookupTable binaryLocationsPerProject) throws CoreException {

    /* Update cycle marker */
    IMarker cycleMarker = javaProject.getCycleMarker();
    if (cycleMarker != null) {
        int severity = JavaCore.ERROR.equals(javaProject.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))
                ? IMarker.SEVERITY_ERROR
                : IMarker.SEVERITY_WARNING;
        if (severity != cycleMarker.getAttribute(IMarker.SEVERITY, severity))
            cycleMarker.setAttribute(IMarker.SEVERITY, severity);
    }//w ww .  j  a  va  2s.c  o m

    IClasspathEntry[] classpathEntries = javaProject.getExpandedClasspath();
    ArrayList sLocations = new ArrayList(classpathEntries.length);
    ArrayList bLocations = new ArrayList(classpathEntries.length);
    nextEntry: for (int i = 0, l = classpathEntries.length; i < l; i++) {
        ClasspathEntry entry = (ClasspathEntry) classpathEntries[i];
        IPath path = entry.getPath();
        Object target = JavaModel.getTarget(path, true);
        if (target == null)
            continue nextEntry;

        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!(target instanceof IContainer))
                continue nextEntry;
            IPath outputPath = entry.getOutputLocation() != null ? entry.getOutputLocation()
                    : javaProject.getOutputLocation();
            IContainer outputFolder;
            if (outputPath.segmentCount() == 1) {
                outputFolder = javaProject.getProject();
            } else {
                outputFolder = root.getFolder(outputPath);
                if (!outputFolder.exists())
                    createOutputFolder(outputFolder);
            }
            sLocations.add(ClasspathLocation.forSourceFolder((IContainer) target, outputFolder,
                    entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars()));
            continue nextEntry;

        case IClasspathEntry.CPE_PROJECT:
            if (!(target instanceof IProject))
                continue nextEntry;
            IProject prereqProject = (IProject) target;
            if (!JavaProject.hasJavaNature(prereqProject))
                continue nextEntry; // if project doesn't have java nature or is not accessible

            JavaProject prereqJavaProject = (JavaProject) JavaCore.create(prereqProject);
            IClasspathEntry[] prereqClasspathEntries = prereqJavaProject.getRawClasspath();
            ArrayList seen = new ArrayList();
            nextPrereqEntry: for (int j = 0, m = prereqClasspathEntries.length; j < m; j++) {
                IClasspathEntry prereqEntry = prereqClasspathEntries[j];
                if (prereqEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    Object prereqTarget = JavaModel.getTarget(prereqEntry.getPath(), true);
                    if (!(prereqTarget instanceof IContainer))
                        continue nextPrereqEntry;
                    IPath prereqOutputPath = prereqEntry.getOutputLocation() != null
                            ? prereqEntry.getOutputLocation()
                            : prereqJavaProject.getOutputLocation();
                    IContainer binaryFolder = prereqOutputPath.segmentCount() == 1 ? (IContainer) prereqProject
                            : (IContainer) root.getFolder(prereqOutputPath);
                    if (binaryFolder.exists() && !seen.contains(binaryFolder)) {
                        seen.add(binaryFolder);
                        ClasspathLocation bLocation = ClasspathLocation.forBinaryFolder(binaryFolder, true,
                                entry.getAccessRuleSet());
                        bLocations.add(bLocation);
                        if (binaryLocationsPerProject != null) { // normal builder mode
                            ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                                    .get(prereqProject);
                            if (existingLocations == null) {
                                existingLocations = new ClasspathLocation[] { bLocation };
                            } else {
                                int size = existingLocations.length;
                                System.arraycopy(existingLocations, 0,
                                        existingLocations = new ClasspathLocation[size + 1], 0, size);
                                existingLocations[size] = bLocation;
                            }
                            binaryLocationsPerProject.put(prereqProject, existingLocations);
                        }
                    }
                }
            }
            continue nextEntry;

        case IClasspathEntry.CPE_LIBRARY:
            if (target instanceof IResource) {
                IResource resource = (IResource) target;
                ClasspathLocation bLocation = null;
                if (resource instanceof IFile) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forLibrary((IFile) resource, accessRuleSet);
                } else if (resource instanceof IContainer) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forBinaryFolder((IContainer) target, false, accessRuleSet); // is library folder not output folder
                }
                bLocations.add(bLocation);
                if (binaryLocationsPerProject != null) { // normal builder mode
                    IProject p = resource.getProject(); // can be the project being built
                    ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                            .get(p);
                    if (existingLocations == null) {
                        existingLocations = new ClasspathLocation[] { bLocation };
                    } else {
                        int size = existingLocations.length;
                        System.arraycopy(existingLocations, 0,
                                existingLocations = new ClasspathLocation[size + 1], 0, size);
                        existingLocations[size] = bLocation;
                    }
                    binaryLocationsPerProject.put(p, existingLocations);
                }
            } else if (target instanceof File) {

                AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                        .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                        && JavaCore.IGNORE.equals(
                                javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true))) ? null
                                        : entry.getAccessRuleSet();
                bLocations.add(ClasspathLocation.forLibrary(path.toString(), accessRuleSet));
            }
            continue nextEntry;
        }
    }

    // now split the classpath locations... place the output folders ahead of the other .class file folders & jars
    ArrayList outputFolders = new ArrayList(1);
    this.sourceLocations = new ClasspathMultiDirectory[sLocations.size()];
    if (!sLocations.isEmpty()) {
        sLocations.toArray(this.sourceLocations);

        // collect the output folders, skipping duplicates
        next: for (int i = 0, l = this.sourceLocations.length; i < l; i++) {
            ClasspathMultiDirectory md = this.sourceLocations[i];
            IPath outputPath = md.binaryFolder.getFullPath();
            for (int j = 0; j < i; j++) { // compare against previously walked source folders
                if (outputPath.equals(this.sourceLocations[j].binaryFolder.getFullPath())) {
                    md.hasIndependentOutputFolder = this.sourceLocations[j].hasIndependentOutputFolder;
                    continue next;
                }
            }
            outputFolders.add(md);

            // also tag each source folder whose output folder is an independent folder & is not also a source folder
            for (int j = 0, m = this.sourceLocations.length; j < m; j++)
                if (outputPath.equals(this.sourceLocations[j].sourceFolder.getFullPath()))
                    continue next;
            md.hasIndependentOutputFolder = true;
        }
    }

    // combine the output folders with the binary folders & jars... place the output folders before other .class file folders & jars
    //        this.binaryLocations = new ClasspathLocation[outputFolders.size() + bLocations.size()];
    //        int index = 0;
    //        for (int i = 0, l = outputFolders.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    //        for (int i = 0, l = bLocations.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);

    this.binaryLocations = new ClasspathLocation[bLocations.size()];
    int index = 0;
    //        for (int i = 0, l = outputFolders.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    for (int i = 0, l = bLocations.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);

    this.baseBinaryLocations = new ClasspathLocation[binaryLocations.length];
    System.arraycopy(binaryLocations, 0, baseBinaryLocations, 0, binaryLocations.length);
    this.baseSourceLocations = new ClasspathMultiDirectory[sourceLocations.length];
    System.arraycopy(sourceLocations, 0, baseSourceLocations, 0, sourceLocations.length);

}