Skip to content

Commit

Permalink
Fix for issue #502: resolve static members from closure in static scope
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 28, 2018
1 parent 76cb378 commit 3526e57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -110,10 +110,10 @@ public void testClosure7() {
" }\n" +
" }\n" +
"}";
assertExprType(contents, "this", "Bar");
assertExprType(contents, "super", "Foo");
assertExprType(contents, "owner", "Bar");
assertExprType(contents, "delegate", "Bar");
assertExprType(contents, "this", "Bar");
assertExprType(contents, "super", "Foo");
assertExprType(contents, "owner", "Bar");
assertExprType(contents, "delegate", "Bar");
}

@Test // closure with non-default resolve strategy
Expand All @@ -130,10 +130,10 @@ public void testClosure8() {
" }\n" +
" }\n" +
"}";
assertExprType(contents, "this", "Bar");
assertExprType(contents, "super", "java.lang.Object");
assertExprType(contents, "owner", "Bar");
assertExprType(contents, "delegate", "Foo");
assertExprType(contents, "this", "Bar");
assertExprType(contents, "super", "java.lang.Object");
assertExprType(contents, "owner", "Bar");
assertExprType(contents, "delegate", "Foo");
}

@Test // closure within static scope wrt owner
Expand All @@ -148,8 +148,24 @@ public void testClosure9() {
" }\n" +
" }\n" +
"}";
assertExprType(contents, "owner", "java.lang.Class<Bar>");
assertExprType(contents, "delegate", "java.lang.Class<Bar>");
assertExprType(contents, "owner", "java.lang.Class<Bar>");
assertExprType(contents, "delegate", "java.lang.Class<Bar>");
}

@Test // https://github.com/groovy/groovy-eclipse/issues/502
public void testClosure9a() {
String contents =
"class Foo {\n" +
" static long bar(String arg) {\n" +
" }\n" +
" static void baz() {\n" +
" String a = 'bc'\n" +
" def closure = {\n" +
" bar(a)\n" + // call static method from closure within static scope
" }\n" +
" }\n" +
"}";
assertExprType(contents, "bar", "java.lang.Long");
}

@Test
Expand Down Expand Up @@ -340,9 +356,9 @@ public void testClosure17() {
" }\n" +
" }\n" +
"}";
assertExprType(contents, "this", "B");
assertExprType(contents, "super", "A");
assertExprType(contents, "owner", "B");
assertExprType(contents, "this", "B");
assertExprType(contents, "super", "A");
assertExprType(contents, "owner", "B");
assertExprType(contents, "delegate", "B");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected ClassNode findDeclaringType(Expression node, VariableScope scope, Type
// method call without an object expression; requires same handling as a free variable
ClassNode ownerType;
if (scope.getEnclosingClosure() != null) {
ownerType = scope.getOwner();
ownerType = getBaseDeclaringType(scope.getOwner());
} else {
ownerType = scope.getEnclosingTypeDeclaration();
}
Expand All @@ -183,7 +183,7 @@ protected ClassNode findDeclaringType(Expression node, VariableScope scope, Type
if (var != null && !(var instanceof Parameter || var instanceof VariableExpression)) {
ClassNode ownerType;
if (scope.getEnclosingClosure() != null) {
ownerType = scope.getOwner();
ownerType = getBaseDeclaringType(scope.getOwner());
} else {
ownerType = scope.getEnclosingTypeDeclaration();
}
Expand Down

0 comments on commit 3526e57

Please sign in to comment.