From e283930effc419ce95346b368e99a0e1c44a4aa8 Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Mon, 28 Mar 2011 12:39:35 +0400 Subject: [PATCH] GrNewExpression api --- .../expressions/GrNewExpression.java | 3 ++ .../expressions/GrNewExpressionImpl.java | 32 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/expressions/GrNewExpression.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/expressions/GrNewExpression.java index 8db00b5e3856..fef3d22f2fc7 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/expressions/GrNewExpression.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/expressions/GrNewExpression.java @@ -39,4 +39,7 @@ public interface GrNewExpression extends GrCallExpression, GrConstructorCall { @Nullable GrAnonymousClassDefinition getAnonymousClassDefinition(); + + @Nullable + GrArrayDeclaration getArrayDeclaration(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrNewExpressionImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrNewExpressionImpl.java index 1c738cb0bf50..8c275f2221e1 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrNewExpressionImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrNewExpressionImpl.java @@ -92,8 +92,10 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject()); final GrArgumentList newList = factory.createExpressionArgumentList(); PsiElement last = getLastChild(); + assert last != null; while (last.getPrevSibling() instanceof PsiWhiteSpace || last.getPrevSibling() instanceof PsiErrorElement) { last = last.getPrevSibling(); + assert last != null; } ASTNode astNode = last.getNode(); assert astNode != null; @@ -133,12 +135,16 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx final GroovyResolveResult[] classResults = ref.multiResolve(false); if (classResults.length == 0) return GroovyResolveResult.EMPTY_ARRAY; - if (getNamedArguments().length > 0 && getArgumentList().getExpressionArguments().length == 0) { + final GrArgumentList argumentList = getArgumentList(); + assert argumentList != null; + + if (getNamedArguments().length > 0 && argumentList.getExpressionArguments().length == 0) { GroovyResolveResult[] constructorResults = PsiUtil.getConstructorCandidates(ref, classResults, new PsiType[]{PsiUtil.createMapType( getResolveScope())}); //one Map parameter, actually for (GroovyResolveResult result : constructorResults) { - if (result.getElement() instanceof PsiMethod) { - PsiMethod constructor = (PsiMethod)result.getElement(); + final PsiElement resolved = result.getElement(); + if (resolved instanceof PsiMethod) { + PsiMethod constructor = (PsiMethod)resolved; final PsiParameter[] parameters = constructor.getParameterList().getParameters(); if (parameters.length == 1 && InheritanceUtil.isInheritor(parameters[0].getType(), CommonClassNames.JAVA_UTIL_MAP)) { return constructorResults; @@ -155,11 +161,11 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx } public GroovyResolveResult[] multiResolveClass() { - return getReferenceElement().multiResolve(false); - } - - public PsiMethod resolveConstructor() { - return PsiImplUtil.extractUniqueElement(multiResolveConstructor()); + final GrCodeReferenceElement referenceElement = getReferenceElement(); + if (referenceElement != null) { + return referenceElement.multiResolve(false); + } + return GroovyResolveResult.EMPTY_ARRAY; } @NotNull @@ -168,7 +174,7 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx } public int getArrayCount() { - final GrArrayDeclaration arrayDeclaration = findChildByClass(GrArrayDeclaration.class); + final GrArrayDeclaration arrayDeclaration = getArrayDeclaration(); if (arrayDeclaration == null) return 0; return arrayDeclaration.getArrayCount(); } @@ -177,9 +183,15 @@ public class GrNewExpressionImpl extends GrCallExpressionImpl implements GrNewEx return findChildByClass(GrAnonymousClassDefinition.class); } + @Nullable + @Override + public GrArrayDeclaration getArrayDeclaration() { + return findChildByClass(GrArrayDeclaration.class); + } + @Nullable public PsiMethod resolveMethod() { - return resolveConstructor(); + return PsiImplUtil.extractUniqueElement(multiResolveConstructor()); } @NotNull