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  * @compile -XDenablePrimitiveClasses UnannotatedProjection.java
 32  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses UnannotatedProjection
 33  */
 34 
 35 import java.lang.annotation.ElementType;
 36 import java.lang.annotation.Retention;
 37 import java.lang.annotation.RetentionPolicy;
 38 import java.lang.annotation.Target;
 39 
 40 import com.sun.tools.classfile.*;
 41 
 42 public class UnannotatedProjection {
 43 
 44     @interface DA {}
 45 
 46     @Retention(RetentionPolicy.RUNTIME)
 47     @interface DARR {}
 48 
 49     @Target(ElementType.TYPE_PARAMETER)
 50     @interface TA {}
 51 
 52     @Retention(RetentionPolicy.RUNTIME)
 53     @Target(ElementType.TYPE_PARAMETER)
 54     @interface TARR {}
 55 
 56     @DA @DARR
 57     public primitive class V<@TA @TARR T> {}
 58 
 59     public static void main(String[] args) throws Exception {
 60         ClassFile cls = ClassFile.read(UnannotatedProjection.class.getResourceAsStream("UnannotatedProjection$V.class"));
 61 
 62         if (cls == null) {
 63             throw new AssertionError("Could not locate the class files");
 64         }
 65 
 66         RuntimeInvisibleAnnotations_attribute inv = (RuntimeInvisibleAnnotations_attribute) cls.attributes.get(Attribute.RuntimeInvisibleAnnotations);
 67         if (inv == null || inv.annotations == null || inv.annotations.length != 1) {
 68             throw new AssertionError("Missing annotations");
 69         }
 70 
 71         String aName = cls.constant_pool.getUTF8Value(inv.annotations[0].type_index);
 72         if (!aName.equals("LUnannotatedProjection$DA;")) {
 73             throw new AssertionError("Unexpected annotation: " + aName);
 74         }
 75 
 76         RuntimeInvisibleTypeAnnotations_attribute invta = (RuntimeInvisibleTypeAnnotations_attribute) cls.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations);
 77         if (invta == null || invta.annotations == null || invta.annotations.length != 1) {
 78             throw new AssertionError("Missing annotations");
 79         }
 80 
 81         aName = cls.constant_pool.getUTF8Value(invta.annotations[0].annotation.type_index);
 82         if (!aName.equals("LUnannotatedProjection$TA;")) {
 83             throw new AssertionError("Unexpected annotation: " + aName);
 84         }
 85 
 86         RuntimeVisibleAnnotations_attribute v = (RuntimeVisibleAnnotations_attribute) cls.attributes.get(Attribute.RuntimeVisibleAnnotations);
 87         if (v == null || v.annotations == null || v.annotations.length != 1) {
 88             throw new AssertionError("Missing annotations");
 89         }
 90 
 91         aName = cls.constant_pool.getUTF8Value(v.annotations[0].type_index);
 92         if (!aName.equals("LUnannotatedProjection$DARR;")) {
 93             throw new AssertionError("Unexpected annotation: " + aName);
 94         }
 95 
 96         RuntimeVisibleTypeAnnotations_attribute vta = (RuntimeVisibleTypeAnnotations_attribute) cls.attributes.get(Attribute.RuntimeVisibleTypeAnnotations);
 97         if (vta == null || vta.annotations == null || vta.annotations.length != 1) {
 98             throw new AssertionError("Missing annotations");
 99         }
100 
101         aName = cls.constant_pool.getUTF8Value(vta.annotations[0].annotation.type_index);
102         if (!aName.equals("LUnannotatedProjection$TARR;")) {
103             throw new AssertionError("Unexpected annotation: " + aName);
104         }
105     }
106 }