Example usage for org.eclipse.jdt.internal.core JavaProject getOption

List of usage examples for org.eclipse.jdt.internal.core JavaProject getOption

Introduction

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

Prototype

@Override
public String getOption(String optionName, boolean inheritJavaCoreOptions) 

Source Link

Usage

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

License:Open Source License

/**
 * Compute the non-java resources contained in this java project.
 *//*from  w w  w .  j a va 2s  . c om*/
private Object[] computeNonJavaResources(JavaProject project) {

    // determine if src == project and/or if bin == project
    IPath projectPath = project.getProject().getFullPath();
    boolean srcIsProject = false;
    boolean binIsProject = false;
    char[][] inclusionPatterns = null;
    char[][] exclusionPatterns = null;
    IPath projectOutput = null;
    boolean isClasspathResolved = true;
    try {
        IClasspathEntry entry = project.getClasspathEntryFor(projectPath);
        if (entry != null) {
            srcIsProject = true;
            inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
            exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
        }
        projectOutput = project.getOutputLocation();
        binIsProject = projectPath.equals(projectOutput);
    } catch (JavaModelException e) {
        isClasspathResolved = false;
    }

    Object[] resources = new IResource[5];
    int resourcesCounter = 0;
    try {
        IResource[] members = ((IContainer) project.getResource()).members();
        int length = members.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            IClasspathEntry[] classpath = project.getResolvedClasspath();
            for (int i = 0; i < length; i++) {
                IResource res = members[i];
                switch (res.getType()) {
                case IResource.FILE:
                    IPath resFullPath = res.getFullPath();
                    String resName = res.getName();

                    // ignore a jar file on the classpath
                    if (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                            res.getLocation()/* see https://bugs.eclipse
                                             .org/bugs/show_bug.cgi?id=244406 */, classpath, projectOutput)) {
                        break;
                    }
                    // ignore .java file if src == project
                    if (srcIsProject && Util.isValidCompilationUnitName(resName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)) {
                        break;
                    }
                    // ignore .class file if bin == project
                    if (binIsProject && Util.isValidClassFileName(resName, sourceLevel, complianceLevel)) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                    break;
                case IResource.FOLDER:
                    resFullPath = res.getFullPath();

                    // ignore non-excluded folders on the classpath or that correspond to an output location
                    if ((srcIsProject && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)
                            && Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel))
                            || (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                                    res.getLocation(), classpath, projectOutput))) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                }
            }
        }
        if (resources.length != resourcesCounter) {
            System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter]), 0, resourcesCounter);
        }
    } catch (CoreException e) {
        resources = NO_NON_JAVA_RESOURCES;
        resourcesCounter = 0;
    }
    return resources;
}

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);
    }/*from  w ww  .  jav a2 s.  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);
                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  v a2  s.  c om

    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.che.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Returns the package fragment root represented by the resource, or
 * the package fragment the given resource is located in, or <code>null</code>
 * if the given resource is not on the classpath of the given project.
 *//*from  w ww.j a  v a2s  .  com*/
public static IJavaElement determineIfOnClasspath(File resource, JavaProject project) {
    IPath resourcePath = new Path(resource.getAbsolutePath());
    boolean isExternal = false; //ExternalFoldersManager.isInternalPathForExternalFolder(resourcePath);
    //        if (isExternal)
    //            resourcePath = resource.getLocation();

    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) ((JavaProject) project).manager
                .getInfo(project);
        JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null
                : projectInfo.projectCache;
        HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null
                : projectCache.allPkgFragmentsCache;
        boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment());
        IClasspathEntry[] entries = isJavaLike ? project.getRawClasspath() // JAVA file can only live inside SRC folder (on the raw path)
                : ((JavaProject) project).getResolvedClasspath();

        int length = entries.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            for (int i = 0; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
                    continue;
                IPath rootPath = entry.getPath();
                if (rootPath.equals(resourcePath)) {
                    if (isJavaLike)
                        return null;
                    return project.getPackageFragmentRoot(resource);
                } else if (rootPath.isPrefixOf(resourcePath)) {
                    // allow creation of package fragment if it contains a .java file that is included
                    if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(),
                            ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) {
                        // given we have a resource child of the root, it cannot be a JAR pkg root
                        PackageFragmentRoot root =
                                //                                    isExternal ?
                                //                                    new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) :
                                (PackageFragmentRoot) ((JavaProject) project)
                                        .getFolderPackageFragmentRoot(rootPath);
                        if (root == null)
                            return null;
                        IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount());

                        if (resource.isFile()) {
                            // if the resource is a file, then remove the last segment which
                            // is the file name in the package
                            pkgPath = pkgPath.removeLastSegments(1);
                        }
                        String[] pkgName = pkgPath.segments();

                        // if package name is in the cache, then it has already been validated
                        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141)
                        if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName))
                            return root.getPackageFragment(pkgName);

                        if (pkgName.length != 0 && JavaConventions
                                .validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel),
                                        sourceLevel, complianceLevel)
                                .getSeverity() == IStatus.ERROR) {
                            return null;
                        }
                        return root.getPackageFragment(pkgName);
                    }
                }
            }
        }
    } catch (JavaModelException npe) {
        return null;
    }
    return null;
}

From source file:org.eclipse.che.jdt.internal.core.PackageFragmentRoot.java

License:Open Source License

/**
 * Starting at this folder, create package fragments and add the fragments that are not exclused
 * to the collection of children.//w  w w .j a v  a2s  .co m
 *
 * @throws JavaModelException
 *         The resource associated with this package fragment does not exist
 */
protected void computeFolderChildren(File folder, boolean isIncluded, String[] pkgName, ArrayList vChildren,
        char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {

    if (isIncluded) {
        IPackageFragment pkg = getPackageFragment(pkgName);
        vChildren.add(pkg);
    }
    try {
        File[] members = folder.listFiles();
        boolean hasIncluded = isIncluded;
        int length = members.length;
        if (length > 0) {
            // if package fragment root refers to folder in another IProject, then
            // folder.getProject() is different than getJavaProject().getProject()
            // use the other java project's options to verify the name
            //                IJavaProject otherJavaProject = JavaCore.create(folder.getProject());
            JavaProject javaProject = (JavaProject) getJavaProject();
            String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            //                JavaModelManager manager = JavaModelManager.getJavaModelManager();
            for (int i = 0; i < length; i++) {
                File member = members[i];
                String memberName = member.getName();
                if (member.isDirectory()) {
                    // recurse into sub folders even even parent not included as a sub folder could be included
                    //                            // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=65637)
                    if (Util.isValidFolderNameForPackage(memberName, sourceLevel, complianceLevel)) {
                        // eliminate binary output only if nested inside direct subfolders
                        //                                if (javaProject.contains(member)) {
                        String[] newNames = Util.arrayConcat(pkgName, manager.intern(memberName));
                        boolean isMemberIncluded = false;//!Util.isExcluded(member, inclusionPatterns, exclusionPatterns);
                        computeFolderChildren(member, isMemberIncluded, newNames, vChildren, inclusionPatterns,
                                exclusionPatterns);
                        //                                }
                    }
                } else {
                    if (!hasIncluded
                            && Util.isValidCompilationUnitName(memberName, sourceLevel, complianceLevel)
                    /*&& !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)*/) {
                        hasIncluded = true;
                        IPackageFragment pkg = getPackageFragment(pkgName);
                        vChildren.add(pkg);
                    }
                }
                //                    switch(member.getType()) {
                //
                //                        case IResource.FOLDER:
                //                            // recurse into sub folders even even parent not included as a sub folder could be included
                //                            // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=65637)
                //                            if (Util.isValidFolderNameForPackage(memberName, sourceLevel, complianceLevel)) {
                //                                // eliminate binary output only if nested inside direct subfolders
                //                                if (javaProject.contains(member)) {
                //                                    String[] newNames = Util.arrayConcat(pkgName, manager.intern(memberName));
                //                                    boolean isMemberIncluded = !Util.isExcluded(member, inclusionPatterns, exclusionPatterns);
                //                                    computeFolderChildren((IFolder) member, isMemberIncluded, newNames, vChildren, inclusionPatterns,
                // exclusionPatterns);
                //                                }
                //                            }
                //                            break;
                //                        case IResource.FILE:
                //                            // inclusion filter may only include files, in which case we still want to include the immediate parent
                // package (lazily)
                //                            if (!hasIncluded
                //                                && Util.isValidCompilationUnitName(memberName, sourceLevel, complianceLevel)
                //                                && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) {
                //                                hasIncluded = true;
                //                                IPackageFragment pkg = getPackageFragment(pkgName);
                //                                vChildren.add(pkg);
                //                            }
                //                            break;
                //                    }
            }
        }
    } catch (IllegalArgumentException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); // could be thrown by ElementTree when path
        // is not found
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}

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

License:Open Source License

public static void validateCycles(Map preferredClasspaths) throws JavaModelException {

    //long start = System.currentTimeMillis();

    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject[] rscProjects = workspaceRoot.getProjects();
    int length = rscProjects.length;
    JavaProject[] projects = new JavaProject[length];

    LinkedHashSet cycleParticipants = new LinkedHashSet();
    HashSet traversed = new HashSet();

    // compute cycle participants
    ArrayList prereqChain = new ArrayList();
    for (int i = 0; i < length; i++) {
        if (hasJavaNature(rscProjects[i])) {
            JavaProject project = (projects[i] = (JavaProject) JavaCore.create(rscProjects[i]));
            if (!traversed.contains(project.getPath())) {
                prereqChain.clear();/* w ww . ja  v  a 2s  .c om*/
                project.updateCycleParticipants(prereqChain, cycleParticipants, workspaceRoot, traversed,
                        preferredClasspaths);
            }
        }
    }
    //System.out.println("updateAllCycleMarkers: " + (System.currentTimeMillis() - start) + " ms");

    for (int i = 0; i < length; i++) {
        JavaProject project = projects[i];
        if (project != null) {
            if (cycleParticipants.contains(project.getPath())) {
                IMarker cycleMarker = project.getCycleMarker();
                String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true);
                int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR
                        : IMarker.SEVERITY_WARNING;
                if (cycleMarker != null) {
                    // update existing cycle marker if needed
                    try {
                        int existingSeverity = ((Integer) cycleMarker.getAttribute(IMarker.SEVERITY))
                                .intValue();
                        if (existingSeverity != circularCPSeverity) {
                            cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity);
                        }
                    } catch (CoreException e) {
                        throw new JavaModelException(e);
                    }
                } else {
                    IJavaProject[] projectsInCycle;
                    String cycleString = ""; //$NON-NLS-1$
                    if (cycleParticipants.isEmpty()) {
                        projectsInCycle = null;
                    } else {
                        projectsInCycle = new IJavaProject[cycleParticipants.size()];
                        Iterator it = cycleParticipants.iterator();
                        int k = 0;
                        while (it.hasNext()) {
                            //projectsInCycle[i++] = (IPath) it.next();
                            IResource member = workspaceRoot.findMember((IPath) it.next());
                            if (member != null && member.getType() == IResource.PROJECT) {
                                projectsInCycle[k] = JavaCore.create((IProject) member);
                                if (projectsInCycle[k] != null) {
                                    if (k != 0)
                                        cycleString += ", "; //$NON-NLS-1$
                                    cycleString += projectsInCycle[k++].getElementName();
                                }
                            }
                        }
                    }
                    // create new marker
                    project.createClasspathProblemMarker(new JavaModelStatus(
                            IJavaModelStatusConstants.CLASSPATH_CYCLE, project, cycleString));
                }
            } else {
                project.flushClasspathProblemMarkers(true, false, false);
            }
        }
    }
}

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

License:Open Source License

private boolean isValidPackageName() {
    JavaProject javaProject = (JavaProject) getJavaProject();
    String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    for (int i = 0, length = this.names.length; i < length; i++) {
        if (!Util.isValidFolderNameForPackage(this.names[i], sourceLevel, complianceLevel))
            return false;
    }/* w  w  w. j  a v  a2s . c  om*/
    return true;
}

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

License:Open Source License

/**
 * Starting at this folder, create non-java resources for this package fragment root
 * and add them to the non-java resources collection.
 *
 * @exception JavaModelException  The resource associated with this package fragment does not exist
 *///from  w  ww . jav a2  s. co m
static Object[] computeFolderNonJavaResources(IPackageFragmentRoot root, IContainer folder,
        char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {
    IResource[] nonJavaResources = new IResource[5];
    int nonJavaResourcesCounter = 0;
    JavaProject project = (JavaProject) root.getJavaProject();
    try {
        // GROOVY start
        // here, we only care about non-source package roots in Groovy projects
        boolean isInterestingPackageRoot = LanguageSupportFactory.isInterestingProject(project.getProject())
                && root.getRawClasspathEntry().getEntryKind() != IClasspathEntry.CPE_SOURCE;
        // GROOVY end
        IClasspathEntry[] classpath = project.getResolvedClasspath();
        IResource[] members = folder.members();
        int length = members.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            nextResource: for (int i = 0; i < length; i++) {
                IResource member = members[i];
                switch (member.getType()) {
                case IResource.FILE:
                    String fileName = member.getName();

                    // ignore .java files that are not excluded
                    // GROOVY start
                    /* old {
                     if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel) && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns))
                    } new */
                    if ((Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) &&
                    // we want to show groovy scripts that are coming from class folders
                            !(isInterestingPackageRoot
                                    && LanguageSupportFactory.isInterestingSourceFile(fileName)))
                        // GROOVY end
                        continue nextResource;
                    // ignore .class files
                    if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel))
                        continue nextResource;
                    // ignore .zip or .jar file on classpath
                    if (isClasspathEntry(member.getFullPath(), classpath))
                        continue nextResource;
                    break;

                case IResource.FOLDER:
                    // ignore valid packages or excluded folders that correspond to a nested pkg fragment root
                    if (Util.isValidFolderNameForPackage(member.getName(), sourceLevel, complianceLevel)
                            && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns)
                                    || isClasspathEntry(member.getFullPath(), classpath)))
                        continue nextResource;
                    break;
                }
                if (nonJavaResources.length == nonJavaResourcesCounter) {
                    // resize
                    System.arraycopy(nonJavaResources, 0,
                            (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0,
                            nonJavaResourcesCounter);
                }
                nonJavaResources[nonJavaResourcesCounter++] = member;
            }
        }
        if (ExternalFoldersManager.isInternalPathForExternalFolder(folder.getFullPath())) {
            IJarEntryResource[] jarEntryResources = new IJarEntryResource[nonJavaResourcesCounter];
            for (int i = 0; i < nonJavaResourcesCounter; i++) {
                jarEntryResources[i] = new NonJavaResource(root, nonJavaResources[i]);
            }
            return jarEntryResources;
        } else if (nonJavaResources.length != nonJavaResourcesCounter) {
            System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]),
                    0, nonJavaResourcesCounter);
        }
        return nonJavaResources;
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}

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);
    }/*from w ww.j a v a2 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()));
            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);

}