Search in sources :

Example 26 with GroovyPsiElement

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

the class GroovyConstructorUsagesSearcher method processConstructorUsages.

static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
    if (!constructor.isConstructor())
        return;
    final PsiClass clazz = constructor.getContainingClass();
    if (clazz == null)
        return;
    SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
    Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
    if (processed == null) {
        collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
    }
    if (!processed.add(clazz))
        return;
    if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
        for (PsiField field : clazz.getFields()) {
            if (field instanceof GrEnumConstant) {
                final PsiReference ref = field.getReference();
                if (ref != null && ref.isReferenceTo(constructor)) {
                    if (!consumer.process(ref))
                        return;
                }
            }
        }
    }
    final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
    final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
        final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
        if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
            return consumer.process(grNewExpression.getReferenceElement());
        }
        return true;
    };
    processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
    //this()
    if (clazz instanceof GrTypeDefinition) {
        if (!processConstructors(constructor, consumer, clazz, true)) {
            return;
        }
    }
    //super()
    DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {

        @Override
        public boolean processInReadAction(PsiClass inheritor) {
            if (inheritor instanceof GrTypeDefinition) {
                if (!processConstructors(constructor, consumer, inheritor, false))
                    return false;
            }
            return true;
        }
    });
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) PairProcessor(com.intellij.util.PairProcessor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Key(com.intellij.openapi.util.Key) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Set(java.util.Set) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TextRange(com.intellij.openapi.util.TextRange) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) Nullable(org.jetbrains.annotations.Nullable) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) Processor(com.intellij.util.Processor) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) SearchScope(com.intellij.psi.search.SearchScope)

Example 27 with GroovyPsiElement

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

the class UnassignedVariableAccessInspection method check.

@Override
protected void check(@NotNull GrControlFlowOwner owner, @NotNull ProblemsHolder problemsHolder) {
    Instruction[] flow = owner.getControlFlow();
    ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, true);
    for (ReadWriteVariableInstruction read : reads) {
        PsiElement element = read.getElement();
        if (element instanceof GroovyPsiElement && !(element instanceof GrClosableBlock)) {
            String name = read.getVariableName();
            GroovyPsiElement property = ResolveUtil.resolveProperty((GroovyPsiElement) element, name);
            if (property != null && !(property instanceof PsiParameter) && !(property instanceof PsiField) && PsiTreeUtil.isAncestor(owner, property, false) && !(myIgnoreBooleanExpressions && isBooleanCheck(element))) {
                problemsHolder.registerProblem(element, GroovyInspectionBundle.message("unassigned.access.tooltip", name));
            }
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiParameter(com.intellij.psi.PsiParameter) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiField(com.intellij.psi.PsiField) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 28 with GroovyPsiElement

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

the class UnusedDefInspection method isUsedInTopLevelFlowOnly.

private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
    GrVariable var = null;
    if (element instanceof GrVariable) {
        var = (GrVariable) element;
    } else if (element instanceof GrReferenceExpression) {
        final PsiElement resolved = ((GrReferenceExpression) element).resolve();
        if (resolved instanceof GrVariable)
            var = (GrVariable) resolved;
    }
    if (var != null) {
        final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
        if (scope == null) {
            PsiFile file = var.getContainingFile();
            if (file == null) {
                LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
                return false;
            } else {
                TextRange range = var.getTextRange();
                LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1));
                return false;
            }
        }
        return ReferencesSearch.search(var, var.getUseScope()).forEach(ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
    }
    return true;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 29 with GroovyPsiElement

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

the class ControlFlowUtils method openBlockCompletesWithStatement.

public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
    GroovyPsiElement elementToCheck = statement;
    while (true) {
        if (elementToCheck == null)
            return false;
        final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
        if (container == null)
            return false;
        if (isLoop(container))
            return false;
        if (container instanceof GrCaseSection) {
            final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
            final GrCaseSection[] sections = switchStatement.getCaseSections();
            if (container == sections[sections.length - 1])
                return false;
        }
        if (container instanceof GrCodeBlock) {
            if (elementToCheck instanceof GrStatement) {
                final GrCodeBlock codeBlock = (GrCodeBlock) container;
                if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
                    return false;
                }
            }
            if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
                if (container.equals(body)) {
                    return true;
                }
                elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
            } else {
                elementToCheck = container;
            }
        } else {
            elementToCheck = container;
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 30 with GroovyPsiElement

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

the class GroovyDslDefaultMembers method delegatesTo.

/**
   * **********************************************************************************
   * Methods and properties of the GroovyDSL language
   * **********************************************************************************
   */
public void delegatesTo(@Nullable PsiElement elem, GdslMembersHolderConsumer consumer) {
    if (elem instanceof PsiClass) {
        final PsiClass clazz = (PsiClass) elem;
        final NonCodeMembersHolder holder = new NonCodeMembersHolder();
        if (clazz instanceof GrTypeDefinition) {
            final PsiClassType type = JavaPsiFacade.getElementFactory(consumer.getProject()).createType(clazz);
            final ResolverProcessor processor = CompletionProcessor.createPropertyCompletionProcessor(clazz);
            final GroovyPsiElement context = (GroovyPsiElement) clazz;
            ResolveUtil.processAllDeclarations(type, processor, ResolveState.initial(), context);
            for (GroovyResolveResult result : processor.getCandidates()) {
                final PsiElement element = result.getElement();
                if (element instanceof PsiMethod && !((PsiMethod) element).isConstructor() || element instanceof PsiField) {
                    holder.addDeclaration(element);
                }
            }
        } else {
            for (PsiMethod method : clazz.getAllMethods()) {
                if (!method.isConstructor())
                    holder.addDeclaration(method);
            }
            for (PsiField field : clazz.getAllFields()) {
                holder.addDeclaration(field);
            }
        }
        consumer.addMemberHolder(holder);
    } else if (elem instanceof GrExpression) {
        GrExpression expr = (GrExpression) elem;
        final PsiType type = expr.getType();
        if (type instanceof PsiClassType) {
            PsiClassType ctype = (PsiClassType) type;
            delegatesTo(ctype.resolve(), consumer);
        }
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) NonCodeMembersHolder(org.jetbrains.plugins.groovy.dsl.holders.NonCodeMembersHolder) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6