langtools/test/tools/javac/T6695379/AnnotationsAreNotCopiedToBridgeMethodsTest.java
changeset 18000 5d29ce00a7a2
child 30730 d3ce7619db2c
equal deleted inserted replaced
17999:42ae6fe53718 18000:5d29ce00a7a2
       
     1 /*
       
     2  * Copyright (c) 2013, 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  * @bug 6695379
       
    27  * @summary Copy method annotations and parameter annotations to synthetic
       
    28  * bridge methods
       
    29  * @run main AnnotationsAreNotCopiedToBridgeMethodsTest
       
    30  */
       
    31 
       
    32 import java.lang.annotation.ElementType;
       
    33 import java.lang.annotation.Target;
       
    34 import java.io.BufferedInputStream;
       
    35 import java.lang.annotation.Retention;
       
    36 import java.lang.annotation.RetentionPolicy;
       
    37 import java.nio.file.Files;
       
    38 import java.nio.file.Path;
       
    39 import java.nio.file.Paths;
       
    40 
       
    41 import com.sun.tools.classfile.AccessFlags;
       
    42 import com.sun.tools.classfile.Attribute;
       
    43 import com.sun.tools.classfile.Attributes;
       
    44 import com.sun.tools.classfile.ClassFile;
       
    45 import com.sun.tools.classfile.Method;
       
    46 import com.sun.tools.javac.util.Assert;
       
    47 
       
    48 public class AnnotationsAreNotCopiedToBridgeMethodsTest {
       
    49 
       
    50     public static void main(String[] args) throws Exception {
       
    51         new AnnotationsAreNotCopiedToBridgeMethodsTest().run();
       
    52     }
       
    53 
       
    54     void run() throws Exception {
       
    55         checkClassFile(Paths.get(System.getProperty("test.classes"),
       
    56                 this.getClass().getSimpleName() + "$CovariantReturnType.class"));
       
    57         checkClassFile(Paths.get(System.getProperty("test.classes"),
       
    58                 this.getClass().getSimpleName() +
       
    59                 "$CovariantReturnType$VisibilityChange.class"));
       
    60     }
       
    61 
       
    62     void checkClassFile(final Path cfilePath) throws Exception {
       
    63         ClassFile classFile = ClassFile.read(
       
    64                 new BufferedInputStream(Files.newInputStream(cfilePath)));
       
    65         for (Method method : classFile.methods) {
       
    66             if (method.access_flags.is(AccessFlags.ACC_BRIDGE)) {
       
    67                 checkForAttr(method.attributes,
       
    68                         "Annotations hasn't been copied to bridge method",
       
    69                         Attribute.RuntimeVisibleAnnotations,
       
    70                         Attribute.RuntimeVisibleParameterAnnotations);
       
    71             }
       
    72         }
       
    73     }
       
    74 
       
    75     void checkForAttr(Attributes attrs, String errorMsg, String... attrNames) {
       
    76         for (String attrName : attrNames) {
       
    77             Assert.checkNonNull(attrs.get(attrName), errorMsg);
       
    78         }
       
    79     }
       
    80 
       
    81     @Target(value = {ElementType.PARAMETER})
       
    82     @Retention(RetentionPolicy.RUNTIME)
       
    83     @interface ParamAnnotation {}
       
    84 
       
    85     @Target(value = {ElementType.METHOD})
       
    86     @Retention(RetentionPolicy.RUNTIME)
       
    87     @interface MethodAnnotation {}
       
    88 
       
    89     abstract class T<A,B> {
       
    90         B m(A a){return null;}
       
    91     }
       
    92 
       
    93     class CovariantReturnType extends T<Integer, Integer> {
       
    94         @MethodAnnotation
       
    95         Integer m(@ParamAnnotation Integer i) {
       
    96             return i;
       
    97         }
       
    98 
       
    99         public class VisibilityChange extends CovariantReturnType {}
       
   100 
       
   101     }
       
   102 
       
   103 }