Search in sources :

Example 31 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature 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)

Example 32 with GrClosureSignature

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

the class ExpressionGenerator method invokeMethodOn.

public void invokeMethodOn(@NotNull PsiMethod method, @Nullable GrExpression caller, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closures, @NotNull PsiSubstitutor substitutor, @NotNull GroovyPsiElement context) {
    if (method instanceof GrGdkMethod) {
        if (CustomMethodInvocator.invokeMethodOn(this, (GrGdkMethod) method, caller, exprs, namedArgs, closures, substitutor, context))
            return;
        GrExpression[] newArgs = new GrExpression[exprs.length + 1];
        System.arraycopy(exprs, 0, newArgs, 1, exprs.length);
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            newArgs[0] = factory.createExpressionFromText("null");
        } else {
            if (caller == null) {
                caller = factory.createExpressionFromText("this", context);
            }
            newArgs[0] = caller;
        }
        invokeMethodOn(((GrGdkMethod) method).getStaticMethod(), null, newArgs, namedArgs, closures, substitutor, context);
        return;
    }
    if (method.hasModifierProperty(PsiModifier.STATIC) && caller == null) {
        final PsiClass containingClass = method.getContainingClass();
        if (containingClass != null && !PsiTreeUtil.isAncestor(containingClass, context, true)) {
            builder.append(containingClass.getQualifiedName()).append('.');
        }
    } else {
        if (caller != null) {
            final boolean castNeeded = GenerationUtil.isCastNeeded(caller, method, this.context);
            if (castNeeded) {
                writeCastForMethod(caller, method, context);
            }
            caller.accept(this);
            if (castNeeded) {
                builder.append(')');
            }
            builder.append('.');
        }
    }
    builder.append(method.getName());
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor);
    new ArgumentListGenerator(builder, this.context).generate(signature, exprs, namedArgs, closures, context);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 33 with GrClosureSignature

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

the class ClassItemGeneratorImpl method writeEnumConstant.

@Override
public void writeEnumConstant(StringBuilder builder, GrEnumConstant constant) {
    GenerationUtil.writeDocComment(builder, constant, false);
    builder.append(constant.getName());
    final GrArgumentList argumentList = constant.getArgumentList();
    if (argumentList != null) {
        final GroovyResolveResult resolveResult = constant.advancedResolve();
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(resolveResult);
        new ArgumentListGenerator(builder, context.extend()).generate(signature, argumentList.getExpressionArguments(), argumentList.getNamedArguments(), GrClosableBlock.EMPTY_ARRAY, constant);
    }
    final GrEnumConstantInitializer anonymousBlock = constant.getInitializingClass();
    if (anonymousBlock != null) {
        builder.append("{\n");
        new ClassGenerator(classNameProvider, this).writeMembers(builder, anonymousBlock);
        builder.append("\n}");
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrEnumConstantInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumConstantInitializer) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Aggregations

GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 ArrayList (java.util.ArrayList)6 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)6 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)6 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 MethodSignature (com.intellij.psi.util.MethodSignature)4 List (java.util.List)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)4 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)4 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 Pair (com.intellij.openapi.util.Pair)3 TIntProcedure (gnu.trove.TIntProcedure)3