Search in sources :

Example 16 with GrAnonymousClassDefinition

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

the class GrIntroduceParameterProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    ArrayList<UsageInfo> result = new ArrayList<>();
    final PsiMethod toSearchFor = ((PsiMethod) mySettings.getToSearchFor());
    if (!mySettings.generateDelegate()) {
        Collection<PsiReference> refs = MethodReferencesSearch.search(toSearchFor, GlobalSearchScope.projectScope(myProject), true).findAll();
        for (PsiReference ref1 : refs) {
            PsiElement ref = ref1.getElement();
            if (ref instanceof PsiMethod && ((PsiMethod) ref).isConstructor()) {
                DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((PsiMethod) ref, ((PsiMethod) ref).getContainingClass(), toSearchFor);
                result.add(implicitUsageInfo);
            } else if (ref instanceof PsiClass) {
                if (ref instanceof GrAnonymousClassDefinition) {
                    result.add(new ExternalUsageInfo(((GrAnonymousClassDefinition) ref).getBaseClassReferenceGroovy()));
                } else if (ref instanceof PsiAnonymousClass) {
                    result.add(new ExternalUsageInfo(((PsiAnonymousClass) ref).getBaseClassReference()));
                } else {
                    result.add(new NoConstructorClassUsageInfo((PsiClass) ref));
                }
            } else if (!PsiTreeUtil.isAncestor(mySettings.getToReplaceIn(), ref, false)) {
                result.add(new ExternalUsageInfo(ref));
            } else {
                result.add(new ChangedMethodCallInfo(ref));
            }
        }
    }
    if (mySettings.replaceAllOccurrences()) {
        if (mySettings.getVar() != null) {
            for (PsiElement element : GrIntroduceHandlerBase.collectVariableUsages(mySettings.getVar(), mySettings.getToReplaceIn())) {
                result.add(new InternalUsageInfo(element));
            }
        } else {
            PsiElement[] exprs = GroovyIntroduceParameterUtil.getOccurrences(mySettings);
            for (PsiElement expr : exprs) {
                result.add(new InternalUsageInfo(expr));
            }
        }
    } else {
        if (mySettings.getExpression() != null) {
            result.add(new InternalUsageInfo(mySettings.getExpression()));
        }
    }
    Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(toSearchFor).findAll();
    for (PsiMethod overridingMethod : overridingMethods) {
        result.add(new UsageInfo(overridingMethod));
    }
    final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) ArrayList(java.util.ArrayList) TIntArrayList(gnu.trove.TIntArrayList) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with GrAnonymousClassDefinition

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

the class GrTypeDefinitionRangeHandler method getDeclarationRange.

@NotNull
@Override
public TextRange getDeclarationRange(@NotNull GrTypeDefinition aClass) {
    if (aClass instanceof GrAnonymousClassDefinition) {
        GrNewExpression call = (GrNewExpression) aClass.getParent();
        int startOffset = call.getTextRange().getStartOffset();
        int endOffset = call.getArgumentList().getTextRange().getEndOffset();
        return new TextRange(startOffset, endOffset);
    } else {
        final PsiModifierList modifierList = aClass.getModifierList();
        int startOffset = modifierList == null ? aClass.getTextRange().getStartOffset() : modifierList.getTextRange().getStartOffset();
        final GrImplementsClause implementsList = aClass.getImplementsClause();
        int endOffset = implementsList == null ? aClass.getTextRange().getEndOffset() : implementsList.getTextRange().getEndOffset();
        return new TextRange(startOffset, endOffset);
    }
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrImplementsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) TextRange(com.intellij.openapi.util.TextRange) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with GrAnonymousClassDefinition

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

the class ExpressionGenerator method visitNewExpression.

@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
    boolean hasFieldInitialization = hasFieldInitialization(newExpression);
    StringBuilder builder;
    final PsiType type = newExpression.getType();
    final String varName;
    if (hasFieldInitialization) {
        builder = new StringBuilder();
        varName = GenerationUtil.suggestVarName(type, newExpression, context);
        TypeWriter.writeType(builder, type, newExpression);
        builder.append(' ').append(varName).append(" = ");
    } else {
        varName = null;
        builder = this.builder;
    }
    final GrTypeElement typeElement = newExpression.getTypeElement();
    final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
    final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
    builder.append("new ");
    if (typeElement != null) {
        final PsiType builtIn = typeElement.getType();
        LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
        final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
        TypeWriter.writeTypeForNew(builder, boxed, newExpression);
    } else if (referenceElement != null) {
        GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
    }
    final GrArgumentList argList = newExpression.getArgumentList();
    if (argList != null) {
        GrClosureSignature signature = null;
        final GroovyResolveResult resolveResult = newExpression.advancedResolve();
        final PsiElement constructor = resolveResult.getElement();
        if (constructor instanceof PsiMethod) {
            signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
        } else if (referenceElement != null) {
            final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
            final PsiElement clazz = clazzResult.getElement();
            if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
                signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
            }
        }
        final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
        new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
    }
    final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
    if (anonymous != null) {
        writeTypeBody(builder, anonymous);
    }
    if (arrayDeclaration != null) {
        final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
        for (GrExpression boundExpression : boundExpressions) {
            builder.append('[');
            boundExpression.accept(this);
            builder.append(']');
        }
        if (boundExpressions.length == 0) {
            builder.append("[]");
        }
    }
    if (hasFieldInitialization) {
        builder.append(';');
        context.myStatements.add(builder.toString());
        final GrNamedArgument[] namedArguments = argList.getNamedArguments();
        for (GrNamedArgument namedArgument : namedArguments) {
            final String fieldName = namedArgument.getLabelName();
            if (fieldName == null) {
                //todo try to initialize field
                final GrArgumentLabel label = namedArgument.getLabel();
                LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
            } else {
                final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
                final PsiElement resolved = resolveResult.getElement();
                LOG.assertTrue(resolved instanceof PsiClass);
                initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
            }
        }
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

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