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