Search in sources :

Example 91 with GrClosableBlock

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

the class OldReferencesResolver method resolveOldReferences.

private void resolveOldReferences(PsiElement expr, PsiElement oldExpr) throws IncorrectOperationException {
    if (expr == null || !expr.isValid() || oldExpr == null)
        return;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    // references continue being resolved in the children of newExpr
    PsiElement newExpr = expr;
    if (oldExpr instanceof GrReferenceExpression) {
        if (isThisReferenceToContainingClass(oldExpr) || isSimpleSuperReference(oldExpr)) {
            if (myInstanceRef != null) {
                newExpr.replace(getInstanceRef(factory));
            }
            return;
        }
        final GrReferenceExpression oldRef = (GrReferenceExpression) oldExpr;
        newExpr = newExpr.replace(decodeReferenceExpression((GrReferenceExpression) newExpr, oldRef));
        //newExpr = ((GrReferenceExpression)newExpr).getReferenceNameElement();
        final GroovyResolveResult adv = oldRef.advancedResolve();
        final PsiElement scope = getClassContainingResolve(adv);
        final PsiElement owner = PsiUtil.getContextClass(oldExpr);
        if (myToReplaceIn instanceof GrClosableBlock || (owner != null && scope != null && PsiTreeUtil.isContextAncestor(owner, scope, false))) {
            final PsiElement subj = adv.getElement();
            // Parameters
            if (subj instanceof PsiParameter) {
                int index = ArrayUtil.indexOf(myParameters, subj);
                if (index < 0)
                    return;
                if (index < myParameters.length) {
                    newExpr = inlineParam(newExpr, getActualArg(index), ((PsiParameter) subj));
                }
            } else // "naked" field and methods  (should become qualified)
            if ((subj instanceof PsiField || subj instanceof PsiMethod) && oldRef.getQualifierExpression() == null) {
                PsiElement newResolved = newExpr instanceof GrReferenceExpression ? ((GrReferenceExpression) newExpr).resolve() : null;
                if (myInstanceRef != null || !subj.getManager().areElementsEquivalent(newResolved, subj)) {
                    boolean isStatic = subj instanceof PsiField && ((PsiField) subj).hasModifierProperty(PsiModifier.STATIC) || subj instanceof PsiMethod && ((PsiMethod) subj).hasModifierProperty(PsiModifier.STATIC);
                    String name = ((PsiNamedElement) subj).getName();
                    boolean shouldBeAt = subj instanceof PsiField && !PsiTreeUtil.isAncestor(((PsiMember) subj).getContainingClass(), newExpr, true) && GroovyPropertyUtils.findGetterForField((PsiField) subj) != null;
                    final GrReferenceExpression fromText = factory.createReferenceExpressionFromText("qualifier." + (shouldBeAt ? "@" : "") + name);
                    if (isStatic) {
                        final GrReferenceExpression qualifier = factory.createReferenceElementForClass(((PsiMember) subj).getContainingClass());
                        newExpr = newExpr.replace(fromText);
                        ((GrReferenceExpression) newExpr).setQualifier(qualifier);
                        newExpr = ((GrReferenceExpression) newExpr).getReferenceNameElement();
                    } else {
                        if (myInstanceRef != null) {
                            GrExpression instanceRef = getInstanceRef(factory);
                            fromText.setQualifier(instanceRef);
                            newExpr = newExpr.replace(fromText);
                            newExpr = ((GrReferenceExpression) newExpr).getReferenceNameElement();
                        }
                    }
                }
            }
            if (subj instanceof PsiField) {
                // probably replacing field with a getter
                if (myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) {
                    if (myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL || myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE && !JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible((PsiMember) subj, newExpr, null)) {
                        newExpr = replaceFieldWithGetter(newExpr, (PsiField) subj);
                    }
                }
            }
        }
    } else {
        PsiClass refClass = oldExpr.getCopyableUserData(ChangeContextUtil.REF_CLASS_KEY);
        if (refClass != null && refClass.isValid()) {
            PsiReference ref = newExpr.getReference();
            if (ref != null) {
                final String qualifiedName = refClass.getQualifiedName();
                if (qualifiedName != null) {
                    if (JavaPsiFacade.getInstance(refClass.getProject()).findClass(qualifiedName, oldExpr.getResolveScope()) != null) {
                        newExpr = ref.bindToElement(refClass);
                    }
                }
            }
        }
    }
    PsiElement[] oldChildren = oldExpr.getChildren();
    PsiElement[] newChildren = newExpr.getChildren();
    if (oldExpr instanceof GrNewExpression && newExpr instanceof GrNewExpression) {
        //special new-expression case
        resolveOldReferences(((GrNewExpression) newExpr).getReferenceElement(), ((GrNewExpression) oldExpr).getReferenceElement());
        resolveOldReferences(((GrNewExpression) newExpr).getArgumentList(), ((GrNewExpression) oldExpr).getArgumentList());
        if (newChildren[1] instanceof GrArrayDeclaration) {
            for (GrExpression expression : ((GrArrayDeclaration) newChildren[1]).getBoundExpressions()) {
                resolveOldReferences(expression, oldChildren[1]);
            }
        }
    } else {
        if (oldExpr instanceof GrReferenceExpression && newExpr instanceof GrReferenceExpression) {
            final GrExpression oldQualifier = ((GrReferenceExpression) oldExpr).getQualifierExpression();
            final GrExpression newQualifier = ((GrReferenceExpression) newExpr).getQualifierExpression();
            if (oldQualifier != null && newQualifier != null) {
                resolveOldReferences(newQualifier, oldQualifier);
                return;
            }
        }
        if (oldChildren.length == newChildren.length) {
            for (int i = 0; i < newChildren.length; i++) {
                resolveOldReferences(newChildren[i], oldChildren[i]);
            }
        }
    }
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 92 with GrClosableBlock

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

the class OldReferencesResolver method inlineParam.

private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
    if (myParamsToNotInline.contains(parameter))
        return newExpr;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    if (myExpr instanceof GrClosableBlock) {
        int count = 0;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
            count++;
            if (count > 1)
                break;
        }
        if (count > 1) {
            myParamsToNotInline.add(parameter);
            final PsiType type;
            if (parameter instanceof GrParameter) {
                type = ((GrParameter) parameter).getDeclaredType();
            } else {
                type = parameter.getType();
            }
            final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
            final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
            GrStatement anchor = statements.length > 0 ? statements[0] : null;
            return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
        }
    }
    int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
    if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
        actualArg = factory.createExpressionFromText(getTempVar(actualArg));
    }
    newExpr = newExpr.replace(actualArg);
    return newExpr;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 93 with GrClosableBlock

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

the class GebUtil method calculateContentElements.

private static Map<String, PsiField> calculateContentElements(@NotNull PsiClass pageOrModuleClass) {
    PsiField contentField = pageOrModuleClass.findFieldByName("content", false);
    if (!(contentField instanceof GrField))
        return Collections.emptyMap();
    GrExpression initializer = ((GrField) contentField).getInitializerGroovy();
    if (!(initializer instanceof GrClosableBlock))
        return Collections.emptyMap();
    Map<String, PsiField> res = new HashMap<>();
    PsiType objectType = PsiType.getJavaLangObject(pageOrModuleClass.getManager(), pageOrModuleClass.getResolveScope());
    for (PsiElement e = initializer.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrMethodCall) {
            GrMethodCall methodCall = (GrMethodCall) e;
            GrExpression invokedExpression = methodCall.getInvokedExpression();
            if (!(invokedExpression instanceof GrReferenceExpression))
                continue;
            if (((GrReferenceExpression) invokedExpression).isQualified())
                continue;
            GrExpression[] arguments = PsiUtil.getAllArguments((GrCall) e);
            if (arguments.length == 0)
                continue;
            final GrClosableBlock block;
            if (arguments.length == 1 && arguments[0] instanceof GrClosableBlock) {
                block = (GrClosableBlock) arguments[0];
            } else if (arguments.length == 2 && arguments[0] == null && arguments[1] instanceof GrClosableBlock) {
                block = (GrClosableBlock) arguments[1];
            } else {
                continue;
            }
            GrLightField field = new GrLightField(pageOrModuleClass, ((GrReferenceExpression) invokedExpression).getReferenceName(), objectType, invokedExpression) {

                @Override
                public PsiType getTypeGroovy() {
                    return block.getReturnType();
                }

                @Override
                public PsiType getDeclaredType() {
                    return null;
                }
            };
            field.getModifierList().addModifier(GrModifierFlags.STATIC_MASK);
            res.put(field.getName(), field);
        }
    }
    return res;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) HashMap(java.util.HashMap) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrLightField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightField)

Example 94 with GrClosableBlock

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

the class GebContentDeclarationSearcher method findDeclarationsAt.

@Override
public void findDeclarationsAt(@NotNull PsiElement element, int offsetInElement, Consumer<PomTarget> consumer) {
    PsiElement grCall = element.getParent();
    if (!(grCall instanceof GrMethodCall))
        return;
    PsiElement grClosure = grCall.getParent();
    if (!(grClosure instanceof GrClosableBlock))
        return;
    PsiElement contentField = grClosure.getParent();
    if (!(contentField instanceof GrField))
        return;
    GrField field = (GrField) contentField;
    if (!"content".equals(field.getName()) || !field.hasModifierProperty(PsiModifier.STATIC))
        return;
    PsiClass containingClass = field.getContainingClass();
    if (!InheritanceUtil.isInheritor(containingClass, "geb.Page") && !InheritanceUtil.isInheritor(containingClass, "geb.Module"))
        return;
    Map<String, PsiField> elements = GebUtil.getContentElements(containingClass);
    for (PsiField f : elements.values()) {
        if (f.getNavigationElement() == element) {
            consumer.consume(f);
            return;
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement)

Example 95 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9