From commits-return-8409-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Sun Apr 28 09:02:14 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 94BBB18077F for ; Sun, 28 Apr 2019 11:02:13 +0200 (CEST) Received: (qmail 27412 invoked by uid 500); 28 Apr 2019 09:02:13 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 27281 invoked by uid 99); 28 Apr 2019 09:02:12 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Apr 2019 09:02:12 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 857348549A; Sun, 28 Apr 2019 09:02:12 +0000 (UTC) Date: Sun, 28 Apr 2019 09:02:16 +0000 To: "commits@groovy.apache.org" Subject: [groovy] 05/05: GROOVY-9095: Default retention policy not implemented for annotation (add test) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: paulk@apache.org In-Reply-To: <155644213177.27678.6304254954711576567@gitbox.apache.org> References: <155644213177.27678.6304254954711576567@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: groovy X-Git-Refname: refs/heads/GROOVY_2_5_X X-Git-Reftype: branch X-Git-Rev: b223f6f389ef7a0b04d2a09c5538e3b34d609bc6 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20190428090212.857348549A@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. paulk pushed a commit to branch GROOVY_2_5_X in repository https://gitbox.apache.org/repos/asf/groovy.git commit b223f6f389ef7a0b04d2a09c5538e3b34d609bc6 Author: Paul King AuthorDate: Sun Apr 28 16:02:12 2019 +1000 GROOVY-9095: Default retention policy not implemented for annotation (add test) --- .../gls/annotations/AnnotationsInfoTest.groovy | 54 +++++++++++++++++ .../AnnotationsTestBase.java} | 67 ++++++++++++++++------ .../gls/annotations/HasDefaultClassRetention.java | 21 +++++++ .../gls/annotations/HasExplicitClassRetention.java | 25 ++++++++ src/test/gls/annotations/HasRuntimeRetention.java | 25 ++++++++ src/test/gls/annotations/HasSourceRetention.java | 25 ++++++++ src/test/gls/generics/GenericsTestBase.java | 1 + 7 files changed, 202 insertions(+), 16 deletions(-) diff --git a/src/test/gls/annotations/AnnotationsInfoTest.groovy b/src/test/gls/annotations/AnnotationsInfoTest.groovy new file mode 100644 index 0000000..f78023d --- /dev/null +++ b/src/test/gls/annotations/AnnotationsInfoTest.groovy @@ -0,0 +1,54 @@ +/* + * 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. + */ +package gls.annotations + +class AnnotationsInfoTest extends AnnotationsTestBase { + + void testClassWithoutParameterExtendsClassWithFixedParameter() { + createClassInfo """ + import gls.annotations.* + + @HasRuntimeRetention + class B extends ArrayList { + @HasSourceRetention + def fieldS + + @HasRuntimeRetention + def fieldR + + @HasDefaultClassRetention + def methodDC() {} + + @HasExplicitClassRetention + def methodEC() {} + } + """ + assert !annotations.any{ it.contains('SourceRetention') } + annotations.findAll { it.contains('RuntimeRetention') }.with { annos -> + assert annos.size() == 2 + assert annos.every{ it.contains('visible=true') } + assert annos.every{ it.contains('class B') || it.contains('field B#fieldR') } + } + annotations.findAll { it.contains('ClassRetention') }.with { annos -> + assert annos.size() == 2 + assert annos.every{ it.contains('visible=false') } + assert annos.every{ it.contains('method B#methodDC') || it.contains('method B#methodEC') } + } + } +} diff --git a/src/test/gls/generics/GenericsTestBase.java b/src/test/gls/annotations/AnnotationsTestBase.java similarity index 58% copy from src/test/gls/generics/GenericsTestBase.java copy to src/test/gls/annotations/AnnotationsTestBase.java index 820a330..1fb6d43 100644 --- a/src/test/gls/generics/GenericsTestBase.java +++ b/src/test/gls/annotations/AnnotationsTestBase.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package gls.generics; +package gls.annotations; import groovy.lang.GroovyClassLoader; import groovy.lang.GroovyClassLoader.InnerLoader; @@ -26,13 +26,15 @@ import org.codehaus.groovy.control.CompilationFailedException; import org.codehaus.groovy.control.CompilationUnit; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.SourceUnit; +import org.objectweb.asm.*; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; -public abstract class GenericsTestBase extends GroovyTestCase { +public abstract class AnnotationsTestBase extends GroovyTestCase { MyLoader loader; - HashMap signatures; + List annotations; + String current = ""; private class MyLoader extends GroovyClassLoader { MyLoader(ClassLoader classLoader) { @@ -51,43 +53,76 @@ public abstract class GenericsTestBase extends GroovyTestCase { protected Class createClass(byte[] code, ClassNode classNode) { ClassReader cr = new ClassReader(code); - GenericsTester classVisitor = new GenericsTester(new org.objectweb.asm.tree.ClassNode()); + AnnotationsTester classVisitor = new AnnotationsTester(new org.objectweb.asm.tree.ClassNode()); cr.accept(classVisitor, ClassWriter.COMPUTE_MAXS); return super.createClass(code, classNode); } } - private class GenericsTester extends ClassVisitor { - GenericsTester(ClassVisitor cv) { + private class FieldAnnotationScanner extends FieldVisitor { + private final String field; + + FieldAnnotationScanner(String field) { + super(CompilerConfiguration.ASM_API_VERSION); + this.field = field; + } + + @Override + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + annotations.add("visiting field " + field + ", found annotation: desc=" + desc + ", visible=" + visible); + return super.visitAnnotation(desc, visible); + } + } + + private class MethodAnnotationScanner extends MethodVisitor { + private final String method; + + MethodAnnotationScanner(String method) { + super(CompilerConfiguration.ASM_API_VERSION); + this.method = method; + } + + @Override + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + annotations.add("visiting method " + method + ", found annotation: desc=" + desc + ", visible=" + visible); + return super.visitAnnotation(desc, visible); + } + } + + private class AnnotationsTester extends ClassVisitor { + AnnotationsTester(ClassVisitor cv) { super(CompilerConfiguration.ASM_API_VERSION, cv); } public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { - if (signature != null) signatures.put("class", signature); + current = name; + } + + public AnnotationVisitor visitAnnotation(String desc, boolean visible){ + annotations.add("visiting class " + current + " found annotation: desc=" + desc + ", visible=" + visible); + return super.visitAnnotation(desc,visible); } public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { - if (signature != null) signatures.put(name, signature); - return super.visitField(access, name, desc, signature, value); + return new FieldAnnotationScanner(current + "#" + name); } public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - if (signature != null) signatures.put(name + desc, signature); - return super.visitMethod(access, name, desc, signature, exceptions); + return new MethodAnnotationScanner(current + "#" + name); } } public void setUp() { loader = new MyLoader(this.getClass().getClassLoader()); - signatures = new HashMap<>(); + annotations = new ArrayList<>(); } public void createClassInfo(String script) { loader.parseClass(script); } - public Map getSignatures() { - return signatures; + public List getAnnotations() { + return annotations; } protected void shouldNotCompile(String script) { diff --git a/src/test/gls/annotations/HasDefaultClassRetention.java b/src/test/gls/annotations/HasDefaultClassRetention.java new file mode 100644 index 0000000..4ff6eee --- /dev/null +++ b/src/test/gls/annotations/HasDefaultClassRetention.java @@ -0,0 +1,21 @@ +/* + * 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. + */ +package gls.annotations; + +public @interface HasDefaultClassRetention { } diff --git a/src/test/gls/annotations/HasExplicitClassRetention.java b/src/test/gls/annotations/HasExplicitClassRetention.java new file mode 100644 index 0000000..87b1802 --- /dev/null +++ b/src/test/gls/annotations/HasExplicitClassRetention.java @@ -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. + */ +package gls.annotations; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.CLASS) +public @interface HasExplicitClassRetention { } diff --git a/src/test/gls/annotations/HasRuntimeRetention.java b/src/test/gls/annotations/HasRuntimeRetention.java new file mode 100644 index 0000000..6c0498a --- /dev/null +++ b/src/test/gls/annotations/HasRuntimeRetention.java @@ -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. + */ +package gls.annotations; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface HasRuntimeRetention { } diff --git a/src/test/gls/annotations/HasSourceRetention.java b/src/test/gls/annotations/HasSourceRetention.java new file mode 100644 index 0000000..8709eed --- /dev/null +++ b/src/test/gls/annotations/HasSourceRetention.java @@ -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. + */ +package gls.annotations; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.SOURCE) +public @interface HasSourceRetention { } diff --git a/src/test/gls/generics/GenericsTestBase.java b/src/test/gls/generics/GenericsTestBase.java index 820a330..5b91306 100644 --- a/src/test/gls/generics/GenericsTestBase.java +++ b/src/test/gls/generics/GenericsTestBase.java @@ -26,6 +26,7 @@ import org.codehaus.groovy.control.CompilationFailedException; import org.codehaus.groovy.control.CompilationUnit; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.SourceUnit; +import org.objectweb.asm.*; import java.util.HashMap; import java.util.Map;