change signatare incorrectly processes constructor calls

This commit is contained in:
Maxim Medvedev
2010-07-29 13:25:14 +04:00
parent 4f47f3ad4d
commit 1dc19a233a
22 changed files with 204 additions and 81 deletions
@@ -153,7 +153,7 @@ public class GroovyConstructorUsagesSearchHelper {
final GrConstructorInvocation invocation = (GrConstructorInvocation)statements[0];
if (invocation.isThisCall() == processThisRefs &&
invocation.getManager().areElementsEquivalent(invocation.resolveConstructor(), searchedConstructor) &&
!consumer.process(invocation)) {
!consumer.process(invocation.getThisOrSuperKeyword())) {
return false;
}
}
@@ -65,8 +65,21 @@ public class ConstructorBody implements GroovyElementTypes {
private static boolean parseExplicitConstructor(PsiBuilder builder, GroovyParser parser) {
TypeArguments.parse(builder);
boolean result = false;
if (ParserUtils.lookAhead(builder, kTHIS, mLPAREN)) {
final PsiBuilder.Marker marker = builder.mark();
ParserUtils.getToken(builder, kTHIS);
marker.done(THIS_REFERENCE_EXPRESSION);
result = true;
}
if (ParserUtils.lookAhead(builder, kSUPER, mLPAREN)) {
final PsiBuilder.Marker marker = builder.mark();
ParserUtils.getToken(builder, kSUPER);
marker.done(SUPER_REFERENCE_EXPRESSION);
result = true;
}
if ((ParserUtils.getToken(builder, kTHIS) || ParserUtils.getToken(builder, kSUPER)) && ParserUtils.lookAhead(builder, mLPAREN)) {
if (result) {
PsiBuilder.Marker marker = builder.mark();
ParserUtils.getToken(builder, mLPAREN);
ArgumentList.parseArgumentList(builder, mRPAREN, parser);
@@ -16,18 +16,17 @@
package org.jetbrains.plugins.groovy.lang.psi.api.statements;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyVariantReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConstructorCall;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrThisSuperReferenceExpression;
/**
* User: Dmitry.Krasilschikov
* Date: 29.05.2007
*/
public interface GrConstructorInvocation extends GrStatement, GrConstructorCall, PsiPolyVariantReference {
public interface GrConstructorInvocation extends GrStatement, GrConstructorCall {
boolean isSuperCall();
boolean isThisCall();
@@ -35,7 +34,7 @@ public interface GrConstructorInvocation extends GrStatement, GrConstructorCall,
@NotNull
GrArgumentList getArgumentList();
PsiElement getThisOrSuperKeyword();
GrThisSuperReferenceExpression getThisOrSuperKeyword();
@Nullable
PsiClass getDelegatedClass();
@@ -4,12 +4,8 @@
package org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions;
import org.jetbrains.annotations.Nullable;
/**
* @author ilyas
*/
public interface GrSuperReferenceExpression extends GrExpression {
@Nullable
GrReferenceExpression getQualifier();
public interface GrSuperReferenceExpression extends GrThisSuperReferenceExpression {
}
@@ -4,12 +4,8 @@
package org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions;
import org.jetbrains.annotations.Nullable;
/**
* @author ilyas
*/
public interface GrThisReferenceExpression extends GrExpression {
@Nullable
GrReferenceExpression getQualifier();
public interface GrThisReferenceExpression extends GrThisSuperReferenceExpression {
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions;
import com.intellij.psi.PsiPolyVariantReference;
import org.jetbrains.annotations.Nullable;
/**
* @author Maxim.Medvedev
*/
public interface GrThisSuperReferenceExpression extends GrExpression, PsiPolyVariantReference {
@Nullable
GrReferenceExpression getQualifier();
}
@@ -21,17 +21,17 @@ import com.intellij.psi.*;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrThisSuperReferenceExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl;
@@ -73,17 +73,18 @@ public class GrConstructorInvocationImpl extends GroovyPsiElementImpl implements
}
public boolean isSuperCall() {
return findChildByType(GroovyTokenTypes.kSUPER) != null;
return findChildByType(GroovyElementTypes.SUPER_REFERENCE_EXPRESSION) != null;
}
public boolean isThisCall() {
return findChildByType(GroovyTokenTypes.kTHIS) != null;
return findChildByType(GroovyElementTypes.THIS_REFERENCE_EXPRESSION) != null;
}
private static final TokenSet THIS_OR_SUPER_SET = TokenSet.create(GroovyTokenTypes.kTHIS, GroovyTokenTypes.kSUPER);
private static final TokenSet THIS_OR_SUPER_SET =
TokenSet.create(GroovyElementTypes.THIS_REFERENCE_EXPRESSION, GroovyElementTypes.SUPER_REFERENCE_EXPRESSION);
public PsiElement getThisOrSuperKeyword() {
return findChildByType(THIS_OR_SUPER_SET);
public GrThisSuperReferenceExpression getThisOrSuperKeyword() {
return (GrThisSuperReferenceExpression)findChildByType(THIS_OR_SUPER_SET);
}
public GroovyResolveResult[] multiResolveConstructor() {
@@ -156,36 +157,4 @@ public class GrConstructorInvocationImpl extends GroovyPsiElementImpl implements
public String getCanonicalText() {
return getText(); //TODO
}
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
return this;
}
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
return this;
}
public boolean isReferenceTo(PsiElement element) {
return element instanceof PsiMethod && ((PsiMethod)element).isConstructor() && getManager().areElementsEquivalent(element, resolve());
}
@NotNull
public Object[] getVariants() {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
public boolean isSoft() {
return false;
}
@Override
public PsiReference getReference() {
return this;
}
@NotNull
public ResolveResult[] multiResolve(boolean incompleteCode) {
return multiResolveConstructor();
}
}
@@ -9,7 +9,6 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
@@ -22,7 +21,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefini
/**
* @author ilyas
*/
public class GrSuperReferenceExpressionImpl extends GrExpressionImpl implements GrSuperReferenceExpression {
public class GrSuperReferenceExpressionImpl extends GrThisSuperReferenceExpressionBase implements GrSuperReferenceExpression {
public GrSuperReferenceExpressionImpl(@NotNull ASTNode node) {
super(node);
}
@@ -60,11 +59,6 @@ public class GrSuperReferenceExpressionImpl extends GrExpressionImpl implements
return null;
}
@Nullable
public GrReferenceExpression getQualifier() {
return (GrReferenceExpression)findChildByType(GroovyElementTypes.REFERENCE_EXPRESSION);
}
@Nullable
private PsiType getSuperType(PsiClass aClass) {
if (aClass.isInterface()) {
@@ -88,4 +82,10 @@ public class GrSuperReferenceExpressionImpl extends GrExpressionImpl implements
return superTypes[0];
}
@NotNull
@Override
public String getCanonicalText() {
return "super";
}
}
@@ -9,8 +9,6 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
@@ -22,7 +20,7 @@ import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
/**
* @author ilyas
*/
public class GrThisReferenceExpressionImpl extends GrExpressionImpl implements GrThisReferenceExpression {
public class GrThisReferenceExpressionImpl extends GrThisSuperReferenceExpressionBase implements GrThisReferenceExpression {
public GrThisReferenceExpressionImpl(@NotNull ASTNode node) {
super(node);
}
@@ -73,8 +71,9 @@ public class GrThisReferenceExpressionImpl extends GrExpressionImpl implements G
return elementFactory.createType(context);
}
@Nullable
public GrReferenceExpression getQualifier() {
return (GrReferenceExpression)findChildByType(GroovyElementTypes.REFERENCE_EXPRESSION);
@NotNull
@Override
public String getCanonicalText() {
return "this";
}
}
@@ -0,0 +1,82 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiReference;
import com.intellij.psi.ResolveResult;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrThisSuperReferenceExpression;
/**
* @author Maxim.Medvedev
*/
public abstract class GrThisSuperReferenceExpressionBase extends GrExpressionImpl implements GrThisSuperReferenceExpression {
public GrThisSuperReferenceExpressionBase(ASTNode node) {
super(node);
}
@Nullable
public GrReferenceExpression getQualifier() {
return (GrReferenceExpression)findChildByType(GroovyElementTypes.REFERENCE_EXPRESSION);
}
@Override
public PsiElement getElement() {
return this;
}
@Override
public TextRange getRangeInElement() {
return new TextRange(0, getTextLength());
}
@Override
public PsiElement resolve() {
final PsiElement parent = getParent();
if (parent instanceof GrConstructorInvocation)return ((GrConstructorInvocation)parent).resolveConstructor();
return null;
}
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
return this;
}
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
return this;
}
public boolean isReferenceTo(PsiElement element) {
return element instanceof PsiMethod && ((PsiMethod)element).isConstructor() && getManager().areElementsEquivalent(element, resolve());
}
@NotNull
public Object[] getVariants() {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
public boolean isSoft() {
return false;
}
@Override
public PsiReference getReference() {
return this;
}
@NotNull
public ResolveResult[] multiResolve(boolean incompleteCode) {
final PsiElement parent = getParent();
if (parent instanceof GrConstructorInvocation) {
return ((GrConstructorInvocation)parent).multiResolveConstructor();
}
return ResolveResult.EMPTY_ARRAY;
}
}
@@ -856,7 +856,7 @@ public class PsiUtil {
public static boolean isMethodUsage(PsiElement element) {
if (element instanceof GrEnumConstant) return true;
if (!(element instanceof GrReferenceElement)) return false;
if (!(element instanceof GrReferenceElement || element instanceof GrThisSuperReferenceExpression)) return false;
PsiElement parent = element.getParent();
if (parent instanceof GrCall) {
return true;
@@ -216,20 +216,24 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
doTest(new SimpleInfo[]{new SimpleInfo("l", 1, null, null, "Map<T, E>[]"), new SimpleInfo(0)});
}
public void testConstructorCall() {
doTest(new SimpleInfo[]{new SimpleInfo(0), new SimpleInfo("a", -1, "1", null, PsiType.INT)});
}
private PsiType createType(String typeText) {
return JavaPsiFacade.getElementFactory(getProject()).createTypeByFQClassName(typeText, GlobalSearchScope.allScope(getProject()));
}
private void doTest(SimpleInfo[] parameterInfos) throws Exception {
private void doTest(SimpleInfo[] parameterInfos) {
doTest("public", null, null, parameterInfos, new ThrownExceptionInfo[0], false);
}
private void doTest(String newReturnType, SimpleInfo[] parameterInfos) throws Exception {
private void doTest(String newReturnType, SimpleInfo[] parameterInfos) {
doTest("public", null, newReturnType, parameterInfos, new ThrownExceptionInfo[0], false);
}
private void doTest(String newReturnType, SimpleInfo[] parameterInfos, final boolean generateDelegate) throws Exception {
private void doTest(String newReturnType, SimpleInfo[] parameterInfos, final boolean generateDelegate) {
doTest("public", null, newReturnType, parameterInfos, new ThrownExceptionInfo[0], generateDelegate);
}
@@ -238,7 +242,7 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
String newReturnType,
SimpleInfo[] parameterInfo,
ThrownExceptionInfo[] exceptionInfo,
final boolean generateDelegate) throws Exception {
final boolean generateDelegate) {
final File javaSrc = new File(getTestDataPath() + "/" + getTestName(false) + ".java");
if (javaSrc.exists()) {
myFixture.copyFileToProject(getTestName(false) + ".java");
@@ -32,7 +32,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
PsiWhiteSpace(' ')
Arguments
PsiElement(()('(')
@@ -34,7 +34,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(super)('super')
'super' reference expression
PsiElement(super)('super')
Arguments
PsiElement(()('(')
PsiElement())(')')
@@ -32,7 +32,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
Arguments
PsiElement(()('(')
PsiElement())(')')
@@ -39,7 +39,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
PsiWhiteSpace(' ')
Arguments
PsiElement(()('(')
@@ -33,7 +33,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
Arguments
PsiElement(()('(')
PsiElement())(')')
@@ -39,7 +39,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
PsiWhiteSpace(' ')
Arguments
PsiElement(()('(')
@@ -43,7 +43,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
PsiWhiteSpace(' ')
Arguments
PsiElement(()('(')
@@ -52,7 +52,8 @@ Groovy script
PsiElement({)('{')
PsiWhiteSpace('\n ')
Constructor invocation
PsiElement(this)('this')
'this' reference expression
PsiElement(this)('this')
Arguments
PsiElement(()('(')
PsiElement())(')')
@@ -0,0 +1,15 @@
class Foo {
def Foo() {
this("a")
}
def F<caret>oo(String s) {
}
}
class Bar extends Foo {
def Bar() {
super("d")
}
}
@@ -0,0 +1,15 @@
class Foo {
def Foo() {
this("a", 1)
}
def F<caret>oo(String s, int a) {
}
}
class Bar extends Foo {
def Bar() {
super("d", 1)
}
}