Skip to content

Commit

Permalink
Fix for same-unit default parameter constructor reference searching
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 26, 2018
1 parent 80c2e8a commit a3c04d3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,44 @@ public void testConstructorReferences10() throws Exception {
assertEquals(1, ctorRefs);
}

@Test // default value generates a synthetic constructor
public void testConstructorReferences11() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i = 0) {}\n" + // search for this
" Foo(String s) {this()}\n" + // yes
" def m() {\n" +
" new Foo()\n" + // yes
" new Foo(0)\n" + // yes
" new Foo('')\n" + // no
" }\n" +
"}");

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy"))
.count();
assertEquals(3, ctorRefs);
}

@Test // default value generates a synthetic constructor
public void testConstructorReferences12() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i) {this()}\n" + // yes
" Foo(String s = '') {}\n" + // search for this
" def m() {\n" +
" new Foo()\n" + // yes
" new Foo(0)\n" + // no
" new Foo('')\n" + // yes
" }\n" +
"}");

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy"))
.count();
assertEquals(3, ctorRefs);
}

//--------------------------------------------------------------------------

List<SearchMatch> searchForReferences(IMethod method) throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.ClassNodeSkip;
import org.codehaus.groovy.classgen.asm.WriterController;
import org.codehaus.groovy.reflection.ClassInfo;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.syntax.RuntimeParserException;
import org.codehaus.groovy.syntax.Token;
Expand Down Expand Up @@ -817,12 +818,11 @@ public void visitVariableExpression(VariableExpression expression) {
addPropertyMethod(newMethod);
// GRECLIPSE add
newMethod.setOriginal(method);
newMethod.setSourcePosition(method);
newMethod.setNameEnd(method.getNameEnd());
newMethod.setNameStart(method.getNameStart());
// GRECLIPSE end
newMethod.setGenericsTypes(method.getGenericsTypes());
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, true);
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
}
});
}
Expand All @@ -835,6 +835,13 @@ public void call(ArgumentListExpression arguments, Parameter[] newParams, Method
ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments);
Statement code = new ExpressionStatement(expression);
addConstructor(newParams, ctor, code, node);
// GRECLIPSE add
ctor = DefaultGroovyMethods.last(node.getDeclaredConstructors());
ctor.setOriginal(method);
ctor.setNameEnd(method.getNameEnd());
ctor.setNameStart(method.getNameStart());
ctor.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
// GRECLIPSE end
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.ClassNodeSkip;
import org.codehaus.groovy.classgen.asm.WriterController;
import org.codehaus.groovy.reflection.ClassInfo;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.syntax.RuntimeParserException;
import org.codehaus.groovy.syntax.Token;
Expand Down Expand Up @@ -884,12 +885,11 @@ public void visitVariableExpression(VariableExpression expression) {
addPropertyMethod(newMethod);
// GRECLIPSE add
newMethod.setOriginal(method);
newMethod.setSourcePosition(method);
newMethod.setNameEnd(method.getNameEnd());
newMethod.setNameStart(method.getNameStart());
// GRECLIPSE end
newMethod.setGenericsTypes(method.getGenericsTypes());
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, true);
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
}
});
}
Expand All @@ -902,6 +902,13 @@ public void call(ArgumentListExpression arguments, Parameter[] newParams, Method
ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments);
Statement code = new ExpressionStatement(expression);
addConstructor(newParams, ctor, code, node);
// GRECLIPSE add
ctor = DefaultGroovyMethods.last(node.getDeclaredConstructors());
ctor.setOriginal(method);
ctor.setNameEnd(method.getNameEnd());
ctor.setNameStart(method.getNameStart());
ctor.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
// GRECLIPSE end
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.ClassNodeSkip;
import org.codehaus.groovy.classgen.asm.WriterController;
import org.codehaus.groovy.reflection.ClassInfo;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.syntax.RuntimeParserException;
import org.codehaus.groovy.syntax.Token;
Expand Down Expand Up @@ -864,12 +865,11 @@ public void visitVariableExpression(VariableExpression expression) {
addPropertyMethod(newMethod);
// GRECLIPSE add
newMethod.setOriginal(method);
newMethod.setSourcePosition(method);
newMethod.setNameEnd(method.getNameEnd());
newMethod.setNameStart(method.getNameStart());
// GRECLIPSE end
newMethod.setGenericsTypes(method.getGenericsTypes());
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, true);
newMethod.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
}
});
}
Expand All @@ -882,6 +882,13 @@ public void call(ArgumentListExpression arguments, Parameter[] newParams, Method
ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments);
Statement code = new ExpressionStatement(expression);
addConstructor(newParams, ctor, code, node);
// GRECLIPSE add
ctor = DefaultGroovyMethods.last(node.getDeclaredConstructors());
ctor.setOriginal(method);
ctor.setNameEnd(method.getNameEnd());
ctor.setNameStart(method.getNameStart());
ctor.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
// GRECLIPSE end
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.function.Function;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ConstructorNode;
Expand Down Expand Up @@ -88,7 +89,7 @@ public ConstructorReferenceSearchRequestor(ConstructorPattern pattern, SearchReq

@Override
public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaElement enclosingElement) {
if (node.getEnd() > 0 && result.declaration instanceof ConstructorNode) {
if (node instanceof AnnotatedNode && ((AnnotatedNode) node).getNameEnd() > 0 && result.declaration instanceof ConstructorNode) {

if (findDeclarations && node instanceof ConstructorNode) {
ConstructorNode decl = (ConstructorNode) node;
Expand All @@ -102,7 +103,7 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
if (findReferences && node instanceof ConstructorCallExpression) {
ConstructorCallExpression call = (ConstructorCallExpression) node;
String typeName = result.declaringType.getName().replace('$', '.');
Parameter[] parameters = ((ConstructorNode) result.declaration).getParameters();
Parameter[] parameters = ((ConstructorNode) result.declaration).getOriginal().getParameters();
if (typeName.equals(declaringQualifiedName) && hasMatchingParameters(parameters)) {
reportSearchMatch(enclosingElement, element -> {
boolean isConstructor = true, isSynthetic = false, isSuperInvocation = call.isSuperCall(), isWithinComment = false;
Expand Down

0 comments on commit a3c04d3

Please sign in to comment.