Search in sources :

Example 96 with GroovyResolveResult

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

the class GrReferenceExpressionImpl method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    if (!PsiUtil.isValidReferenceName(newElementName)) {
        final PsiElement old = getReferenceNameElement();
        if (old == null)
            throw new IncorrectOperationException("ref has no name element");
        PsiElement element = GroovyPsiElementFactory.getInstance(getProject()).createStringLiteralForReference(newElementName);
        old.replace(element);
        return this;
    }
    if (PsiUtil.isThisOrSuperRef(this))
        return this;
    final GroovyResolveResult result = advancedResolve();
    if (result.isInvokedOnProperty()) {
        final String name = GroovyPropertyUtils.getPropertyNameByAccessorName(newElementName);
        if (name != null) {
            newElementName = name;
        }
    }
    return super.handleElementRename(newElementName);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 97 with GroovyResolveResult

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

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

the class GrDocFieldReferenceImpl method multiResolveImpl.

@Override
protected ResolveResult[] multiResolveImpl() {
    String name = getReferenceName();
    GrDocReferenceElement holder = getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(GrDocCommentUtil.findDocOwner(this));
    }
    if (resolved instanceof PsiClass) {
        PropertyResolverProcessor processor = new PropertyResolverProcessor(name, this);
        resolved.processDeclarations(processor, ResolveState.initial(), resolved, this);
        GroovyResolveResult[] candidates = processor.getCandidates();
        if (candidates.length == 0) {
            PsiType thisType = JavaPsiFacade.getInstance(getProject()).getElementFactory().createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            MethodResolverProcessor methodProcessor = new MethodResolverProcessor(name, this, false, thisType, null, PsiType.EMPTY_ARRAY);
            MethodResolverProcessor constructorProcessor = new MethodResolverProcessor(name, this, true, thisType, null, PsiType.EMPTY_ARRAY);
            resolved.processDeclarations(methodProcessor, ResolveState.initial(), resolved, this);
            resolved.processDeclarations(constructorProcessor, ResolveState.initial(), resolved, this);
            candidates = ArrayUtil.mergeArrays(methodProcessor.getCandidates(), constructorProcessor.getCandidates());
            if (candidates.length > 0) {
                candidates = new GroovyResolveResult[] { candidates[0] };
            }
        }
        return candidates;
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor)

Example 99 with GroovyResolveResult

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

the class GrDocParameterReferenceImpl method multiResolve.

@Override
@NotNull
public ResolveResult[] multiResolve(boolean incompleteCode) {
    final String name = getName();
    if (name == null)
        return ResolveResult.EMPTY_ARRAY;
    ArrayList<GroovyResolveResult> candidates = new ArrayList<>();
    final PsiElement owner = GrDocCommentUtil.findDocOwner(this);
    if (owner instanceof GrMethod) {
        final GrMethod method = (GrMethod) owner;
        final GrParameter[] parameters = method.getParameters();
        for (GrParameter parameter : parameters) {
            if (name.equals(parameter.getName())) {
                candidates.add(new GroovyResolveResultImpl(parameter, true));
            }
        }
        return candidates.toArray(new ResolveResult[candidates.size()]);
    } else {
        final PsiElement firstChild = getFirstChild();
        if (owner instanceof GrTypeParameterListOwner && firstChild != null) {
            final ASTNode node = firstChild.getNode();
            if (node != null && GroovyDocTokenTypes.mGDOC_TAG_VALUE_LT.equals(node.getElementType())) {
                final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
                for (PsiTypeParameter typeParameter : typeParameters) {
                    if (name.equals(typeParameter.getName())) {
                        candidates.add(new GroovyResolveResultImpl(typeParameter, true));
                    }
                }
            }
        }
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ASTNode(com.intellij.lang.ASTNode) GrTypeParameterListOwner(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterListOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 100 with GroovyResolveResult

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

the class ControlFlowBuilderUtil method resolveNonQualifiedRefWithoutFlow.

@NotNull
public static GroovyResolveResult[] resolveNonQualifiedRefWithoutFlow(@NotNull GrReferenceExpression ref) {
    LOG.assertTrue(!ref.isQualified());
    final String referenceName = ref.getReferenceName();
    final ResolverProcessor processor = new PropertyResolverProcessor(referenceName, ref);
    treeWalkUp(ref, processor);
    final GroovyResolveResult[] candidates = processor.getCandidates();
    if (candidates.length != 0) {
        return candidates;
    }
    return GroovyResolveResult.EMPTY_ARRAY;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor) 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