Search in sources :

Example 81 with GroovyResolveResult

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

the class GroovyReferenceCopyPasteProcessor method addReferenceData.

@Override
protected void addReferenceData(PsiFile file, int startOffset, PsiElement element, ArrayList<ReferenceData> to) {
    if (element instanceof GrReferenceElement) {
        if (((GrReferenceElement) element).getQualifier() == null) {
            final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
            final PsiElement refElement = resolveResult.getElement();
            if (refElement != null) {
                if (refElement instanceof PsiClass) {
                    if (refElement.getContainingFile() != element.getContainingFile()) {
                        final String qName = ((PsiClass) refElement).getQualifiedName();
                        if (qName != null) {
                            addReferenceData(element, to, startOffset, qName, null);
                        }
                    }
                } else if (resolveResult.getCurrentFileResolveContext() instanceof GrImportStatement && ((GrImportStatement) resolveResult.getCurrentFileResolveContext()).isStatic()) {
                    final String classQName = ((PsiMember) refElement).getContainingClass().getQualifiedName();
                    final String name = ((PsiNamedElement) refElement).getName();
                    if (classQName != null && name != null) {
                        addReferenceData(element, to, startOffset, classQName, name);
                    }
                }
            }
        }
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 82 with GroovyResolveResult

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

the class GrIntroduceClosureParameterProcessor method processExternalUsage.

private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
    final PsiElement element = usage.getElement();
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
    if (callExpression == null) {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
            callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
        }
    }
    if (callExpression == null)
        return;
    //check for x.getFoo()(args)
    if (callExpression instanceof GrMethodCall) {
        final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
        if (invoked instanceof GrReferenceExpression) {
            final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
            final PsiElement resolved = result.getElement();
            if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
                PsiElement actualCallExpression = callExpression.getParent();
                if (actualCallExpression instanceof GrCall) {
                    callExpression = (GrCall) actualCallExpression;
                }
            }
        }
    }
    GrArgumentList argList = callExpression.getArgumentList();
    LOG.assertTrue(argList != null);
    GrExpression[] oldArgs = argList.getExpressionArguments();
    GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
    if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
        argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
    } else {
        PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newarg can be replaced by OldReferenceResolve
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
    } else {
        GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver)

Example 83 with GroovyResolveResult

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

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

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

the class StubGenerator method collectThrowsTypes.

private Set<String> collectThrowsTypes(PsiMethod constructor, Set<PsiMethod> visited) {
    LOG.assertTrue(constructor.isConstructor());
    final GroovyResolveResult resolveResult = constructor instanceof GrMethod ? resolveChainingConstructor((GrMethod) constructor) : null;
    if (resolveResult == null) {
        return Collections.emptySet();
    }
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    final PsiMethod chainedConstructor = (PsiMethod) resolveResult.getElement();
    assert chainedConstructor != null;
    if (!visited.add(chainedConstructor)) {
        return Collections.emptySet();
    }
    final Set<String> result = ContainerUtil.newTroveSet(ArrayUtil.EMPTY_STRING_ARRAY);
    for (PsiClassType type : chainedConstructor.getThrowsList().getReferencedTypes()) {
        StringBuilder builder = new StringBuilder();
        TypeWriter.writeType(builder, substitutor.substitute(type), constructor, classNameProvider);
        result.add(builder.toString());
    }
    if (chainedConstructor instanceof GrMethod) {
        LOG.assertTrue(chainedConstructor.isConstructor());
        result.addAll(collectThrowsTypes(chainedConstructor, visited));
    }
    return result;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

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