Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X 4694379ea -> 6543c626f
GROOVY-8303: VerifyError for nested class this call to static method (closes #593) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6543c626 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6543c626 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6543c626 Branch: refs/heads/GROOVY_2_6_X Commit: 6543c626fe132fda4c1e4a249e29a41460b3fb64 Parents: 4694379 Author: John Wagenleitner <[email protected]> Authored: Sun Aug 27 14:23:29 2017 -0700 Committer: John Wagenleitner <[email protected]> Committed: Sun Aug 27 21:19:58 2017 -0700 ---------------------------------------------------------------------- .../groovy/ast/tools/ClassNodeUtils.java | 10 ++++++- .../groovy/bugs/ConstructorThisCallBug.groovy | 28 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/6543c626/src/main/org/codehaus/groovy/ast/tools/ClassNodeUtils.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/tools/ClassNodeUtils.java b/src/main/org/codehaus/groovy/ast/tools/ClassNodeUtils.java index 3c6377a..077e6cb 100644 --- a/src/main/org/codehaus/groovy/ast/tools/ClassNodeUtils.java +++ b/src/main/org/codehaus/groovy/ast/tools/ClassNodeUtils.java @@ -30,6 +30,7 @@ import org.codehaus.groovy.ast.expr.MapExpression; import org.codehaus.groovy.ast.expr.SpreadExpression; import org.codehaus.groovy.ast.expr.TupleExpression; +import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -195,7 +196,14 @@ public class ClassNodeUtils { return null; } + /** + * Detect whether a given ClassNode is a inner class (non-static). + * + * @param currentClass the ClassNode of interest + * @return true if the given node is a (non-static) inner class, else false + */ public static boolean isInnerClass(ClassNode currentClass) { - return currentClass.getOuterClass() != null && !currentClass.isStaticClass(); + return currentClass.redirect().getOuterClass() != null + && !Modifier.isStatic(currentClass.getModifiers()); } } http://git-wip-us.apache.org/repos/asf/groovy/blob/6543c626/src/test/groovy/bugs/ConstructorThisCallBug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/ConstructorThisCallBug.groovy b/src/test/groovy/bugs/ConstructorThisCallBug.groovy index d9873ec..e6faf9e 100644 --- a/src/test/groovy/bugs/ConstructorThisCallBug.groovy +++ b/src/test/groovy/bugs/ConstructorThisCallBug.groovy @@ -31,6 +31,19 @@ class ConstructorThisCallBug extends GroovyTestCase { assert msg.contains("Can't access instance method 'getData' before the class is constructed") } + void testNestedClassThisCallingInstanceMethod() { + def msg = shouldFail ''' + class Base { + static class Nested { + String getData() { return "ABCD" } + Nested() { this(getData()) } + Nested(String arg) {} + } + } + ''' + assert msg.contains("Can't access instance method 'getData' before the class is constructed") + } + void testThisCallingStaticMethod() { assertScript ''' class Base { @@ -44,6 +57,21 @@ class ConstructorThisCallBug extends GroovyTestCase { ''' } + void testNestedThisCallingStaticMethod() { + assertScript ''' + class Base { + static class Nested { + private String b + static String getData() { return "ABCD" } + Nested() { this(getData()) } + Nested(String b) { this.b = b } + String toString() { b } + } + } + assert new Base.Nested().toString() == 'ABCD' + ''' + } + void testInnerClassSuperCallingInstanceMethod() { assertScript ''' class Parent {
