Search in sources :

Example 46 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.

the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.

private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
    final PsiElement namedElem = getReferencedElement(owner);
    if (namedElem == null)
        return true;
    final Ref<Boolean> result = new Ref<>(true);
    final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
            final Processor<PsiReference> consumer = psiReference -> {
                references.add(psiReference);
                return true;
            };
            ReferencesSearch.search(namedElem).forEach(consumer);
            boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
                }
            });
            if (isProperty) {
                final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {

                    @Override
                    public GrAccessorMethod[] compute() {
                        return ((GrField) namedElem).getGetters();
                    }
                });
                for (GrAccessorMethod getter : getters) {
                    MethodReferencesSearch.search(getter).forEach(consumer);
                }
            }
            for (final PsiReference reference : references) {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        occurrences.add(element);
                    }
                });
            }
        }

        @Override
        public void onCancel() {
            result.set(false);
        }

        @Override
        public void onThrowable(@NotNull Throwable error) {
            super.onThrowable(error);
            result.set(false);
        }

        @Override
        public void onSuccess() {
            result.set(true);
        }
    };
    ProgressManager.getInstance().run(task);
    return result.get().booleanValue();
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) HashSet(com.intellij.util.containers.HashSet) GrMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef.members.GrMethodImpl) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) Ref(com.intellij.openapi.util.Ref) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) GroovyIntentionsBundle(org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyValidationUtil(org.jetbrains.plugins.groovy.refactoring.GroovyValidationUtil) Collections(java.util.Collections) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Task(com.intellij.openapi.progress.Task) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 47 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.

the class GrVariableInliner method getConflicts.

@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    GrExpression expr = (GrExpression) reference.getElement();
    if (expr.getParent() instanceof GrAssignmentExpression) {
        GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
        if (expr.equals(parent.getLValue())) {
            conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
        }
    }
    if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
        if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
            final PsiElement parent = expr.getParent();
            if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
                conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
            }
        }
    }
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.

the class GroovyInlineLocalProcessor method collectRefs.

private static void collectRefs(final GrVariable variable, Instruction[] flow, final List<BitSet> writes, final int writeInstructionNumber, final ArrayList<UsageInfo> toInline) {
    for (Instruction instruction : flow) {
        final PsiElement element = instruction.getElement();
        if (instruction instanceof ReadWriteVariableInstruction) {
            if (((ReadWriteVariableInstruction) instruction).isWrite())
                continue;
            if (element instanceof GrVariable && element != variable)
                continue;
            if (!(element instanceof GrReferenceExpression))
                continue;
            final GrReferenceExpression ref = (GrReferenceExpression) element;
            if (ref.isQualified() || ref.resolve() != variable)
                continue;
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber)) {
                toInline.add(new UsageInfo(ref));
            } else if (writeInstructionNumber == -1 && prev.cardinality() == 0) {
                toInline.add(new ClosureUsage(ref));
            }
        } else if (element instanceof GrClosableBlock) {
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
                final Instruction[] closureFlow = ((GrClosableBlock) element).getControlFlow();
                collectRefs(variable, closureFlow, ControlFlowUtils.inferWriteAccessMap(closureFlow, variable), -1, toInline);
            }
        } else if (element instanceof GrAnonymousClassDefinition) {
            final BitSet prev = writes.get(instruction.num());
            if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
                ((GrAnonymousClassDefinition) element).acceptChildren(new GroovyRecursiveElementVisitor() {

                    @Override
                    public void visitField(@NotNull GrField field) {
                        GrExpression initializer = field.getInitializerGroovy();
                        if (initializer != null) {
                            Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
                            collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                        }
                    }

                    @Override
                    public void visitMethod(@NotNull GrMethod method) {
                        GrOpenBlock block = method.getBlock();
                        if (block != null) {
                            Instruction[] flow = block.getControlFlow();
                            collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                        }
                    }

                    @Override
                    public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
                        GrOpenBlock block = initializer.getBlock();
                        Instruction[] flow = block.getControlFlow();
                        collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
                    }
                });
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) BitSet(java.util.BitSet) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ControlFlowBuilder(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 49 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.

the class GroovyMethodInliner method collectConflicts.

@NotNull
private static MultiMap<PsiElement, String> collectConflicts(@NotNull GrCallExpression call, @NotNull Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos) {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (GroovyInlineMethodUtil.ReferenceExpressionInfo info : infos) {
        if (!PsiUtil.isAccessible(call, info.declaration)) {
            if (info.declaration instanceof PsiMethod) {
                String className = info.containingClass.getName();
                String signature = GroovyRefactoringUtil.getMethodSignature((PsiMethod) info.declaration);
                String name = CommonRefactoringUtil.htmlEmphasize(className + "." + signature);
                conflicts.putValue(info.declaration, GroovyRefactoringBundle.message("method.is.not.accessible.form.context.0", name));
            } else if (info.declaration instanceof PsiField) {
                if (!(info.declaration instanceof GrField && ((GrField) info.declaration).getGetters().length > 0)) {
                    // conflict if field doesn't have implicit getters
                    String className = info.containingClass.getName();
                    String name = CommonRefactoringUtil.htmlEmphasize(className + "." + info.getPresentation());
                    conflicts.putValue(info.declaration, GroovyRefactoringBundle.message("field.is.not.accessible.form.context.0", name));
                }
            }
        }
        AnySupers visitor = new AnySupers();
        info.expression.accept(visitor);
        if (visitor.containsSupers()) {
            conflicts.putValue(info.expression, GroovyRefactoringBundle.message("super.reference.is.used"));
        }
    }
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with GrField

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.

the class GroovyLineMarkerProvider method getGroovyCategory.

private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
    if (element instanceof GrVariableDeclarationImpl) {
        GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
        if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
            return 2;
        }
    }
    if (element instanceof GrField || element instanceof GrTypeParameter)
        return 1;
    if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
        return 2;
    if (element instanceof GrMethod) {
        if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
            return 1;
        }
        TextRange textRange = element.getTextRange();
        int start = textRange.getStartOffset();
        int end = Math.min(documentChars.length(), textRange.getEndOffset());
        int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
        return crlf == 0 ? 1 : 2;
    }
    return 0;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrTypeParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)55 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)13 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)11 PsiElement (com.intellij.psi.PsiElement)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)9 NotNull (org.jetbrains.annotations.NotNull)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)7 ArrayList (java.util.ArrayList)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 Project (com.intellij.openapi.project.Project)4 PsiClass (com.intellij.psi.PsiClass)4 UsageInfo (com.intellij.usageView.UsageInfo)4