1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8228988 8266598
27  * @summary An annotation-typed property of an annotation that is represented as an
28  *          incompatible property of another type should yield an AnnotationTypeMismatchException.
29  * @modules java.base/jdk.internal.org.objectweb.asm
30  * @run main AnnotationTypeMismatchTest
31  */
32 
33 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
34 import jdk.internal.org.objectweb.asm.ClassWriter;
35 import jdk.internal.org.objectweb.asm.Opcodes;
36 import jdk.internal.org.objectweb.asm.Type;
37 
38 import java.lang.annotation.AnnotationTypeMismatchException;
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 
42 public class AnnotationTypeMismatchTest {
43 
main(String[] args)44     public static void main(String[] args) throws Exception {
45         /*
46          * @AnAnnotation(value = AnEnum.VALUE) // would now be: value = @Value
47          * class Carrier { }
48          */
49         ClassWriter writer = new ClassWriter(0);
50         writer.visit(Opcodes.V1_8, 0, "sample/Carrier", null, Type.getInternalName(Object.class), null);
51         AnnotationVisitor v = writer.visitAnnotation(Type.getDescriptor(AnAnnotation.class), true);
52         v.visitEnum("value", Type.getDescriptor(AnEnum.class), "VALUE");
53         writer.visitEnd();
54         byte[] b = writer.toByteArray();
55         ByteArrayClassLoader cl = new ByteArrayClassLoader(AnnotationTypeMismatchTest.class.getClassLoader());
56         cl.init(b);
57         AnAnnotation sample = cl.loadClass("sample.Carrier").getAnnotation(AnAnnotation.class);
58         try {
59             Value value = sample.value();
60             throw new IllegalStateException("Found value: " + value);
61         } catch (AnnotationTypeMismatchException e) {
62             if (!e.element().getName().equals("value")) {
63                 throw new IllegalStateException("Unexpected element: " + e.element());
64             } else if (!e.foundType().equals(AnEnum.class.getName() + "." + AnEnum.VALUE.name())) {
65                 throw new IllegalStateException("Unexpected type: " + e.foundType());
66             }
67         }
68     }
69 
70     public enum AnEnum {
71         VALUE
72     }
73 
74     @Retention(RetentionPolicy.RUNTIME)
75     public @interface AnAnnotation {
value()76         Value value() default @Value;
77     }
78 
79     public @interface Value { }
80 
81     public static class ByteArrayClassLoader extends ClassLoader {
82 
ByteArrayClassLoader(ClassLoader parent)83         public ByteArrayClassLoader(ClassLoader parent) {
84             super(parent);
85         }
86 
init(byte[] b)87         public void init(byte[] b) {
88             defineClass("sample.Carrier", b, 0, b.length);
89         }
90     }
91 }
92