Search in sources :

Example 36 with GroovyResolveResult

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

the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.

private static boolean canBeSimplified(PsiElement element) {
    if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
        return false;
    if (element instanceof GrCodeReferenceElement) {
        if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
            return false;
    } else if (element instanceof GrReferenceExpression) {
        if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
            return false;
    } else {
        return false;
    }
    final GrReferenceElement ref = (GrReferenceElement) element;
    if (ref.getQualifier() == null)
        return false;
    if (!(ref.getContainingFile() instanceof GroovyFileBase))
        return false;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass))
        return false;
    final String name = ((PsiClass) resolved).getName();
    if (name == null)
        return false;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
    final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
    final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
    if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
        return true;
    }
    final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
    if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
        return false;
    }
    return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 37 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult 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 38 with GroovyResolveResult

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

the class GDKSuperMethodSearcher method execute.

@Override
public boolean execute(@NotNull SuperMethodsSearch.SearchParameters queryParameters, @NotNull Processor<MethodSignatureBackedByPsiMethod> consumer) {
    final PsiMethod method = queryParameters.getMethod();
    if (!(method instanceof GrMethod)) {
        return true;
    }
    if (method.hasModifierProperty(PsiModifier.STATIC))
        return true;
    final PsiClass psiClass = method.getContainingClass();
    if (psiClass == null)
        return true;
    final HierarchicalMethodSignature hierarchicalSignature = method.getHierarchicalMethodSignature();
    if (!hierarchicalSignature.getSuperSignatures().isEmpty())
        return true;
    final Project project = method.getProject();
    final String name = method.getName();
    final MethodResolverProcessor processor = new MethodResolverProcessor(name, ((GrMethod) method), false, null, null, PsiType.EMPTY_ARRAY);
    ResolveUtil.processNonCodeMembers(JavaPsiFacade.getElementFactory(project).createType(psiClass), processor, (GrMethod) method, ResolveState.initial());
    final GroovyResolveResult[] candidates = processor.getCandidates();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final MethodSignature signature = method.getHierarchicalMethodSignature();
    List<PsiMethod> goodSupers = new ArrayList<>();
    for (GroovyResolveResult candidate : candidates) {
        final PsiElement element = candidate.getElement();
        if (element instanceof PsiMethod) {
            final PsiMethod m = (PsiMethod) element;
            if (!isTheSameMethod(method, psiManager, m) && PsiImplUtil.isExtendsSignature(m.getHierarchicalMethodSignature(), signature)) {
                goodSupers.add(m);
            }
        }
    }
    if (goodSupers.isEmpty())
        return true;
    List<PsiMethod> result = new ArrayList<>(goodSupers.size());
    result.add(goodSupers.get(0));
    final Comparator<PsiMethod> comparator = (o1, o2) -> {
        //compare by first parameter type
        final PsiType type1 = getRealType(o1);
        final PsiType type2 = getRealType(o2);
        if (TypesUtil.isAssignableByMethodCallConversion(type1, type2, o1)) {
            return -1;
        } else if (TypesUtil.isAssignableByMethodCallConversion(type2, type1, o1)) {
            return 1;
        }
        return 0;
    };
    Outer: for (PsiMethod current : goodSupers) {
        for (Iterator<PsiMethod> i = result.iterator(); i.hasNext(); ) {
            PsiMethod m = i.next();
            final int res = comparator.compare(m, current);
            if (res > 0) {
                continue Outer;
            } else if (res < 0) {
                i.remove();
            }
        }
        result.add(current);
    }
    for (PsiMethod psiMethod : result) {
        if (!consumer.process(getRealMethod(psiMethod).getHierarchicalMethodSignature())) {
            return false;
        }
    }
    return true;
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) Iterator(java.util.Iterator) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) QueryExecutor(com.intellij.util.QueryExecutor) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) ResolveUtil(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) MethodSignature(com.intellij.psi.util.MethodSignature) SuperMethodsSearch(com.intellij.psi.search.searches.SuperMethodsSearch) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) MethodSignature(com.intellij.psi.util.MethodSignature) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Iterator(java.util.Iterator) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)

Example 39 with GroovyResolveResult

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

the class GrUnresolvedAccessChecker method checkCodeRefInner.

@Nullable
private HighlightInfo checkCodeRefInner(GrCodeReferenceElement refElement) {
    if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class) != null)
        return null;
    PsiElement nameElement = refElement.getReferenceNameElement();
    if (nameElement == null)
        return null;
    if (isResolvedStaticImport(refElement))
        return null;
    GroovyResolveResult resolveResult = refElement.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    if (!(refElement.getParent() instanceof GrPackageDefinition) && resolved == null) {
        String message = GroovyBundle.message("cannot.resolve", refElement.getReferenceName());
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(nameElement).descriptionAndTooltip(message).create();
        // todo implement for nested classes
        registerCreateClassByTypeFix(refElement, info, myDisplayKey);
        registerAddImportFixes(refElement, info, myDisplayKey);
        UnresolvedReferenceQuickFixProvider.registerReferenceFixes(refElement, new QuickFixActionRegistrarAdapter(info, myDisplayKey));
        QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarAdapter(info, myDisplayKey), refElement);
        return info;
    }
    if (refElement.getParent() instanceof GrNewExpression) {
        boolean inStaticContext = GrStaticChecker.isInStaticContext(refElement);
        if (!inStaticContext && GrUnresolvedAccessInspection.isSuppressed(refElement))
            return null;
        if (!inStaticContext) {
            if (!myInspectionEnabled)
                return null;
            assert myInspection != null;
            if (!myInspection.myHighlightInnerClasses)
                return null;
        }
        GrNewExpression newExpression = (GrNewExpression) refElement.getParent();
        if (resolved instanceof PsiClass) {
            PsiClass clazz = (PsiClass) resolved;
            final PsiClass outerClass = clazz.getContainingClass();
            if (com.intellij.psi.util.PsiUtil.isInnerClass(clazz) && outerClass != null && newExpression.getArgumentList() != null && !PsiUtil.hasEnclosingInstanceInScope(outerClass, newExpression, true) && !hasEnclosingInstanceInArgList(newExpression.getArgumentList(), outerClass)) {
                String qname = clazz.getQualifiedName();
                LOG.assertTrue(qname != null, clazz.getText());
                return createAnnotationForRef(refElement, inStaticContext, GroovyBundle.message("cannot.reference.non.static", qname));
            }
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with GroovyResolveResult

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

the class GroovyUntypedAccessInspection method buildVisitor.

@Override
@NotNull
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
            super.visitReferenceExpression(refExpr);
            if (PsiUtil.isThisOrSuperRef(refExpr))
                return;
            GroovyResolveResult resolveResult = refExpr.advancedResolve();
            PsiElement resolved = resolveResult.getElement();
            if (resolved != null) {
                if (GrHighlightUtil.isDeclarationAssignment(refExpr) || resolved instanceof PsiPackage)
                    return;
            } else {
                GrExpression qualifier = refExpr.getQualifierExpression();
                if (qualifier == null && GrHighlightUtil.isDeclarationAssignment(refExpr))
                    return;
            }
            final PsiType refExprType = refExpr.getType();
            if (refExprType == null) {
                if (resolved != null) {
                    registerError(refExpr);
                }
            } else if (refExprType instanceof PsiClassType && ((PsiClassType) refExprType).resolve() == null) {
                registerError(refExpr);
            }
        }
    };
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiPackage(com.intellij.psi.PsiPackage) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6