Search in sources :

Example 1 with GrAnonymousClassDefinition

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

the class GrRefactoringConflictsUtil method checkUsedElements.

public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
    final Set<PsiMember> moving = new HashSet<>(membersToMove);
    if (abstractMethods != null) {
        moving.addAll(abstractMethods);
    }
    if (scope instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                GrExpression qualifier = refExpr.getQualifierExpression();
                PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
            }
        }
    } else if (scope instanceof GrNewExpression) {
        final GrNewExpression newExpression = (GrNewExpression) scope;
        final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
        if (anonymousClass != null) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
            }
        } else {
            final PsiMethod refElement = newExpression.resolveMethod();
            if (refElement != null) {
                if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                    RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
                }
            }
        }
    } else if (scope instanceof GrCodeReferenceElement) {
        GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
            }
        }
    }
    for (PsiElement child : scope.getChildren()) {
        if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
            continue;
        checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
    }
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 2 with GrAnonymousClassDefinition

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

the class GrCallReferenceProcessor method process.

@Override
public boolean process(@NotNull PsiReference reference, @NotNull JavaCallHierarchyData data) {
    PsiClass originalClass = data.getOriginalClass();
    PsiMethod method = data.getMethod();
    Set<PsiMethod> methodsToFind = data.getMethodsToFind();
    PsiMethod methodToFind = data.getMethodToFind();
    PsiClassType originalType = data.getOriginalType();
    Map<PsiMember, NodeDescriptor> methodToDescriptorMap = data.getResultMap();
    Project project = data.getProject();
    if (reference instanceof GrReferenceExpression) {
        final GrExpression qualifier = ((GrReferenceExpression) reference).getQualifierExpression();
        if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(qualifier)) {
            // filter super.foo() call inside foo() and similar cases (bug 8411)
            assert qualifier != null;
            final PsiClass superClass = PsiUtil.resolveClassInType(qualifier.getType());
            if (originalClass == null || originalClass.isInheritor(superClass, true)) {
                return true;
            }
        }
        if (qualifier != null && !methodToFind.hasModifierProperty(PsiModifier.STATIC)) {
            final PsiType qualifierType = qualifier.getType();
            if (qualifierType instanceof PsiClassType && !TypeConversionUtil.isAssignable(qualifierType, originalType) && methodToFind != method) {
                final PsiClass psiClass = ((PsiClassType) qualifierType).resolve();
                if (psiClass != null) {
                    final PsiMethod callee = psiClass.findMethodBySignature(methodToFind, true);
                    if (callee != null && !methodsToFind.contains(callee)) {
                        // skip sibling methods
                        return true;
                    }
                }
            }
        }
    } else {
        if (!(reference instanceof PsiElement)) {
            return true;
        }
        final PsiElement parent = ((PsiElement) reference).getParent();
        if (parent instanceof PsiNewExpression) {
            if (((PsiNewExpression) parent).getClassReference() != reference) {
                return true;
            }
        } else if (parent instanceof GrAnonymousClassDefinition) {
            if (((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy() != reference) {
                return true;
            }
        } else {
            return true;
        }
    }
    final PsiElement element = reference.getElement();
    final PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);
    synchronized (methodToDescriptorMap) {
        CallHierarchyNodeDescriptor d = (CallHierarchyNodeDescriptor) methodToDescriptorMap.get(key);
        if (d == null) {
            d = new CallHierarchyNodeDescriptor(project, (CallHierarchyNodeDescriptor) data.getNodeDescriptor(), element, false, true);
            methodToDescriptorMap.put(key, d);
        } else if (!d.hasReference(reference)) {
            d.incrementUsageCount();
        }
        d.addReference(reference);
    }
    return true;
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor)

Example 3 with GrAnonymousClassDefinition

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

the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.

private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod overridingMethod : overridingMethods) {
        if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
            result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
        }
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
                continue;
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (PsiUtil.isMethodUsage(element)) {
                result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
            } else if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(ref.getElement()));
            } else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
                DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
                result.add(implicitUsageInfo);
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (psiClass instanceof GrAnonymousClassDefinition) {
                    result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
                    continue;
                }
                /*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
            continue;
          }
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
            continue;
          }*/
                result.add(new NoConstructorClassUsageInfo(psiClass));
            } else if (ref instanceof PsiCallReference) {
                result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
            } else {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    } else if (myChangeInfo.isParameterTypesChanged()) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference reference : refs) {
            final PsiElement element = reference.getElement();
            if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    if (method instanceof GrMethod) {
        detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
    }
    for (final PsiMethod overridingMethod : overridingMethods) {
        if (overridingMethod instanceof GrMethod) {
            detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
        }
    }
    return overridingMethods;
}
Also used : GrDocTagValueToken(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTagValueToken) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 4 with GrAnonymousClassDefinition

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

the class GroovySpacingProcessor method visitTypeDefinition.

@Override
public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
    if (myType2 == GroovyElementTypes.CLASS_BODY) {
        if (typeDefinition instanceof GrAnonymousClassDefinition) {
            //don't manually remove line feeds because this line is ambiguous
            createSpaceProperty(mySettings.SPACE_BEFORE_CLASS_LBRACE, true, 100);
        } else {
            PsiElement nameIdentifier = typeDefinition.getNameIdentifierGroovy();
            int dependenceStart = nameIdentifier.getTextRange().getStartOffset();
            final TextRange range = new TextRange(dependenceStart, myChild1.getTextRange().getEndOffset());
            createSpaceBeforeLBrace(mySettings.SPACE_BEFORE_CLASS_LBRACE, mySettings.CLASS_BRACE_STYLE, range, false);
        }
    } else if (myType2 == GroovyElementTypes.TYPE_PARAMETER_LIST) {
        createSpaceInCode(false);
    } else if (myType2 == GroovyElementTypes.ARGUMENTS) {
        manageSpaceBeforeCallLParenth();
    }
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) TextRange(com.intellij.openapi.util.TextRange) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GrAnonymousClassDefinition

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

the class GroovySpacingProcessorBasic method getSpacing.

public static Spacing getSpacing(GroovyBlock child1, GroovyBlock child2, FormattingContext context) {
    ASTNode leftNode = child1.getNode();
    ASTNode rightNode = child2.getNode();
    final PsiElement left = leftNode.getPsi();
    final PsiElement right = rightNode.getPsi();
    IElementType leftType = leftNode.getElementType();
    IElementType rightType = rightNode.getElementType();
    final CommonCodeStyleSettings settings = context.getSettings();
    final GroovyCodeStyleSettings groovySettings = context.getGroovySettings();
    if (!(mirrorsAst(child1) && mirrorsAst(child2))) {
        return NO_SPACING;
    }
    if (child2 instanceof ClosureBodyBlock) {
        return settings.SPACE_WITHIN_BRACES ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (child1 instanceof ClosureBodyBlock) {
        return createDependentSpacingForClosure(settings, groovySettings, (GrClosableBlock) left.getParent(), false);
    }
    if (leftType == GroovyDocElementTypes.GROOVY_DOC_COMMENT) {
        return COMMON_SPACING_WITH_NL;
    }
    if (right instanceof GrTypeArgumentList) {
        return NO_SPACING_WITH_NEWLINE;
    }
    /********** punctuation marks ************/
    if (GroovyTokenTypes.mCOMMA == leftType) {
        return settings.SPACE_AFTER_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mCOMMA == rightType) {
        return settings.SPACE_BEFORE_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == leftType) {
        return settings.SPACE_AFTER_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == rightType) {
        return settings.SPACE_BEFORE_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    // For dots, commas etc.
    if ((TokenSets.DOTS.contains(rightType)) || (GroovyTokenTypes.mCOLON.equals(rightType) && !(right.getParent() instanceof GrConditionalExpression))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (TokenSets.DOTS.contains(leftType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    //todo:check it for multiple assignments
    if ((GroovyElementTypes.VARIABLE_DEFINITION.equals(leftType) || GroovyElementTypes.VARIABLE_DEFINITION.equals(rightType)) && !(leftNode.getTreeNext() instanceof PsiErrorElement)) {
        return Spacing.createSpacing(0, 0, 1, false, 100);
    }
    // For regexes
    if (leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mREGEX_LITERAL || leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        return NO_SPACING;
    }
    // For << and >> ...
    if ((GroovyTokenTypes.mLT.equals(leftType) && GroovyTokenTypes.mLT.equals(rightType)) || (GroovyTokenTypes.mGT.equals(leftType) && GroovyTokenTypes.mGT.equals(rightType))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    // Unary and postfix expressions
    if (SpacingTokens.PREFIXES.contains(leftType) || SpacingTokens.POSTFIXES.contains(rightType) || (SpacingTokens.PREFIXES_OPTIONAL.contains(leftType) && left.getParent() instanceof GrUnaryExpression)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (SpacingTokens.RANGES.contains(leftType) || SpacingTokens.RANGES.contains(rightType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyDocTokenTypes.mGDOC_ASTERISKS == leftType && GroovyDocTokenTypes.mGDOC_COMMENT_DATA == rightType) {
        String text = rightNode.getText();
        if (!text.isEmpty() && !StringUtil.startsWithChar(text, ' ')) {
            return COMMON_SPACING;
        }
        return NO_SPACING;
    }
    if (leftType == GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) {
        return LAZY_SPACING;
    }
    if (left instanceof GrStatement && right instanceof GrStatement && left.getParent() instanceof GrStatementOwner && right.getParent() instanceof GrStatementOwner) {
        return COMMON_SPACING_WITH_NL;
    }
    if (rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END) {
        return NO_SPACING;
    }
    if ((leftType == GroovyDocElementTypes.GDOC_INLINED_TAG && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) || (leftType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA && rightType == GroovyDocElementTypes.GDOC_INLINED_TAG)) {
        // Keep formatting between groovy doc text and groovy doc reference tag as is.
        return NO_SPACING;
    }
    if (leftType == GroovyElementTypes.CLASS_TYPE_ELEMENT && rightType == GroovyTokenTypes.mTRIPLE_DOT) {
        return NO_SPACING;
    }
    // diamonds
    if (rightType == GroovyTokenTypes.mLT || rightType == GroovyTokenTypes.mGT) {
        if (right.getParent() instanceof GrCodeReferenceElement) {
            PsiElement p = right.getParent().getParent();
            if (p instanceof GrNewExpression || p instanceof GrAnonymousClassDefinition) {
                return NO_SPACING;
            }
        }
    }
    return COMMON_SPACING;
}
Also used : GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IElementType(com.intellij.psi.tree.IElementType) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)18 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)5 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)5 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 UsageInfo (com.intellij.usageView.UsageInfo)3 NotNull (org.jetbrains.annotations.NotNull)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 TextRange (com.intellij.openapi.util.TextRange)2 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)2 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)2 ArrayList (java.util.ArrayList)2 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2