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 V.ref class should not inadvertently carry over attributes from V.class
 29  * @bug 8244713
 30  * @modules jdk.jdeps/com.sun.tools.classfile
 31  * @run main AttributesTest
 32  */
 33 
 34 import com.sun.tools.classfile.*;
 35 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
 36 
 37 public class AttributesTest {
 38 
 39     void foo() {
 40         @Deprecated
 41         primitive class V<T> {}
 42     }
 43 
 44     public static void main(String[] args) throws Exception {
 45         ClassFile cls = ClassFile.read(AttributesTest.class.getResourceAsStream("AttributesTest$1V.class"));
 46 
 47         if (cls == null) {
 48             throw new AssertionError("Could not locate the class files");
 49         }
 50 
 51         /* Check emission of inner class attribute */
 52         InnerClasses_attribute inners = (InnerClasses_attribute) cls.attributes.get(Attribute.InnerClasses);
 53         if (inners == null) {
 54             throw new AssertionError("Missing inner class attribute");
 55         }
 56         if (inners.number_of_classes != 1) {
 57             throw new AssertionError("Unexpected inner class attribute");
 58         }
 59         String name = inners.classes[0].getInnerName(cls.constant_pool);
 60         if (!name.equals("V")) {
 61             throw new AssertionError("Unexpected inner class " + name);
 62         }
 63 
 64         // Test emission of nest host attribute. Nest members attribute tested in InlineNesting*
 65         NestHost_attribute nestHost = (NestHost_attribute)cls.attributes.get(Attribute.NestHost);
 66         CONSTANT_Class_info host = nestHost != null ? nestHost.getNestTop(cls.constant_pool) : null;
 67         if (host == null || !host.getName().equals("AttributesTest")) {
 68             throw new RuntimeException("Wrong Nest host " + host.getName());
 69         }
 70 
 71         // Test signature attribute
 72         Signature_attribute signature = (Signature_attribute)cls.attributes.get(Attribute.Signature);
 73         String sign =  signature.getSignature(cls.constant_pool);
 74         if (sign == null || !sign.equals("<T:Ljava/lang/Object;>Ljava/lang/Object;")) {
 75             throw new RuntimeException("Wrong signature " + sign);
 76         }
 77 
 78         // Test SourceFile attribute
 79         SourceFile_attribute source = (SourceFile_attribute)cls.attributes.get(Attribute.SourceFile);
 80         String src =  source.getSourceFile(cls.constant_pool);
 81         if (src == null || !src.equals("AttributesTest.java")) {
 82             throw new RuntimeException("Wrong source " + src);
 83         }
 84 
 85         // Test Deprecated attribute
 86         Deprecated_attribute depr = (Deprecated_attribute) cls.attributes.get(Attribute.Deprecated);
 87         if (depr == null) {
 88             throw new RuntimeException("Missing deprecated annotation");
 89         }
 90 
 91         // Test EnclosingMethod attribute
 92         EnclosingMethod_attribute meth = (EnclosingMethod_attribute) cls.attributes.get(Attribute.EnclosingMethod);
 93         if (meth == null) {
 94             throw new RuntimeException("Missing enclosing method attribute");
 95         }
 96         String mName = meth.getMethodName(cls.constant_pool);
 97         if (mName == null || !mName.equals("foo")) {
 98             throw new RuntimeException("Wrong enclosing method " + mName);
 99         }
100 
101         // The following attributes should not be present in the projection file.
102         String [] shouldBeAbsent = {
103                                         "SourceDebugExtension",
104                                         "BootstrapMethods",
105                                         "Module",
106                                         "ModulePackages",
107                                         "ModuleMainClass",
108                                         "Synthetic",
109                                    };
110         for (String attr : shouldBeAbsent) {
111             if (cls.getAttribute(attr) != null) {
112                 throw new RuntimeException("Unexpected attribute: " + attr);
113             }
114         }
115     }
116 }