test/langtools/tools/javac/T8187805/BogusRTTAForUnusedVarTest.java
changeset 48513 5b834ec96236
equal deleted inserted replaced
48512:e8e8c9e6ccf8 48513:5b834ec96236
       
     1 /*
       
     2  * Copyright (c) 2018, 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 8187805
       
    26  * @summary bogus RuntimeVisibleTypeAnnotations for unused local in a block
       
    27  * @modules jdk.jdeps/com.sun.tools.classfile
       
    28  *          jdk.compiler/com.sun.tools.javac.util
       
    29  * @run main BogusRTTAForUnusedVarTest
       
    30  */
       
    31 
       
    32 import com.sun.tools.classfile.*;
       
    33 
       
    34 import java.io.File;
       
    35 import java.lang.annotation.Target;
       
    36 import java.lang.annotation.ElementType;
       
    37 import java.lang.annotation.Retention;
       
    38 import java.lang.annotation.RetentionPolicy;
       
    39 
       
    40 import com.sun.tools.classfile.Attribute;
       
    41 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
       
    42 import com.sun.tools.classfile.TypeAnnotation;
       
    43 import com.sun.tools.classfile.TypeAnnotation.Position;
       
    44 import com.sun.tools.javac.util.Assert;
       
    45 
       
    46 public class BogusRTTAForUnusedVarTest {
       
    47 
       
    48     class Foo {
       
    49         void something() {
       
    50             {
       
    51                 @MyAnno Object o = new Object();
       
    52             }
       
    53         }
       
    54     }
       
    55 
       
    56     @Target(ElementType.TYPE_USE)
       
    57     @Retention(RetentionPolicy.RUNTIME)
       
    58     @interface MyAnno {}
       
    59 
       
    60     public static void main(String args[]) throws Throwable {
       
    61         new BogusRTTAForUnusedVarTest().run();
       
    62     }
       
    63 
       
    64     void run() throws Throwable {
       
    65         checkRTTA();
       
    66     }
       
    67 
       
    68     void checkRTTA() throws Throwable {
       
    69         File testClasses = new File(System.getProperty("test.classes"));
       
    70         File file = new File(testClasses,
       
    71                 BogusRTTAForUnusedVarTest.class.getName() + "$Foo.class");
       
    72         ClassFile classFile = ClassFile.read(file);
       
    73         for (Method m : classFile.methods) {
       
    74             if (m.getName(classFile.constant_pool).equals("something")) {
       
    75                 for (Attribute a : m.attributes) {
       
    76                     if (a.getName(classFile.constant_pool).equals("Code")) {
       
    77                         Code_attribute code = (Code_attribute)a;
       
    78                         for (Attribute codeAttrs : code.attributes) {
       
    79                             if (codeAttrs.getName(classFile.constant_pool).equals("RuntimeVisibleTypeAnnotations")) {
       
    80                                 throw new AssertionError("no RuntimeVisibleTypeAnnotations attribute should have been generated in this case");
       
    81                             }
       
    82                         }
       
    83                     }
       
    84                 }
       
    85             }
       
    86         }
       
    87     }
       
    88 }