Skip to content

Commit

Permalink
Fix for #1096: save enclosing call reference for static method call args
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 22, 2020
1 parent af63a7b commit c094093
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,23 @@ public void testClosureParamsAnnotation3() {
assertType(contents, "list", "java.util.List<java.lang.Integer>");
}

@Test
public void testClosureParamsAnnotation4() {
String contents =
//@formatter:off
"import groovy.transform.stc.*\n" +
"class C {\n" +
" static m(String s, @ClosureParams(value=SimpleType, options='java.util.List<java.lang.Integer>') Closure c) {\n" +
" }\n" +
" static test() {\n" +
" m('str', { list -> null })\n" +
" }\n" +
"}\n";
//@formatter:on

assertType(contents, "list", "java.util.List<java.lang.Integer>");
}

@Test
public void testClosureReferencesSuperClass() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,24 @@ public void testTypeChecked8() {

runNegativeTest(sources, "");
}

@Test
public void testTypeChecked9() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"class C {\n" +
" static m(String s, Comparable<List<Integer>> c) {\n" +
" }\n" +
" @groovy.transform.TypeChecked\n" +
" static test() {\n" +
" m('blah', { list -> list.get(0) })\n" +
" }\n" +
"}\n" +
"C.test()\n",
};
//@formatter:on

runNegativeTest(sources, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1648,18 +1648,20 @@ public void visitSpreadMapExpression(final SpreadMapExpression node) {

@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression node) {
ClassNode type = node.getOwnerType();
if (isPrimaryExpression(node)) {
visitMethodCallExpression(new MethodCallExpression(new ClassExpression(type), node.getMethod(), node.getArguments()));
}
handleSimpleExpression(node, () -> {
Tuple t = dependentDeclarationStack.removeLast();
boolean isPresentInSource = (node.getEnd() > 0);
ClassNode type = node.getOwnerType();
if (isPresentInSource) {
visitClassReference(type);
}
if (isPresentInSource || isEnumInit(node)) {
// visit static method call arguments
scopes.getLast().addEnclosingMethodCall(
new VariableScope.CallAndType(node, t.declaration, t.declaringType, enclosingModule));
super.visitStaticMethodCallExpression(node);
scopes.getLast().forgetEnclosingMethodCall();

// visit anonymous inner class members
if (GroovyUtils.isAnonymous(type)) {
visitMethodOverrides(type);
Expand Down Expand Up @@ -1986,7 +1988,8 @@ private boolean handleRequestor(final Expression node, final ClassNode primaryTy
}
switch (status) {
case CONTINUE:
if (node instanceof ConstructorCallExpression) {
// TODO: (node instanceof MethodCall && !(node instanceof MethodCallExpression))
if (node instanceof ConstructorCallExpression || node instanceof StaticMethodCallExpression) {
dependentDeclarationStack.add(new Tuple(result.declaringType, result.declaration));
}
// fall through
Expand Down

0 comments on commit c094093

Please sign in to comment.