1 /*
   2  * Copyright (c) 2009, 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 import java.io.*;
  25 import com.sun.tools.classfile.*;
  26 
  27 /*
  28  * @test Wildcards
  29  * @bug 6843077
  30  * @summary test that annotations target wildcards get emitted to classfile
  31  */
  32 public class Wildcards {
  33     public static void main(String[] args) throws Exception {
  34         new Wildcards().run();
  35     }
  36 
  37     public void run() throws Exception {
  38         File javaFile = writeTestFile();
  39         File classFile = compileTestFile(javaFile);
  40 
  41         ClassFile cf = ClassFile.read(classFile);
  42         test(cf);
  43         for (Field f : cf.fields) {
  44             test(cf, f);
  45         }
  46         for (Method m: cf.methods) {
  47             test(cf, m);
  48         }
  49 
  50         countAnnotations();
  51 
  52         if (errors > 0)
  53             throw new Exception(errors + " errors found");
  54         System.out.println("PASSED");
  55     }
  56 
  57     void test(ClassFile cf) {
  58         test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
  59         test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
  60     }
  61 
  62     void test(ClassFile cf, Method m) {
  63         test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
  64         test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
  65     }
  66 
  67     void test(ClassFile cf, Field m) {
  68         test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
  69         test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
  70     }
  71 
  72     // test the result of Attributes.getIndex according to expectations
  73     // encoded in the method's name
  74     void test(ClassFile cf, String name, boolean visible) {
  75         int index = cf.attributes.getIndex(cf.constant_pool, name);
  76         if (index != -1) {
  77             Attribute attr = cf.attributes.get(index);
  78             assert attr instanceof RuntimeTypeAnnotations_attribute;
  79             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
  80             all += tAttr.annotations.length;
  81             if (visible)
  82                 visibles += tAttr.annotations.length;
  83             else
  84                 invisibles += tAttr.annotations.length;
  85         }
  86     }
  87 
  88     // test the result of Attributes.getIndex according to expectations
  89     // encoded in the method's name
  90     void test(ClassFile cf, Method m, String name, boolean visible) {
  91         int index = m.attributes.getIndex(cf.constant_pool, name);
  92         if (index != -1) {
  93             Attribute attr = m.attributes.get(index);
  94             assert attr instanceof RuntimeTypeAnnotations_attribute;
  95             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
  96             all += tAttr.annotations.length;
  97             if (visible)
  98                 visibles += tAttr.annotations.length;
  99             else
 100                 invisibles += tAttr.annotations.length;
 101         }
 102     }
 103 
 104     // test the result of Attributes.getIndex according to expectations
 105     // encoded in the method's name
 106     void test(ClassFile cf, Field m, String name, boolean visible) {
 107         int index = m.attributes.getIndex(cf.constant_pool, name);
 108         if (index != -1) {
 109             Attribute attr = m.attributes.get(index);
 110             assert attr instanceof RuntimeTypeAnnotations_attribute;
 111             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
 112             all += tAttr.annotations.length;
 113             if (visible)
 114                 visibles += tAttr.annotations.length;
 115             else
 116                 invisibles += tAttr.annotations.length;
 117         }
 118     }
 119 
 120     File writeTestFile() throws IOException {
 121       File f = new File("Test.java");
 122         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
 123         out.println("import java.lang.annotation.*;");
 124         out.println("import java.util.*;");
 125         out.println("class Test { ");
 126         out.println("  @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
 127         out.println("  @interface A { }");
 128 
 129         out.println("  List<? extends @A Number> f;");
 130 
 131         out.println("  List<? extends @A Object> test(List<? extends @A Number> p) {");
 132         out.println("    List<? extends @A Object> l;");    // not counted... gets optimized away
 133         out.println("    return null;");
 134         out.println(" }");
 135         out.println("}");
 136 
 137         out.close();
 138         return f;
 139     }
 140 
 141     File compileTestFile(File f) {
 142         int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
 143         if (rc != 0)
 144             throw new Error("compilation failed. rc=" + rc);
 145         String path = f.getPath();
 146         return new File(path.substring(0, path.length() - 5) + ".class");
 147     }
 148 
 149     void countAnnotations() {
 150         int expected_visibles = 0, expected_invisibles = 3;
 151         int expected_all = expected_visibles + expected_invisibles;
 152 
 153         if (expected_all != all) {
 154             errors++;
 155             System.err.println("expected " + expected_all
 156                     + " annotations but found " + all);
 157         }
 158 
 159         if (expected_visibles != visibles) {
 160             errors++;
 161             System.err.println("expected " + expected_visibles
 162                     + " visibles annotations but found " + visibles);
 163         }
 164 
 165         if (expected_invisibles != invisibles) {
 166             errors++;
 167             System.err.println("expected " + expected_invisibles
 168                     + " invisibles annotations but found " + invisibles);
 169         }
 170 
 171     }
 172 
 173     int errors;
 174     int all;
 175     int visibles;
 176     int invisibles;
 177 }