Search in sources :

Example 6 with GrAnonymousClassDefinition

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

the class ReplaceAbstractClassInstanceByMapIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
    final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
    final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
    final PsiElement resolved = ref.resolve();
    // && ((PsiClass)resolved).isInterface();
    assert resolved instanceof PsiClass;
    GrTypeDefinitionBody body = anonymous.getBody();
    assert body != null;
    List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
    for (GrMethod method : body.getMethods()) {
        methods.add(new Pair<>(method, method.getBlock()));
    }
    final PsiClass iface = (PsiClass) resolved;
    final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
    for (CandidateInfo info : collection) {
        methods.add(new Pair<>((PsiMethod) info.getElement(), null));
    }
    StringBuilder buffer = new StringBuilder();
    if (methods.size() == 1) {
        final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
        appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
        if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
            buffer.append(" as ").append(iface.getQualifiedName());
        }
    } else {
        buffer.append("[");
        buffer.append("\n");
        for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
            final PsiMethod method = pair.getFirst();
            final GrOpenBlock block = pair.getSecond();
            buffer.append(method.getName()).append(": ");
            appendClosureTextByMethod(method, buffer, block, newExpr);
            buffer.append(",\n");
        }
        if (!methods.isEmpty()) {
            buffer.delete(buffer.length() - 2, buffer.length());
            buffer.append('\n');
        }
        buffer.append("]");
        buffer.append(" as ").append(iface.getQualifiedName());
    }
    createAndAdjustNewExpression(project, newExpr, buffer);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair)

Example 7 with GrAnonymousClassDefinition

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

the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.

private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
    PsiElement parent = element.getParent();
    if (parent instanceof GrAnonymousClassDefinition) {
        parent = parent.getParent();
    }
    if (parent instanceof GrNewExpression) {
        return newExpressionProcessor.process((GrNewExpression) parent);
    }
    if (parent instanceof GrTypeElement) {
        final GrTypeElement typeElement = (GrTypeElement) parent;
        final PsiElement grandpa = typeElement.getParent();
        if (grandpa instanceof GrVariableDeclaration) {
            final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
            if (vars.length == 1) {
                final GrVariable variable = vars[0];
                if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
                    return false;
                }
            }
        } else if (grandpa instanceof GrMethod) {
            final GrMethod method = (GrMethod) grandpa;
            if (typeElement == method.getReturnTypeElementGroovy()) {
                ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {

                    @Override
                    public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
                        if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
                            return false;
                        }
                        return true;
                    }
                });
            }
        } else if (grandpa instanceof GrTypeCastExpression) {
            final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        } else if (grandpa instanceof GrSafeCastExpression) {
            final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with GrAnonymousClassDefinition

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

the class ResolveUtil method getCallVariants.

public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
    final PsiElement parent = place.getParent();
    GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
    if (parent instanceof GrCallExpression) {
        variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
    } else if (parent instanceof GrConstructorInvocation) {
        final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
        if (clazz != null) {
            final PsiMethod[] constructors = clazz.getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
        if (element instanceof PsiClass) {
            final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (place instanceof GrReferenceExpression) {
        variants = ((GrReferenceExpression) place).getSameNameVariants();
    }
    return variants;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 9 with GrAnonymousClassDefinition

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

the class SuppressForMemberFix method getContainer.

@Override
@Nullable
public GrDocCommentOwner getContainer(final PsiElement context) {
    if (context == null || context instanceof PsiFile) {
        return null;
    }
    GrDocCommentOwner container = null;
    GrDocComment docComment = PsiTreeUtil.getParentOfType(context, GrDocComment.class);
    if (docComment != null) {
        container = docComment.getOwner();
    }
    if (container == null) {
        container = PsiTreeUtil.getParentOfType(context, GrDocCommentOwner.class);
    }
    while (container instanceof GrAnonymousClassDefinition || container instanceof GrTypeParameter) {
        container = PsiTreeUtil.getParentOfType(container, GrDocCommentOwner.class);
        if (container == null)
            return null;
    }
    if (myForClass) {
        while (container != null) {
            final GrTypeDefinition parentClass = PsiTreeUtil.getParentOfType(container, GrTypeDefinition.class);
            if (parentClass == null && container instanceof GrTypeDefinition) {
                return container;
            }
            container = parentClass;
        }
    }
    return container;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrTypeParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrAnonymousClassDefinition

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

the class EachToForIntention method updateReturnStatements.

private static GrForStatement updateReturnStatements(GrForStatement forStatement) {
    GrStatement body = forStatement.getBody();
    assert body != null;
    final Set<String> usedLabels = ContainerUtil.newHashSet();
    final Ref<Boolean> needLabel = Ref.create(false);
    body.accept(new GroovyRecursiveElementVisitor() {

        private int myLoops = 0;

        @Override
        public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
            if (returnStatement.getReturnValue() != null)
                return;
            if (myLoops > 0)
                needLabel.set(true);
        }

        @Override
        public void visitLabeledStatement(@NotNull GrLabeledStatement labeledStatement) {
            super.visitLabeledStatement(labeledStatement);
            usedLabels.add(labeledStatement.getName());
        }

        @Override
        public void visitForStatement(@NotNull GrForStatement forStatement) {
            myLoops++;
            super.visitForStatement(forStatement);
            myLoops--;
        }

        @Override
        public void visitWhileStatement(@NotNull GrWhileStatement whileStatement) {
            myLoops++;
            super.visitWhileStatement(whileStatement);
            myLoops--;
        }

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
        //don't go into closures
        }

        @Override
        public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
        //don't go into anonymous
        }
    });
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(forStatement.getProject());
    final String continueText;
    if (needLabel.get()) {
        int i = 0;
        String label = OUTER;
        while (usedLabels.contains(label)) {
            label = OUTER + i;
            i++;
        }
        continueText = "continue " + label;
        GrLabeledStatement labeled = (GrLabeledStatement) factory.createStatementFromText(label + ": while (true){}");
        labeled.getStatement().replaceWithStatement(forStatement);
        labeled = forStatement.replaceWithStatement(labeled);
        forStatement = (GrForStatement) labeled.getStatement();
        body = forStatement.getBody();
        assert body != null;
    } else {
        continueText = "continue";
    }
    final GrStatement continueStatement = factory.createStatementFromText(continueText);
    body.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
            if (returnStatement.getReturnValue() == null) {
                returnStatement.replaceWithStatement(continueStatement);
            }
        }

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
        //don't go into closures
        }

        @Override
        public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
        //don't go into anonymous
        }
    });
    return forStatement;
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

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