Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 75c1f14da -> 21662c8b2
GROOVY-3278: Using referenced String constant as value of Annotation causes compile error (closes #814) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/21662c8b Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/21662c8b Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/21662c8b Branch: refs/heads/GROOVY_2_5_X Commit: 21662c8b233d50fb3ece394b5fcf9559027b4cf8 Parents: 75c1f14 Author: Paul King <pa...@asert.com.au> Authored: Mon Oct 22 17:49:26 2018 +1000 Committer: Paul King <pa...@asert.com.au> Committed: Tue Oct 23 13:39:02 2018 +1000 ---------------------------------------------------------------------- gradle/pomconfigurer.gradle | 3 +++ .../groovy/ast/decompiled/AsmDecompiler.java | 4 +--- .../groovy/ast/decompiled/ClassStub.java | 8 ++++++- .../ast/decompiled/MemberSignatureParser.java | 5 ++-- .../groovy/classgen/AnnotationVisitor.java | 2 +- src/test/gls/annotations/AnnotationTest.groovy | 16 +++++++++++++ src/test/gls/annotations/Base3278.groovy | 22 +++++++++++++++++ src/test/gls/annotations/Child2.groovy | 20 ++++++++++++++++ src/test/gls/annotations/MyAnnotation.groovy | 25 ++++++++++++++++++++ 9 files changed, 98 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/gradle/pomconfigurer.gradle ---------------------------------------------------------------------- diff --git a/gradle/pomconfigurer.gradle b/gradle/pomconfigurer.gradle index d3a0908..02d4d70 100644 --- a/gradle/pomconfigurer.gradle +++ b/gradle/pomconfigurer.gradle @@ -645,6 +645,9 @@ project.ext.pomConfigureClosureWithoutTweaks = { contributor { name 'Lidia Donajczyk-Lipinska' } + contributor { + name 'Peter Gromov' + } } mailingLists { mailingList { http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java b/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java index f0bf0bd..6286ff9 100644 --- a/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java +++ b/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java @@ -43,8 +43,6 @@ import java.util.concurrent.ConcurrentHashMap; /** * A utility class responsible for decompiling JVM class files and producing {@link ClassStub} objects reflecting their structure. - * - * @author Peter Gromov */ public abstract class AsmDecompiler { @@ -172,7 +170,7 @@ public abstract class AsmDecompiler { @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { - final FieldStub stub = new FieldStub(name, access, desc, signature); + final FieldStub stub = new FieldStub(name, access, desc, signature, value); if (result.fields == null) result.fields = new ArrayList<FieldStub>(1); result.fields.add(stub); return new FieldVisitor(api) { http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/main/java/org/codehaus/groovy/ast/decompiled/ClassStub.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/ClassStub.java b/src/main/java/org/codehaus/groovy/ast/decompiled/ClassStub.java index 987753b..43b10df 100644 --- a/src/main/java/org/codehaus/groovy/ast/decompiled/ClassStub.java +++ b/src/main/java/org/codehaus/groovy/ast/decompiled/ClassStub.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.Map; /** - * @author Peter Gromov + * Data structures holding class info to enable lazy loading */ public class ClassStub extends MemberStub { final String className; @@ -81,12 +81,18 @@ class FieldStub extends MemberStub { final int accessModifiers; final String desc; final String signature; + final Object value; public FieldStub(String fieldName, int accessModifiers, String desc, String signature) { + this(fieldName, accessModifiers, desc, signature, null); + } + + public FieldStub(String fieldName, int accessModifiers, String desc, String signature, Object value) { this.fieldName = fieldName; this.accessModifiers = accessModifiers; this.desc = desc; this.signature = signature; + this.value = value; } } http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/main/java/org/codehaus/groovy/ast/decompiled/MemberSignatureParser.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/MemberSignatureParser.java b/src/main/java/org/codehaus/groovy/ast/decompiled/MemberSignatureParser.java index 54f972f..ecb1028 100644 --- a/src/main/java/org/codehaus/groovy/ast/decompiled/MemberSignatureParser.java +++ b/src/main/java/org/codehaus/groovy/ast/decompiled/MemberSignatureParser.java @@ -35,7 +35,7 @@ import java.util.List; import java.util.Map; /** - * @author Peter Gromov + * Utility methods for lazy class loading */ class MemberSignatureParser { static MethodNode createMethodNode(final AsmReferenceResolver resolver, MethodStub method) { @@ -153,7 +153,8 @@ class MemberSignatureParser { } }); } - return new FieldNode(field.fieldName, field.accessModifiers, type[0], owner, null); + ConstantExpression value = field.value == null ? null : new ConstantExpression(field.value); + return new FieldNode(field.fieldName, field.accessModifiers, type[0], owner, value); } } http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/main/java/org/codehaus/groovy/classgen/AnnotationVisitor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/classgen/AnnotationVisitor.java b/src/main/java/org/codehaus/groovy/classgen/AnnotationVisitor.java index dafc741..103d5e1 100644 --- a/src/main/java/org/codehaus/groovy/classgen/AnnotationVisitor.java +++ b/src/main/java/org/codehaus/groovy/classgen/AnnotationVisitor.java @@ -142,7 +142,7 @@ public class AnnotationVisitor { return exp; try { - Field field = type.getTypeClass().getField(pe.getPropertyAsString()); + Field field = type.redirect().getTypeClass().getField(pe.getPropertyAsString()); if (field != null && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) { return new ConstantExpression(field.get(null)); } http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/test/gls/annotations/AnnotationTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/gls/annotations/AnnotationTest.groovy b/src/test/gls/annotations/AnnotationTest.groovy index fae832d..2ad49b0 100644 --- a/src/test/gls/annotations/AnnotationTest.groovy +++ b/src/test/gls/annotations/AnnotationTest.groovy @@ -757,6 +757,22 @@ class AnnotationTest extends CompilableTestSupport { ''' } + void testAnnotationAttributeConstantFromPrecompiledGroovyClass() { + // GROOVY-3278 + assertScript ''' + @MyAnnotation(groups = 42) + class Child1 extends Base3278 {} + + @MyAnnotation(groups = [-1, Base3278.CONST, Integer.MIN_VALUE]) + class Child3 extends Base3278 {} + + assert new Child1().run() == [42] + assert new Child2().run() == [2147483647] + assert new Child3().run() == [-1, 3278, -2147483648] + ''' + + } + //Parametrized tests in Spock would allow to make it much more readable private static String codeWithMetaAnnotationWithTarget(String targetElementTypeName) { """ http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/test/gls/annotations/Base3278.groovy ---------------------------------------------------------------------- diff --git a/src/test/gls/annotations/Base3278.groovy b/src/test/gls/annotations/Base3278.groovy new file mode 100644 index 0000000..05da7fa --- /dev/null +++ b/src/test/gls/annotations/Base3278.groovy @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +class Base3278 { + static final int CONST = 3278 + def run() { getClass().annotations[0].groups() } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/test/gls/annotations/Child2.groovy ---------------------------------------------------------------------- diff --git a/src/test/gls/annotations/Child2.groovy b/src/test/gls/annotations/Child2.groovy new file mode 100644 index 0000000..16fe19a --- /dev/null +++ b/src/test/gls/annotations/Child2.groovy @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +@MyAnnotation(groups = Integer.MAX_VALUE) +class Child2 extends Base3278 {} http://git-wip-us.apache.org/repos/asf/groovy/blob/21662c8b/src/test/gls/annotations/MyAnnotation.groovy ---------------------------------------------------------------------- diff --git a/src/test/gls/annotations/MyAnnotation.groovy b/src/test/gls/annotations/MyAnnotation.groovy new file mode 100644 index 0000000..5a4d0f3 --- /dev/null +++ b/src/test/gls/annotations/MyAnnotation.groovy @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +import java.lang.annotation.* + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@interface MyAnnotation { + int[] groups() default [] +}