Search in sources :

Example 1 with GrConstructorInvocation

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

the class GrIntroduceFieldDialog method isAlwaysInvokedConstructor.

private static boolean isAlwaysInvokedConstructor(@Nullable PsiMethod method, @NotNull PsiClass clazz) {
    if (method == null)
        return false;
    if (!method.isConstructor())
        return false;
    final PsiMethod[] constructors = clazz.getConstructors();
    if (constructors.length == 1)
        return true;
    final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) method);
    if (invocation != null && invocation.isThisCall())
        return false;
    for (PsiMethod constructor : constructors) {
        if (constructor == method)
            continue;
        final GrConstructorInvocation inv = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
        if (inv == null || inv.isSuperCall())
            return false;
    }
    return true;
}
Also used : GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation)

Example 2 with GrConstructorInvocation

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

the class StubGenerator method resolveChainingConstructor.

@Nullable
private static GroovyResolveResult resolveChainingConstructor(GrMethod constructor) {
    LOG.assertTrue(constructor.isConstructor());
    final GrConstructorInvocation constructorInvocation = PsiImplUtil.getChainingConstructorInvocation(constructor);
    if (constructorInvocation == null) {
        return null;
    }
    GroovyResolveResult resolveResult = constructorInvocation.advancedResolve();
    if (resolveResult.getElement() != null) {
        return resolveResult;
    }
    final GroovyResolveResult[] results = constructorInvocation.multiResolve(false);
    if (results.length > 0) {
        int i = 0;
        while (results.length > i + 1) {
            final PsiMethod candidate = (PsiMethod) results[i].getElement();
            final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(constructor.getProject()).getResolveHelper();
            if (candidate != null && candidate != constructor && resolveHelper.isAccessible(candidate, constructorInvocation, null)) {
                break;
            }
            i++;
        }
        return results[i];
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrConstructorInvocation

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

the class GrSuperReferenceResolver method resolveSuperExpression.

@Nullable("null if ref is not 'super' reference")
public static GroovyResolveResult[] resolveSuperExpression(@NotNull GrReferenceExpression ref) {
    GrExpression qualifier = ref.getQualifier();
    if (qualifier == null) {
        final PsiElement parent = ref.getParent();
        if (parent instanceof GrConstructorInvocation) {
            return ((GrConstructorInvocation) parent).multiResolve(false);
        }
        PsiClass aClass = PsiUtil.getContextClass(ref);
        if (aClass != null) {
            return getSuperClass(aClass);
        }
    } else if (qualifier instanceof GrReferenceExpression) {
        GroovyResolveResult result = ((GrReferenceExpression) qualifier).advancedResolve();
        PsiElement resolved = result.getElement();
        if (resolved instanceof PsiClass) {
            PsiClass superClass = (PsiClass) resolved;
            GrTypeDefinition scopeClass = PsiTreeUtil.getParentOfType(ref, GrTypeDefinition.class, true);
            if (scopeClass != null && GrTraitUtil.isTrait(superClass) && scopeClass.isInheritor(superClass, false)) {
                PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, scopeClass, PsiSubstitutor.EMPTY);
                return new GroovyResolveResultImpl[] { new GroovyResolveResultImpl(superClass, null, null, superClassSubstitutor, true, true) };
            }
            if (PsiUtil.hasEnclosingInstanceInScope(superClass, ref, false)) {
                return getSuperClass(superClass);
            }
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) PsiSubstitutor(com.intellij.psi.PsiSubstitutor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrConstructorInvocation

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

the class PsiImplUtil method getChainingConstructorInvocation.

@Nullable
public static GrConstructorInvocation getChainingConstructorInvocation(GrMethod constructor) {
    if (constructor instanceof GrReflectedMethod && ((GrReflectedMethod) constructor).getSkippedParameters().length > 0)
        return null;
    LOG.assertTrue(constructor.isConstructor());
    GrOpenBlock body = constructor.getBlock();
    if (body == null)
        return null;
    GrStatement[] statements = body.getStatements();
    if (statements.length > 0 && statements[0] instanceof GrConstructorInvocation) {
        return (GrConstructorInvocation) statements[0];
    }
    return null;
}
Also used : GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GrConstructorInvocation

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

the class GrCreateFieldForParameterIntention method getAnchor.

@Nullable
private static GrStatement getAnchor(GrOpenBlock block) {
    GrStatement[] statements = block.getStatements();
    GrStatement fist = ArrayUtil.getFirstElement(statements);
    if (fist instanceof GrConstructorInvocation) {
        return statements.length > 1 ? statements[1] : null;
    } else {
        return fist;
    }
}
Also used : GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

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