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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 /*
 27  * @test
 28  * @summary Verify that primitive class declarations can be annotated
 29  * @bug 8244713
 30  * @modules jdk.jdeps/com.sun.tools.classfile
 31  * @run main UnannotatedProjection
 32  */
 33 
 34 import java.lang.annotation.ElementType;
 35 import java.lang.annotation.Retention;
 36 import java.lang.annotation.RetentionPolicy;
 37 import java.lang.annotation.Target;
 38 
 39 import com.sun.tools.classfile.*;
 40 
 41 public class UnannotatedProjection {
 42 
 43     @interface DA {}
 44 
 45     @Retention(RetentionPolicy.RUNTIME)
 46     @interface DARR {}
 47 
 48     @Target(ElementType.TYPE_PARAMETER)
 49     @interface TA {}
 50 
 51     @Retention(RetentionPolicy.RUNTIME)
 52     @Target(ElementType.TYPE_PARAMETER)
 53     @interface TARR {}
 54 
 55     @DA @DARR
 56     public primitive class V<@TA @TARR T> {}
 57 
 58     public static void main(String[] args) throws Exception {
 59         ClassFile cls = ClassFile.read(UnannotatedProjection.class.getResourceAsStream("UnannotatedProjection$V.class"));
 60 
 61         if (cls == null) {
 62             throw new AssertionError("Could not locate the class files");
 63         }
 64 
 65         RuntimeInvisibleAnnotations_attribute inv = (RuntimeInvisibleAnnotations_attribute) cls.attributes.get(Attribute.RuntimeInvisibleAnnotations);
 66         if (inv == null || inv.annotations == null || inv.annotations.length != 1) {
 67             throw new AssertionError("Missing annotations");
 68         }
 69 
 70         String aName = cls.constant_pool.getUTF8Value(inv.annotations[0].type_index);
 71         if (!aName.equals("LUnannotatedProjection$DA;")) {
 72             throw new AssertionError("Unexpected annotation: " + aName);
 73         }
 74 
 75         RuntimeInvisibleTypeAnnotations_attribute invta = (RuntimeInvisibleTypeAnnotations_attribute) cls.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations);
 76         if (invta == null || invta.annotations == null || invta.annotations.length != 1) {
 77             throw new AssertionError("Missing annotations");
 78         }
 79 
 80         aName = cls.constant_pool.getUTF8Value(invta.annotations[0].annotation.type_index);
 81         if (!aName.equals("LUnannotatedProjection$TA;")) {
 82             throw new AssertionError("Unexpected annotation: " + aName);
 83         }
 84 
 85         RuntimeVisibleAnnotations_attribute v = (RuntimeVisibleAnnotations_attribute) cls.attributes.get(Attribute.RuntimeVisibleAnnotations);
 86         if (v == null || v.annotations == null || v.annotations.length != 1) {
 87             throw new AssertionError("Missing annotations");
 88         }
 89 
 90         aName = cls.constant_pool.getUTF8Value(v.annotations[0].type_index);
 91         if (!aName.equals("LUnannotatedProjection$DARR;")) {
 92             throw new AssertionError("Unexpected annotation: " + aName);
 93         }
 94 
 95         RuntimeVisibleTypeAnnotations_attribute vta = (RuntimeVisibleTypeAnnotations_attribute) cls.attributes.get(Attribute.RuntimeVisibleTypeAnnotations);
 96         if (vta == null || vta.annotations == null || vta.annotations.length != 1) {
 97             throw new AssertionError("Missing annotations");
 98         }
 99 
100         aName = cls.constant_pool.getUTF8Value(vta.annotations[0].annotation.type_index);
101         if (!aName.equals("LUnannotatedProjection$TARR;")) {
102             throw new AssertionError("Unexpected annotation: " + aName);
103         }
104     }
105 }