Search in sources :

Example 16 with GrGdkMethod

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

the class AccessorMethodReferencesSearcher method processQuery.

@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    final PsiMethod method = queryParameters.getMethod();
    final String propertyName;
    if (GdkMethodUtil.isCategoryMethod(method, null, null, PsiSubstitutor.EMPTY)) {
        final GrGdkMethod cat = GrGdkMethodImpl.createGdkMethod(method, false, null);
        propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod) cat);
    } else {
        propertyName = GroovyPropertyUtils.getPropertyName(method);
    }
    if (propertyName == null)
        return;
    final SearchScope onlyGroovyFiles = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope(), GroovyScopeUtil.getEffectiveScope(method));
    queryParameters.getOptimizer().searchWord(propertyName, onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
    if (!GroovyPropertyUtils.isPropertyName(propertyName)) {
        queryParameters.getOptimizer().searchWord(StringUtil.decapitalize(propertyName), onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
    }
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) PsiMethod(com.intellij.psi.PsiMethod) SearchScope(com.intellij.psi.search.SearchScope)

Example 17 with GrGdkMethod

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

the class GroovyTargetElementEvaluator method getElementByReference.

@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
    PsiElement sourceElement = ref.getElement();
    if (sourceElement instanceof GrCodeReferenceElement) {
        GrNewExpression newExpr;
        if (sourceElement.getParent() instanceof GrNewExpression) {
            newExpr = (GrNewExpression) sourceElement.getParent();
        } else if (sourceElement.getParent().getParent() instanceof GrNewExpression) {
            //anonymous class declaration
            newExpr = (GrNewExpression) sourceElement.getParent().getParent();
        } else {
            return null;
        }
        final PsiMethod constructor = newExpr.resolveMethod();
        final GrArgumentList argumentList = newExpr.getArgumentList();
        if (constructor != null && argumentList != null && PsiImplUtil.hasNamedArguments(argumentList) && !PsiImplUtil.hasExpressionArguments(argumentList)) {
            if (constructor.getParameterList().getParametersCount() == 0)
                return constructor.getContainingClass();
        }
        return constructor;
    }
    if (sourceElement instanceof GrReferenceExpression) {
        PsiElement resolved = ((GrReferenceExpression) sourceElement).resolve();
        if (resolved instanceof GrGdkMethod || !(resolved instanceof GrRenameableLightElement)) {
            return correctSearchTargets(resolved);
        }
        return resolved;
    }
    return null;
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) PsiMethod(com.intellij.psi.PsiMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrRenameableLightElement(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrRenameableLightElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 18 with GrGdkMethod

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

the class PsiImplUtil method getRuntimeQualifier.

@Nullable
public static GrExpression getRuntimeQualifier(@NotNull GrReferenceExpression refExpr) {
    GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier != null)
        return qualifier;
    for (GrClosableBlock closure = PsiTreeUtil.getParentOfType(refExpr, GrClosableBlock.class); closure != null; closure = PsiTreeUtil.getParentOfType(closure, GrClosableBlock.class)) {
        PsiElement parent = closure.getParent();
        if (parent instanceof GrArgumentList)
            parent = parent.getParent();
        if (!(parent instanceof GrMethodCall))
            continue;
        GrExpression funExpr = ((GrMethodCall) parent).getInvokedExpression();
        if (!(funExpr instanceof GrReferenceExpression))
            return funExpr;
        final PsiElement resolved = ((GrReferenceExpression) funExpr).resolve();
        if (!(resolved instanceof PsiMethod))
            return funExpr;
        if (resolved instanceof GrGdkMethod && isFromDGM((GrGdkMethod) resolved) && !GdkMethodUtil.isWithName(((GrGdkMethod) resolved).getStaticMethod().getName())) {
            continue;
        }
        qualifier = ((GrReferenceExpression) funExpr).getQualifierExpression();
        if (qualifier != null)
            return qualifier;
    }
    return null;
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with GrGdkMethod

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

the class GroovyExtractMethodHandler method findConflicts.

private static boolean findConflicts(InitialInfo info) {
    //new ConflictsDialog()
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PsiElement declarationOwner = info.getContext().getParent();
    GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
            PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
            if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
                conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
            }
        }

        //skip 'print' and 'println'
        private boolean skipResult(GroovyResolveResult result) {
            PsiElement element = result.getElement();
            if (element instanceof PsiMethod) {
                String name = ((PsiMethod) element).getName();
                if (!name.startsWith("print"))
                    return false;
                if (element instanceof GrGdkMethod)
                    element = ((GrGdkMethod) element).getStaticMethod();
                PsiClass aClass = ((PsiMethod) element).getContainingClass();
                if (aClass != null) {
                    String qname = aClass.getQualifiedName();
                    return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
                }
            }
            return false;
        }
    };
    GrStatement[] statements = info.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    if (conflicts.isEmpty())
        return false;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
    }
    ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
    dialog.show();
    return !dialog.isOK();
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

Example 20 with GrGdkMethod

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

the class ExpressionGenerator method invokeMethodOn.

public void invokeMethodOn(@NotNull PsiMethod method, @Nullable GrExpression caller, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closures, @NotNull PsiSubstitutor substitutor, @NotNull GroovyPsiElement context) {
    if (method instanceof GrGdkMethod) {
        if (CustomMethodInvocator.invokeMethodOn(this, (GrGdkMethod) method, caller, exprs, namedArgs, closures, substitutor, context))
            return;
        GrExpression[] newArgs = new GrExpression[exprs.length + 1];
        System.arraycopy(exprs, 0, newArgs, 1, exprs.length);
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            newArgs[0] = factory.createExpressionFromText("null");
        } else {
            if (caller == null) {
                caller = factory.createExpressionFromText("this", context);
            }
            newArgs[0] = caller;
        }
        invokeMethodOn(((GrGdkMethod) method).getStaticMethod(), null, newArgs, namedArgs, closures, substitutor, context);
        return;
    }
    if (method.hasModifierProperty(PsiModifier.STATIC) && caller == null) {
        final PsiClass containingClass = method.getContainingClass();
        if (containingClass != null && !PsiTreeUtil.isAncestor(containingClass, context, true)) {
            builder.append(containingClass.getQualifiedName()).append('.');
        }
    } else {
        if (caller != null) {
            final boolean castNeeded = GenerationUtil.isCastNeeded(caller, method, this.context);
            if (castNeeded) {
                writeCastForMethod(caller, method, context);
            }
            caller.accept(this);
            if (castNeeded) {
                builder.append(')');
            }
            builder.append('.');
        }
    }
    builder.append(method.getName());
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor);
    new ArgumentListGenerator(builder, this.context).generate(signature, exprs, namedArgs, closures, context);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Aggregations

GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)22 Nullable (org.jetbrains.annotations.Nullable)8 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)5 NotNull (org.jetbrains.annotations.NotNull)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 PsiMethod (com.intellij.psi.PsiMethod)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)3 NameHint (com.intellij.psi.scope.NameHint)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrTupleType (org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType)2 ASTNode (com.intellij.lang.ASTNode)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1