Search in sources :

Example 6 with GrAccessorMethod

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

the class OldReferencesResolver method getQualifierFromGetterCall.

/**
   * checks for the case: qualifier.getFoo()(args)
   * @param methodExpression
   */
@Nullable
private static GrExpression getQualifierFromGetterCall(GrMethodCall methodExpression) {
    final GroovyResolveResult result = methodExpression.advancedResolve();
    if (!(result.getElement() instanceof GrAccessorMethod) || result.isInvokedOnProperty())
        return null;
    final GrExpression invoked = methodExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression)
        return ((GrReferenceExpression) invoked).getQualifier();
    return null;
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrAccessorMethod

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

the class GroovyPostHighlightingPass method isFieldUnused.

private static boolean isFieldUnused(GrField field, ProgressIndicator progress, GlobalUsageHelper usageHelper) {
    if (!UnusedSymbolUtil.isFieldUnused(field.getProject(), field.getContainingFile(), field, progress, usageHelper))
        return false;
    final GrAccessorMethod[] getters = field.getGetters();
    final GrAccessorMethod setter = field.getSetter();
    for (GrAccessorMethod getter : getters) {
        if (getter.findSuperMethods().length > 0) {
            return false;
        }
    }
    if (setter != null) {
        if (setter.findSuperMethods().length > 0) {
            return false;
        }
    }
    return true;
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)

Example 8 with GrAccessorMethod

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

the class GroovyNamedArgumentProvider method getNamedArgumentsFromAllProviders.

@Nullable
public static Map<String, NamedArgumentDescriptor> getNamedArgumentsFromAllProviders(@NotNull GrCall call, @Nullable String argumentName, boolean forCompletion) {
    Map<String, NamedArgumentDescriptor> namedArguments = new HashMap<String, NamedArgumentDescriptor>() {

        @Override
        public NamedArgumentDescriptor put(String key, NamedArgumentDescriptor value) {
            NamedArgumentDescriptor oldValue = super.put(key, value);
            if (oldValue != null) {
                super.put(key, oldValue);
            }
            //noinspection ConstantConditions
            return oldValue;
        }
    };
    GroovyResolveResult[] callVariants = call.getCallVariants(null);
    if (callVariants.length == 0 || PsiUtil.isSingleBindingVariant(callVariants)) {
        for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
            namedArgumentProvider.getNamedArguments(call, GroovyResolveResult.EMPTY_RESULT, argumentName, forCompletion, namedArguments);
        }
    } else {
        boolean mapExpected = false;
        for (GroovyResolveResult result : callVariants) {
            PsiElement element = result.getElement();
            if (element instanceof GrAccessorMethod)
                continue;
            if (element instanceof PsiMethod) {
                PsiMethod method = (PsiMethod) element;
                PsiParameter[] parameters = method.getParameterList().getParameters();
                if (!method.isConstructor() && !(parameters.length > 0 && canBeMap(parameters[0])))
                    continue;
                mapExpected = true;
                for (GroovyMethodInfo methodInfo : GroovyMethodInfo.getInfos(method)) {
                    if (methodInfo.getNamedArguments() != null || methodInfo.isNamedArgumentProviderDefined()) {
                        if (methodInfo.isApplicable(method)) {
                            if (methodInfo.isNamedArgumentProviderDefined()) {
                                methodInfo.getNamedArgProvider().getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
                            }
                            if (methodInfo.getNamedArguments() != null) {
                                namedArguments.putAll(methodInfo.getNamedArguments());
                            }
                        }
                    }
                }
            }
            for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
                namedArgumentProvider.getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
            }
            if (element instanceof GrVariable && InheritanceUtil.isInheritor(((GrVariable) element).getTypeGroovy(), GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
                mapExpected = true;
            }
        }
        if (!mapExpected && namedArguments.isEmpty()) {
            return null;
        }
    }
    return namedArguments;
}
Also used : HashMap(java.util.HashMap) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrAccessorMethod

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

the class AccessorReferencesSearcher method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    final PsiElement element = queryParameters.getElementToSearch();
    if (element instanceof PsiMethod) {
        final String propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod) element);
        if (propertyName == null)
            return;
        SearchScope scope = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope());
        queryParameters.getOptimizer().searchWord(propertyName, scope, UsageSearchContext.IN_CODE, true, element);
    } else if (element instanceof GrField) {
        for (GrAccessorMethod method : ((GrField) element).getGetters()) {
            MethodReferencesSearch.search(method, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
        }
        final GrAccessorMethod setter = ((GrField) element).getSetter();
        if (setter != null) {
            MethodReferencesSearch.search(setter, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiMethod(com.intellij.psi.PsiMethod) SearchScope(com.intellij.psi.search.SearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 10 with GrAccessorMethod

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

the class GrReferenceExpressionImpl method calculateType.

@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
    final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();
    for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
        PsiType type = calculator.calculateType(refExpr, resolved);
        if (type != null)
            return type;
    }
    if (ResolveUtil.isClassReference(refExpr)) {
        GrExpression qualifier = refExpr.getQualifier();
        LOG.assertTrue(qualifier != null);
        return qualifier.getType();
    }
    if (PsiUtil.isCompileStatic(refExpr)) {
        final PsiType type;
        if (resolved instanceof GrField) {
            type = ((GrField) resolved).getType();
        } else if (resolved instanceof GrVariable) {
            type = ((GrVariable) resolved).getDeclaredType();
        } else if (resolved instanceof GrAccessorMethod) {
            type = ((GrAccessorMethod) resolved).getProperty().getType();
        } else {
            type = null;
        }
        if (type != null) {
            return result.getSubstitutor().substitute(type);
        }
    }
    final PsiType nominal = refExpr.getNominalType(forceRValue);
    Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
    if (reassigned != null && reassigned.booleanValue()) {
        return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
    }
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
        if (nominal == null) {
            //inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
            if (resolved instanceof GrVariable) {
                LOG.assertTrue(resolved.isValid());
                return ((GrVariable) resolved).getTypeGroovy();
            }
        }
        return nominal;
    }
    if (nominal == null)
        return inferred;
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
        if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
            return nominal;
        }
    }
    return inferred;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpressionTypeCalculator(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)25 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)13 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)5 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 MultiMap (com.intellij.util.containers.MultiMap)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiMethod (com.intellij.psi.PsiMethod)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 UsageInfo (com.intellij.usageView.UsageInfo)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2