Search in sources :

Example 6 with LightElement

use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.

the class RefactoringConflictsUtil method analyzeModuleConflicts.

public static void analyzeModuleConflicts(final Project project, final Collection<? extends PsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
    if (scopes == null)
        return;
    for (final PsiElement scope : scopes) {
        if (scope instanceof PsiPackage)
            return;
    }
    final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
    if (targetModule == null)
        return;
    final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
    final HashSet<PsiElement> reported = new HashSet<>();
    for (final PsiElement scope : scopes) {
        scope.accept(new JavaRecursiveElementVisitor() {

            @Override
            public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
                super.visitReferenceElement(reference);
                final PsiElement resolved = reference.resolve();
                if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !(resolved instanceof LightElement) && !haveElementInScope(resolved)) {
                    if (resolved instanceof PsiMethod) {
                        for (PsiMethod superMethod : ((PsiMethod) resolved).findDeepestSuperMethods()) {
                            if (haveElementInScope(superMethod))
                                return;
                        }
                    }
                    final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
                    final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
                    conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
                    reported.add(resolved);
                }
            }

            private boolean haveElementInScope(PsiElement resolved) {
                if (PsiSearchScopeUtil.isInScope(resolveScope, resolved)) {
                    return true;
                }
                if (!resolved.getManager().isInProject(resolved)) {
                    if (resolved instanceof PsiMember) {
                        final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
                        if (containingClass != null) {
                            final String fqn = containingClass.getQualifiedName();
                            if (fqn != null) {
                                final PsiClass classFromTarget = JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope);
                                if (classFromTarget != null) {
                                    if (resolved instanceof PsiMethod) {
                                        return classFromTarget.findMethodsBySignature((PsiMethod) resolved, true).length > 0;
                                    }
                                    if (resolved instanceof PsiField) {
                                        return classFromTarget.findFieldByName(((PsiField) resolved).getName(), false) != null;
                                    }
                                    if (resolved instanceof PsiClass) {
                                        return classFromTarget.findInnerClassByName(((PsiClass) resolved).getName(), false) != null;
                                    }
                                }
                            }
                        }
                    }
                    if (resolved instanceof PsiClass) {
                        final String fqn = ((PsiClass) resolved).getQualifiedName();
                        return fqn != null && JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope) != null;
                    }
                }
                return false;
            }
        });
    }
    boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
    NextUsage: for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element != null && PsiTreeUtil.getParentOfType(element, PsiImportStatement.class, false) == null) {
            for (PsiElement scope : scopes) {
                if (PsiTreeUtil.isAncestor(scope, element, false))
                    continue NextUsage;
            }
            final GlobalSearchScope resolveScope1 = element.getResolveScope();
            if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
                final PsiFile usageFile = element.getContainingFile();
                PsiElement container;
                if (usageFile instanceof PsiJavaFile) {
                    container = ConflictsUtil.getContainer(element);
                } else {
                    container = usageFile;
                }
                final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
                final VirtualFile usageVFile = usageFile.getVirtualFile();
                if (usageVFile != null) {
                    Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
                    if (module != null) {
                        final String message;
                        final PsiElement referencedElement;
                        if (usage instanceof MoveRenameUsageInfo) {
                            referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
                        } else {
                            referencedElement = usage.getElement();
                        }
                        assert referencedElement != null : usage;
                        if (module == targetModule && isInTestSources) {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        } else {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        }
                        conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightElement(com.intellij.psi.impl.light.LightElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 7 with LightElement

use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.

the class RenameProcessor method classifyUsages.

public static MultiMap<PsiElement, UsageInfo> classifyUsages(Collection<? extends PsiElement> elements, UsageInfo[] usages) {
    final MultiMap<PsiElement, UsageInfo> result = new MultiMap<>();
    for (UsageInfo usage : usages) {
        LOG.assertTrue(usage instanceof MoveRenameUsageInfo);
        if (usage.getReference() instanceof LightElement) {
            //filter out implicit references (e.g. from derived class to super class' default constructor)
            continue;
        }
        MoveRenameUsageInfo usageInfo = (MoveRenameUsageInfo) usage;
        if (usage instanceof RelatedUsageInfo) {
            final PsiElement relatedElement = ((RelatedUsageInfo) usage).getRelatedElement();
            if (elements.contains(relatedElement)) {
                result.putValue(relatedElement, usage);
            }
        } else {
            PsiElement referenced = usageInfo.getReferencedElement();
            if (elements.contains(referenced)) {
                result.putValue(referenced, usage);
            } else if (referenced != null) {
                PsiElement indirect = referenced.getNavigationElement();
                if (elements.contains(indirect)) {
                    result.putValue(indirect, usage);
                }
            }
        }
    }
    return result;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) LightElement(com.intellij.psi.impl.light.LightElement) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo)

Example 8 with LightElement

use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.

the class GrRefactoringConflictsUtil method analyzeModuleConflicts.

public static void analyzeModuleConflicts(final Project project, final Collection<? extends GroovyPsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
    if (scopes == null)
        return;
    for (final PsiElement scope : scopes) {
        if (scope instanceof PsiPackage)
            return;
    }
    final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
    if (targetModule == null)
        return;
    final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
    final HashSet<PsiElement> reported = new HashSet<>();
    for (final GroovyPsiElement scope : scopes) {
        scope.accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
                super.visitCodeReferenceElement(refElement);
                visit(refElement);
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression reference) {
                super.visitReferenceExpression(reference);
                visit(reference);
            }

            private void visit(GrReferenceElement<? extends GroovyPsiElement> reference) {
                final PsiElement resolved = reference.resolve();
                if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !PsiSearchScopeUtil.isInScope(resolveScope, resolved) && !(resolved instanceof LightElement)) {
                    final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
                    final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
                    conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
                    reported.add(resolved);
                }
            }
        });
    }
    boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
    NextUsage: for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element != null && PsiTreeUtil.getParentOfType(element, GrImportStatement.class, false) == null) {
            for (PsiElement scope : scopes) {
                if (PsiTreeUtil.isAncestor(scope, element, false))
                    continue NextUsage;
            }
            final GlobalSearchScope resolveScope1 = element.getResolveScope();
            if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
                final PsiFile usageFile = element.getContainingFile();
                PsiElement container;
                if (usageFile instanceof PsiJavaFile) {
                    container = ConflictsUtil.getContainer(element);
                } else {
                    container = usageFile;
                }
                final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
                final VirtualFile usageVFile = usageFile.getVirtualFile();
                if (usageVFile != null) {
                    Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
                    if (module != null) {
                        final String message;
                        final PsiElement referencedElement;
                        if (usage instanceof MoveRenameUsageInfo) {
                            referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
                        } else {
                            referencedElement = usage.getElement();
                        }
                        assert referencedElement != null : usage;
                        if (module == targetModule && isInTestSources) {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        } else {
                            message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
                        }
                        conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LightElement(com.intellij.psi.impl.light.LightElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 9 with LightElement

use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.

the class StubGenerator method writeConstructor.

@Override
public void writeConstructor(final StringBuilder text, PsiMethod constructor, boolean isEnum) {
    LOG.assertTrue(constructor.isConstructor());
    if (!isEnum) {
        text.append("public ");
    //writeModifiers(text, constructor.getModifierList(), JAVA_MODIFIERS);
    }
    /************* name **********/
    //append constructor name
    text.append(constructor.getName());
    /************* parameters **********/
    GenerationUtil.writeParameterList(text, constructor.getParameterList().getParameters(), classNameProvider, null);
    final Set<String> throwsTypes = collectThrowsTypes(constructor, new THashSet<>());
    if (!throwsTypes.isEmpty()) {
        text.append("throws ").append(StringUtil.join(throwsTypes, ", ")).append(' ');
    }
    /************* body **********/
    text.append("{\n");
    if (constructor instanceof GrReflectedMethod) {
        constructor = ((GrReflectedMethod) constructor).getBaseMethod();
    }
    if (constructor instanceof GrMethod) {
        final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
        if (invocation != null) {
            final GroovyResolveResult resolveResult = resolveChainingConstructor((GrMethod) constructor);
            if (resolveResult != null) {
                text.append(invocation.isSuperCall() ? "super(" : "this(");
                writeStubConstructorInvocation(text, (PsiMethod) resolveResult.getElement(), resolveResult.getSubstitutor(), invocation);
                text.append(");");
            }
        } else if (constructor instanceof LightElement) {
            writeStubConstructorInvocation(constructor, text);
        }
    }
    text.append("\n}\n");
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LightElement(com.intellij.psi.impl.light.LightElement)

Example 10 with LightElement

use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.

the class ResolveResultList method poke.

// Allows to add non-null elements and discard nulls in a hassle-free way.
public boolean poke(final PsiElement what, final int rate) {
    PyPsiUtils.assertValid(what);
    if (what == null)
        return false;
    if (!(what instanceof LightElement) && !what.isValid()) {
        throw new PsiInvalidElementAccessException(what, "Trying to resolve a reference to an invalid element");
    }
    super.add(new RatedResolveResult(rate, what));
    return true;
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) LightElement(com.intellij.psi.impl.light.LightElement)

Aggregations

LightElement (com.intellij.psi.impl.light.LightElement)10 UsageInfo (com.intellij.usageView.UsageInfo)4 Module (com.intellij.openapi.module.Module)3 Project (com.intellij.openapi.project.Project)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 HashSet (com.intellij.util.containers.HashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 ATTR_NAME (com.android.SdkConstants.ATTR_NAME)1 TAG_STRING (com.android.SdkConstants.TAG_STRING)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 ResourceType (com.android.resources.ResourceType)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 PsiField (com.intellij.psi.PsiField)1