1 /*
 2  * Copyright (c) 2021, 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  * @summary [lworld] test for equal treatment of annotations on primitive records (copy of ApplicableAnnotationsOnRecords)
27  * @bug 8273018
28  * @library /tools/lib
29  * @modules jdk.compiler/com.sun.tools.javac.util
30  *          jdk.jdeps/com.sun.tools.classfile
31  * @compile -XDenablePrimitiveClasses ApplicableAnnotationsOnPrimitiveRecords.java
32  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses ApplicableAnnotationsOnPrimitiveRecords
33  */
34 import com.sun.tools.classfile.*;
35 import com.sun.tools.javac.util.Assert;
36 import java.lang.annotation.*;
37 import java.io.InputStream;
38 
39 @Retention(RetentionPolicy.RUNTIME)
40 @Target({ElementType.FIELD})
41 @interface FieldAnnotation {
42 }
43 
44 @Retention(RetentionPolicy.RUNTIME)
45 @Target({ElementType.METHOD})
46 @interface MethodAnnotation {
47 }
48 
49 @Retention(RetentionPolicy.RUNTIME)
50 @Target({ElementType.PARAMETER})
51 @interface ParameterAnnotation {
52 }
53 
54 public primitive record ApplicableAnnotationsOnPrimitiveRecords(@FieldAnnotation @MethodAnnotation @ParameterAnnotation String s, @FieldAnnotation @MethodAnnotation @ParameterAnnotation int i) {
55 
56     public static void main(String... args) throws Exception {
57         try ( InputStream in = ApplicableAnnotationsOnPrimitiveRecords.class.getResourceAsStream("ApplicableAnnotationsOnPrimitiveRecords.class")) {
58             ClassFile cf = ClassFile.read(in);
59             Assert.check(cf.methods.length > 5);
60             for (Method m : cf.methods) {
61                 String methodName = m.getName(cf.constant_pool);
62                 if (methodName.equals("toString") || methodName.equals("hashCode") || methodName.equals("equals") || methodName.equals("main")) {
63                     // ignore
64                 } else if (methodName.equals("<vnew>")) {
65                     var paAnnos = ((RuntimeVisibleParameterAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleParameterAnnotations)).parameter_annotations;
66                     Assert.check(paAnnos != null && paAnnos.length > 0);
67                     for (var pa : paAnnos) {
68                         Assert.check(pa.length == 1);
69                         Assert.check(cf.constant_pool.getUTF8Value(pa[0].type_index).equals("LParameterAnnotation;"));
70                     }
71                 } else {
72                     var annos = ((RuntimeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleAnnotations)).annotations;
73                     Assert.check(annos.length == 1);
74                     Assert.check(cf.constant_pool.getUTF8Value(annos[0].type_index).equals("LMethodAnnotation;"));
75                 }
76             }
77             Assert.check(cf.fields.length > 0);
78             for (Field field : cf.fields) {
79                 var annos = ((RuntimeAnnotations_attribute) field.attributes.get(Attribute.RuntimeVisibleAnnotations)).annotations;
80                 Assert.check(annos.length == 1);
81                 Assert.check(cf.constant_pool.getUTF8Value(annos[0].type_index).equals("LFieldAnnotation;"));
82             }
83         }
84     }
85 }