Search in sources :

Example 6 with GrConstructorInvocation

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

the class GroovyIntroduceParameterMethodUsagesProcessor method processAddSuperCall.

@Override
public boolean processAddSuperCall(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
        return true;
    GrMethod constructor = (GrMethod) usage.getElement();
    if (!constructor.isConstructor())
        return true;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
    GrConstructorInvocation superCall = factory.createConstructorInvocation("super();");
    GrOpenBlock body = constructor.getBlock();
    final GrStatement[] statements = body.getStatements();
    if (statements.length > 0) {
        superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, statements[0]);
    } else {
        superCall = (GrConstructorInvocation) body.addStatementBefore(superCall, null);
    }
    processChangeMethodUsage(data, new UsageInfo(superCall), usages);
    return false;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) UsageInfo(com.intellij.usageView.UsageInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 7 with GrConstructorInvocation

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

the class StubGenerator method writeConstructor.

@Override
public void writeConstructor(final StringBuilder text, PsiMethod constructor, boolean isEnum) {
    LOG.assertTrue(constructor.isConstructor());
    if (!isEnum) {
        text.append("public ");
    //writeModifiers(text, constructor.getModifierList(), JAVA_MODIFIERS);
    }
    /************* name **********/
    //append constructor name
    text.append(constructor.getName());
    /************* parameters **********/
    GenerationUtil.writeParameterList(text, constructor.getParameterList().getParameters(), classNameProvider, null);
    final Set<String> throwsTypes = collectThrowsTypes(constructor, new THashSet<>());
    if (!throwsTypes.isEmpty()) {
        text.append("throws ").append(StringUtil.join(throwsTypes, ", ")).append(' ');
    }
    /************* body **********/
    text.append("{\n");
    if (constructor instanceof GrReflectedMethod) {
        constructor = ((GrReflectedMethod) constructor).getBaseMethod();
    }
    if (constructor instanceof GrMethod) {
        final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
        if (invocation != null) {
            final GroovyResolveResult resolveResult = resolveChainingConstructor((GrMethod) constructor);
            if (resolveResult != null) {
                text.append(invocation.isSuperCall() ? "super(" : "this(");
                writeStubConstructorInvocation(text, (PsiMethod) resolveResult.getElement(), resolveResult.getSubstitutor(), invocation);
                text.append(");");
            }
        } else if (constructor instanceof LightElement) {
            writeStubConstructorInvocation(constructor, text);
        }
    }
    text.append("\n}\n");
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LightElement(com.intellij.psi.impl.light.LightElement)

Aggregations

GrConstructorInvocation (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation)7 Nullable (org.jetbrains.annotations.Nullable)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 GrReflectedMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod)2 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 PsiSubstitutor (com.intellij.psi.PsiSubstitutor)1 LightElement (com.intellij.psi.impl.light.LightElement)1 MethodSignatureBackedByPsiMethod (com.intellij.psi.util.MethodSignatureBackedByPsiMethod)1 UsageInfo (com.intellij.usageView.UsageInfo)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1 GroovyResolveResultImpl (org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)1