Search in sources :

Example 1 with AccessRuleSet

use of org.eclipse.jdt.internal.compiler.env.AccessRuleSet in project che by eclipse.

the class ClasspathEntry method equals.

/**
     * Returns true if the given object is a classpath entry
     * with equivalent attributes.
     */
public boolean equals(Object object) {
    if (this == object)
        return true;
    if (object instanceof org.eclipse.jdt.internal.core.ClasspathEntry) {
        org.eclipse.jdt.internal.core.ClasspathEntry otherEntry = (org.eclipse.jdt.internal.core.ClasspathEntry) object;
        if (this.contentKind != otherEntry.getContentKind())
            return false;
        if (this.entryKind != otherEntry.getEntryKind())
            return false;
        if (this.isExported != otherEntry.isExported())
            return false;
        if (!this.path.equals(otherEntry.getPath()))
            return false;
        IPath otherPath = otherEntry.getSourceAttachmentPath();
        if (this.sourceAttachmentPath == null) {
            if (otherPath != null)
                return false;
        } else {
            if (!this.sourceAttachmentPath.equals(otherPath))
                return false;
        }
        otherPath = otherEntry.getSourceAttachmentRootPath();
        if (this.sourceAttachmentRootPath == null) {
            if (otherPath != null)
                return false;
        } else {
            if (!this.sourceAttachmentRootPath.equals(otherPath))
                return false;
        }
        if (!equalPatterns(this.inclusionPatterns, otherEntry.getInclusionPatterns()))
            return false;
        if (!equalPatterns(this.exclusionPatterns, otherEntry.getExclusionPatterns()))
            return false;
        AccessRuleSet otherRuleSet = otherEntry.getAccessRuleSet();
        if (getAccessRuleSet() != null) {
            if (!getAccessRuleSet().equals(otherRuleSet))
                return false;
        } else if (otherRuleSet != null)
            return false;
        if (this.combineAccessRules != otherEntry.combineAccessRules())
            return false;
        otherPath = otherEntry.getOutputLocation();
        if (this.specificOutputLocation == null) {
            if (otherPath != null)
                return false;
        } else {
            if (!this.specificOutputLocation.equals(otherPath))
                return false;
        }
        if (!equalAttributes(this.extraAttributes, otherEntry.getExtraAttributes()))
            return false;
        return true;
    } else {
        return false;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet)

Example 2 with AccessRuleSet

use of org.eclipse.jdt.internal.compiler.env.AccessRuleSet in project che by eclipse.

the class JavaSearchScope method add.

/**
     * Add a path to current java search scope or all project fragment roots if null.
     * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
     * Recurse if dependent projects are found.
     * @param javaProject Project used to get resolved classpath entries
     * @param pathToAdd Path to add in case of single element or null if user want to add all project package fragment roots
     * @param includeMask Mask to apply on classpath entries
     * @param projectsToBeAdded Set to avoid infinite recursion
     * @param visitedProjects Set to avoid adding twice the same project
     * @param referringEntry Project raw entry in referring project classpath
     * @throws JavaModelException May happen while getting java model info
     */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;
    IPath projectPath = project.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    IJavaModel model = javaProject.getJavaModel();
    JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
        //				cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch(entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IClasspathEntry rawEntry = null;
                Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
                if (rootPathToRawEntries != null) {
                    rawEntry = (IClasspathEntry) rootPathToRawEntries.get(entry.getPath());
                }
                if (rawEntry == null)
                    break;
                rawKind: switch(rawEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_VARIABLE:
                        if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                            IPath path = entry.getPath();
                            if (pathToAdd == null || pathToAdd.equals(path)) {
                                Object target = JavaModel.getTarget(path, false);
                                if (// case of an external folder
                                target instanceof IFolder)
                                    path = ((IFolder) target).getFullPath();
                                String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                                //$NON-NLS-1$
                                add(projectPath.toString(), "", pathToString, false, /*not a package*/
                                access);
                                addEnclosingProjectOrJar(entry.getPath());
                            }
                        }
                        break;
                    case IClasspathEntry.CPE_CONTAINER:
                        IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                        if (container == null)
                            break;
                        switch(container.getKind()) {
                            case IClasspathContainer.K_APPLICATION:
                                if ((includeMask & APPLICATION_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            case IClasspathContainer.K_SYSTEM:
                            case IClasspathContainer.K_DEFAULT_SYSTEM:
                                if ((includeMask & SYSTEM_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            default:
                                break rawKind;
                        }
                        IPath path = entry.getPath();
                        if (pathToAdd == null || pathToAdd.equals(path)) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                            //$NON-NLS-1$
                            add(projectPath.toString(), "", pathToString, false, /*not a package*/
                            access);
                            addEnclosingProjectOrJar(entry.getPath());
                        }
                        break;
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                if ((includeMask & REFERENCED_PROJECTS) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        JavaProject referencedProject = (JavaProject) model.getJavaProject(path.toOSString());
                        if (!projectsToBeAdded.contains(referencedProject)) {
                            // do not recurse if depending project was used to create the scope
                            add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
                        }
                    }
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                if ((includeMask & SOURCES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        add(projectPath.toString(), Util.relativePath(path, projectPath.segmentCount()), projectPathString, false, /*not a package*/
                        access);
                    }
                }
                break;
        }
    }
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IProject(org.eclipse.core.resources.IProject) JavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) Map(java.util.Map) IJavaModel(org.eclipse.jdt.core.IJavaModel) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with AccessRuleSet

use of org.eclipse.jdt.internal.compiler.env.AccessRuleSet in project xtext-eclipse by eclipse.

the class JdtTypeProvider method findSecondaryType.

/**
 * Searches a secondary type with the given name and package.
 *
 * Secondary types are toplevel types with a name that does not match the name of the compilation unit.
 * @since 2.9
 */
protected IType findSecondaryType(String packageName, final String typeName) throws JavaModelException {
    IPackageFragmentRoot[] sourceFolders = getSourceFolders();
    IndexManager indexManager = JavaModelManager.getIndexManager();
    if (indexManager.awaitingJobsCount() > 0) {
        // still indexing - don't enter a busy wait loop but ask the source folders directly
        return findSecondaryTypeInSourceFolders(packageName, typeName, sourceFolders);
    }
    // code below is adapted from BasicSearchEnginge.searchAllSecondaryTypes
    // index is ready, query it for a secondary type
    final TypeDeclarationPattern pattern = new TypeDeclarationPattern(packageName == null ? CharOperation.NO_CHAR : packageName.toCharArray(), // top level type - no enclosing type names
    CharOperation.NO_CHAR_CHAR, typeName.toCharArray(), IIndexConstants.SECONDARY_SUFFIX, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    // Get working copy path(s). Store in a single string in case of only one to optimize comparison in requestor
    final HashSet<String> workingCopyPaths = new HashSet<String>();
    String workingCopyPath = null;
    ICompilationUnit[] copies = getWorkingCopies();
    final int copiesLength = copies == null ? 0 : copies.length;
    if (copies != null) {
        if (copiesLength == 1) {
            ICompilationUnit singleWC = copies[0];
            if (singleWC.getPackageDeclaration(packageName).exists()) {
                IType result = singleWC.getType(typeName);
                if (result.exists()) {
                    return result;
                }
            }
            workingCopyPath = copies[0].getPath().toString();
        } else {
            for (int i = 0; i < copiesLength; i++) {
                ICompilationUnit workingCopy = copies[i];
                if (workingCopy.getPackageDeclaration(packageName).exists()) {
                    IType result = workingCopy.getType(typeName);
                    if (result.exists()) {
                        return result;
                    }
                }
                workingCopyPaths.add(workingCopy.getPath().toString());
            }
        }
    }
    final String singleWkcpPath = workingCopyPath;
    final Wrapper<IType> result = Wrapper.forType(IType.class);
    IndexQueryRequestor searchRequestor = new IndexQueryRequestor() {

        @Override
        public boolean acceptIndexMatch(String documentPath, SearchPattern indexRecord, SearchParticipant participant, AccessRuleSet access) {
            // Filter unexpected types
            switch(copiesLength) {
                case 0:
                    break;
                case 1:
                    if (singleWkcpPath == null) {
                        throw new IllegalStateException();
                    }
                    if (singleWkcpPath.equals(documentPath)) {
                        // filter out *the* working copy
                        return true;
                    }
                    break;
                default:
                    if (workingCopyPaths.contains(documentPath)) {
                        // filter out working copies
                        return true;
                    }
                    break;
            }
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(documentPath));
            ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
            IType type = unit.getType(typeName);
            result.set(type);
            return false;
        }
    };
    try {
        indexManager.performConcurrentJob(new PatternSearchJob(pattern, // Java search only
        BasicSearchEngine.getDefaultSearchParticipant(), BasicSearchEngine.createJavaSearchScope(sourceFolders), searchRequestor), IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
    } catch (OperationCanceledException oce) {
    // do nothing
    }
    return result.get();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) PatternSearchJob(org.eclipse.jdt.internal.core.search.PatternSearchJob) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) TypeDeclarationPattern(org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IndexQueryRequestor(org.eclipse.jdt.internal.core.search.IndexQueryRequestor) HashSet(java.util.HashSet)

Example 4 with AccessRuleSet

use of org.eclipse.jdt.internal.compiler.env.AccessRuleSet in project xtext-eclipse by eclipse.

the class ProjectAwareUniqueClassNameValidator method doCheckUniqueInProject.

public boolean doCheckUniqueInProject(final QualifiedName name, final JvmDeclaredType type) throws JavaModelException {
    final IJavaProject javaProject = this.javaProjectProvider.getJavaProject(type.eResource().getResourceSet());
    this.getContext().put(ProjectAwareUniqueClassNameValidator.OUTPUT_CONFIGS, this.outputConfigurationProvider.getOutputConfigurations(type.eResource()));
    final String packageName = type.getPackageName();
    final String typeName = type.getSimpleName();
    final Function1<IPackageFragmentRoot, Boolean> _function = (IPackageFragmentRoot it) -> {
        try {
            int _kind = it.getKind();
            return Boolean.valueOf((_kind == IPackageFragmentRoot.K_SOURCE));
        } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
        }
    };
    final Iterable<IPackageFragmentRoot> sourceFolders = IterableExtensions.<IPackageFragmentRoot>filter(((Iterable<IPackageFragmentRoot>) Conversions.doWrapArray(javaProject.getPackageFragmentRoots())), _function);
    IndexManager indexManager = JavaModelManager.getIndexManager();
    if (((((Object[]) Conversions.unwrapArray(sourceFolders, Object.class)).length == 0) || (indexManager.awaitingJobsCount() > 0))) {
        String _elvis = null;
        if (packageName != null) {
            _elvis = packageName;
        } else {
            _elvis = "";
        }
        ProjectAwareUniqueClassNameValidator.SourceTraversal _doCheckUniqueInProjectSource = this.doCheckUniqueInProjectSource(_elvis, typeName, type, ((IPackageFragmentRoot[]) Conversions.unwrapArray(sourceFolders, IPackageFragmentRoot.class)));
        if (_doCheckUniqueInProjectSource != null) {
            switch(_doCheckUniqueInProjectSource) {
                case DUPLICATE:
                    return false;
                case UNIQUE:
                    return true;
                default:
                    break;
            }
        } else {
        }
    }
    final HashSet<String> workingCopyPaths = CollectionLiterals.<String>newHashSet();
    ICompilationUnit[] copies = this.getWorkingCopies(type);
    if ((copies != null)) {
        for (final ICompilationUnit workingCopy : copies) {
            {
                final IPath path = workingCopy.getPath();
                if ((javaProject.getPath().isPrefixOf(path) && (!this.isDerived(workingCopy.getResource())))) {
                    boolean _exists = workingCopy.getPackageDeclaration(packageName).exists();
                    if (_exists) {
                        IType result = workingCopy.getType(typeName);
                        boolean _exists_1 = result.exists();
                        if (_exists_1) {
                            this.addIssue(type, workingCopy.getElementName());
                            return false;
                        }
                    }
                    workingCopyPaths.add(workingCopy.getPath().toString());
                }
            }
        }
    }
    char[] _xifexpression = null;
    if ((packageName == null)) {
        _xifexpression = CharOperation.NO_CHAR;
    } else {
        _xifexpression = packageName.toCharArray();
    }
    char[] _charArray = typeName.toCharArray();
    final TypeDeclarationPattern pattern = new TypeDeclarationPattern(_xifexpression, CharOperation.NO_CHAR_CHAR, _charArray, IIndexConstants.TYPE_SUFFIX, (SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE));
    final IndexQueryRequestor _function_1 = new IndexQueryRequestor() {

        @Override
        public boolean acceptIndexMatch(final String documentPath, final SearchPattern indexRecord, final SearchParticipant participant, final AccessRuleSet access) {
            boolean _contains = workingCopyPaths.contains(documentPath);
            if (_contains) {
                return true;
            }
            IWorkspaceRoot _root = ResourcesPlugin.getWorkspace().getRoot();
            Path _path = new Path(documentPath);
            IFile file = _root.getFile(_path);
            boolean _isDerived = ProjectAwareUniqueClassNameValidator.this.isDerived(file);
            boolean _not = (!_isDerived);
            if (_not) {
                ProjectAwareUniqueClassNameValidator.this.addIssue(type, file.getName());
                return false;
            }
            return true;
        }
    };
    IndexQueryRequestor searchRequestor = _function_1;
    try {
        SearchParticipant _defaultSearchParticipant = BasicSearchEngine.getDefaultSearchParticipant();
        IJavaSearchScope _createJavaSearchScope = BasicSearchEngine.createJavaSearchScope(((IJavaElement[]) Conversions.unwrapArray(sourceFolders, IJavaElement.class)));
        PatternSearchJob _patternSearchJob = new PatternSearchJob(pattern, _defaultSearchParticipant, _createJavaSearchScope, searchRequestor);
        indexManager.performConcurrentJob(_patternSearchJob, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
        return true;
    } catch (final Throwable _t) {
        if (_t instanceof OperationCanceledException) {
            return false;
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) TypeDeclarationPattern(org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IndexQueryRequestor(org.eclipse.jdt.internal.core.search.IndexQueryRequestor) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) PatternSearchJob(org.eclipse.jdt.internal.core.search.PatternSearchJob) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot)

Aggregations

IPath (org.eclipse.core.runtime.IPath)4 AccessRuleSet (org.eclipse.jdt.internal.compiler.env.AccessRuleSet)4 IFile (org.eclipse.core.resources.IFile)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 Path (org.eclipse.core.runtime.Path)2 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 IType (org.eclipse.jdt.core.IType)2 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)2 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)2 IndexQueryRequestor (org.eclipse.jdt.internal.core.search.IndexQueryRequestor)2 PatternSearchJob (org.eclipse.jdt.internal.core.search.PatternSearchJob)2 IndexManager (org.eclipse.jdt.internal.core.search.indexing.IndexManager)2 TypeDeclarationPattern (org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1