Search in sources :

Example 36 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class HighlightControlFlowUtil method checkVariableMustBeFinal.

@Nullable
static HighlightInfo checkVariableMustBeFinal(@NotNull PsiVariable variable, @NotNull PsiJavaCodeReferenceElement context, @NotNull LanguageLevel languageLevel) {
    if (variable.hasModifierProperty(PsiModifier.FINAL))
        return null;
    final PsiElement innerClass = getInnerClassVariableReferencedFrom(variable, context);
    if (innerClass instanceof PsiClass) {
        if (variable instanceof PsiParameter) {
            final PsiElement parent = variable.getParent();
            if (parent instanceof PsiParameterList && parent.getParent() instanceof PsiLambdaExpression && notAccessedForWriting(variable, new LocalSearchScope(((PsiParameter) variable).getDeclarationScope()))) {
                return null;
            }
        }
        final boolean isToBeEffectivelyFinal = languageLevel.isAtLeast(LanguageLevel.JDK_1_8);
        if (isToBeEffectivelyFinal && isEffectivelyFinal(variable, innerClass, context)) {
            return null;
        }
        final String description = JavaErrorMessages.message(isToBeEffectivelyFinal ? "variable.must.be.final.or.effectively.final" : "variable.must.be.final", context.getText());
        final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(description).create();
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createVariableAccessFromInnerClassFix(variable, innerClass));
        return highlightInfo;
    }
    return checkWriteToFinalInsideLambda(variable, context);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class SurroundAutoCloseableAction method moveStatements.

private static List<PsiElement> moveStatements(PsiElement last, PsiTryStatement statement) {
    PsiCodeBlock tryBlock = statement.getTryBlock();
    assert tryBlock != null : statement.getText();
    PsiElement parent = statement.getParent();
    LocalSearchScope scope = new LocalSearchScope(parent);
    List<PsiElement> toFormat = new SmartList<>();
    PsiElement stopAt = last.getNextSibling();
    PsiElement i = statement.getNextSibling();
    while (i != null && i != stopAt) {
        PsiElement child = i;
        i = PsiTreeUtil.skipSiblingsForward(i, PsiWhiteSpace.class, PsiComment.class);
        if (!(child instanceof PsiDeclarationStatement))
            continue;
        PsiElement anchor = child;
        for (PsiElement declared : ((PsiDeclarationStatement) child).getDeclaredElements()) {
            if (!(declared instanceof PsiLocalVariable))
                continue;
            int endOffset = last.getTextRange().getEndOffset();
            boolean contained = ReferencesSearch.search(declared, scope).forEach(ref -> ref.getElement().getTextOffset() <= endOffset);
            if (!contained) {
                PsiLocalVariable var = (PsiLocalVariable) declared;
                PsiElementFactory factory = JavaPsiFacade.getElementFactory(statement.getProject());
                String name = var.getName();
                assert name != null : child.getText();
                toFormat.add(parent.addBefore(factory.createVariableDeclarationStatement(name, var.getType(), null), statement));
                PsiExpression varInit = var.getInitializer();
                if (varInit != null) {
                    String varAssignText = name + " = " + varInit.getText() + ";";
                    anchor = parent.addAfter(factory.createStatementFromText(varAssignText, parent), anchor);
                }
                var.delete();
            }
        }
        if (child == last && !child.isValid()) {
            last = anchor;
        }
    }
    PsiElement first = statement.getNextSibling();
    tryBlock.addRangeBefore(first, last, tryBlock.getRBrace());
    parent.deleteChildRange(first, last);
    return toFormat;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SmartList(com.intellij.util.SmartList)

Example 38 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class CollectionAddAllCanBeReplacedWithConstructorInspection method isAddAllReplaceable.

private static boolean isAddAllReplaceable(final PsiExpression addAllExpression, PsiNewExpression newExpression) {
    final boolean[] isReplaceable = new boolean[] { true };
    final PsiFile newExpressionContainingFile = newExpression.getContainingFile();
    final TextRange newExpressionTextRange = newExpression.getTextRange();
    addAllExpression.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(PsiReferenceExpression expression) {
            final PsiElement resolved = expression.resolve();
            if (resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter) {
                PsiVariable variable = (PsiVariable) resolved;
                final LocalSearchScope useScope = (LocalSearchScope) variable.getUseScope();
                if (!useScope.containsRange(newExpressionContainingFile, newExpressionTextRange)) {
                    isReplaceable[0] = false;
                }
            }
        }
    });
    return isReplaceable[0];
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) TextRange(com.intellij.openapi.util.TextRange)

Example 39 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class ImportHelper method calcClassesToReimport.

private static void calcClassesToReimport(@NotNull PsiJavaFile file, @NotNull JavaPsiFacade facade, @NotNull PsiResolveHelper helper, @NotNull String packageName, @NotNull Collection<String> onDemandRefs, @NotNull List<PsiClass> outClassesToReimport) {
    if (onDemandRefs.isEmpty()) {
        return;
    }
    PsiPackage aPackage = facade.findPackage(packageName);
    if (aPackage == null) {
        return;
    }
    PsiDirectory[] dirs = aPackage.getDirectories();
    GlobalSearchScope resolveScope = file.getResolveScope();
    for (PsiDirectory dir : dirs) {
        // do not iterate classes - too slow when not loaded
        PsiFile[] files = dir.getFiles();
        for (PsiFile aFile : files) {
            if (!(aFile instanceof PsiJavaFile))
                continue;
            String name = aFile.getVirtualFile().getNameWithoutExtension();
            for (String refName : onDemandRefs) {
                String conflictClassName = refName + "." + name;
                PsiClass conflictClass = facade.findClass(conflictClassName, resolveScope);
                if (conflictClass == null || !helper.isAccessible(conflictClass, file, null))
                    continue;
                String conflictClassName2 = packageName + "." + name;
                PsiClass conflictClass2 = facade.findClass(conflictClassName2, resolveScope);
                if (conflictClass2 != null && helper.isAccessible(conflictClass2, file, null) && ReferencesSearch.search(conflictClass, new LocalSearchScope(file), false).findFirst() != null) {
                    outClassesToReimport.add(conflictClass);
                }
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 40 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class TurnRefsToSuperProcessorBase method linkTypeParameterInstantiations.

private void linkTypeParameterInstantiations(PsiTypeParameter typeParameter, final PsiTypeElement instantiation, final PsiClass inheritingClass) {
    final PsiTypeParameterListOwner owner = typeParameter.getOwner();
    if (owner instanceof PsiClass) {
        final PsiClass ownerClass = ((PsiClass) owner);
        final LocalSearchScope derivedScope = new LocalSearchScope(inheritingClass);
        final PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(ownerClass, inheritingClass, PsiSubstitutor.EMPTY);
        if (substitutor == null)
            return;
        ownerClass.accept(new JavaRecursiveElementVisitor() {

            @Override
            public void visitTypeElement(PsiTypeElement parent) {
                super.visitTypeElement(parent);
                final PsiElement pparent = parent.getParent();
                if (pparent instanceof PsiMethod && parent.equals(((PsiMethod) pparent).getReturnTypeElement())) {
                    final PsiMethod method = (PsiMethod) pparent;
                    final MethodSignature signature = method.getSignature(substitutor);
                    if (PsiUtil.isAccessible(method, inheritingClass, null)) {
                        final PsiMethod inInheritor = MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false);
                        if (inInheritor != null && inInheritor.getReturnTypeElement() != null) {
                            addLink(instantiation, method.getReturnTypeElement());
                            addLink(method.getReturnTypeElement(), instantiation);
                        }
                    }
                } else if (pparent instanceof PsiParameter) {
                    final PsiParameter parameter = (PsiParameter) pparent;
                    if (parameter.getDeclarationScope() instanceof PsiMethod) {
                        PsiMethod method = (PsiMethod) parameter.getDeclarationScope();
                        final int index = ((PsiParameterList) parameter.getParent()).getParameterIndex(parameter);
                        final MethodSignature signature = method.getSignature(substitutor);
                        if (PsiUtil.isAccessible(method, inheritingClass, null)) {
                            final PsiMethod inInheritor = MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false);
                            if (inInheritor != null) {
                                final PsiParameter[] inheritorParams = inInheritor.getParameterList().getParameters();
                                LOG.assertTrue(inheritorParams.length > index);
                                final PsiTypeElement hisTypeElement = inheritorParams[index].getTypeElement();
                                addLink(instantiation, hisTypeElement);
                                addLink(hisTypeElement, instantiation);
                            }
                        }
                    }
                }
            }
        });
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4