test/tools/javap/typeAnnotations/Presence.java

Print this page




  74 
  75     // test the result of Attributes.getIndex according to expectations
  76     // encoded in the method's name
  77     void test(ClassFile cf, String name, boolean visible) {
  78         int index = cf.attributes.getIndex(cf.constant_pool, name);
  79         if (index != -1) {
  80             Attribute attr = cf.attributes.get(index);
  81             assert attr instanceof RuntimeTypeAnnotations_attribute;
  82             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
  83             all += tAttr.annotations.length;
  84             if (visible)
  85                 visibles += tAttr.annotations.length;
  86             else
  87                 invisibles += tAttr.annotations.length;
  88         }
  89     }
  90 
  91     // test the result of Attributes.getIndex according to expectations
  92     // encoded in the method's name
  93     void test(ClassFile cf, Method m, String name, boolean visible) {





  94         int index = m.attributes.getIndex(cf.constant_pool, name);
  95         if (index != -1) {
  96             Attribute attr = m.attributes.get(index);
  97             assert attr instanceof RuntimeTypeAnnotations_attribute;
  98             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
  99             all += tAttr.annotations.length;
 100             if (visible)
 101                 visibles += tAttr.annotations.length;
 102             else
 103                 invisibles += tAttr.annotations.length;
 104         }
















 105     }


 106 
 107     // test the result of Attributes.getIndex according to expectations
 108     // encoded in the method's name
 109     void test(ClassFile cf, Field m, String name, boolean visible) {
 110         int index = m.attributes.getIndex(cf.constant_pool, name);
 111         if (index != -1) {
 112             Attribute attr = m.attributes.get(index);
 113             assert attr instanceof RuntimeTypeAnnotations_attribute;
 114             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
 115             all += tAttr.annotations.length;
 116             if (visible)
 117                 visibles += tAttr.annotations.length;
 118             else
 119                 invisibles += tAttr.annotations.length;
 120         }
 121     }
 122 
 123     File writeTestFile() throws IOException {
 124         File f = new File("Test.java");
 125         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
 126         out.println("import java.util.*;");
 127         out.println("import java.lang.annotation.*;");
 128 
 129         out.println("class Test<@Test.A T extends @Test.A List<@Test.A String>> { ");
 130         out.println("  @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
 131         out.println("  @interface A { }");
 132 
 133         out.println("  Map<@A String, Map<@A String, @A String>> f1;");
 134 
 135         out.println("  <@A TM extends @A List<@A String>>");
 136         out.println("  Map<@A String, @A List<@A String>>");
 137         out.println("  method(@A Test<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)");
 138         out.println("  throws @A Exception {");
 139         out.println("    @A String lc1 = null;");
 140         out.println("    @A List<@A String> lc2 = null;");
 141         out.println("    @A String @A [] [] @A[] lc3 = null;");
 142         out.println("    List<? extends @A List<@A String>> lc4 = null;");
 143         out.println("    Object lc5 = (@A List<@A String>) null;");
 144         out.println("    boolean lc6 = lc1 instanceof @A String;");
 145         out.println("    boolean lc7 = lc5 instanceof @A String @A [] @A [];");
 146         out.println("    new @A ArrayList<@A String>();");
 147         out.println("    Object lc8 = new @A String @A [4];");
 148         out.println("    try {");
 149         out.println("      Object lc10 = int.class;");
 150         out.println("    } catch (@A Exception e) { e.toString(); }");
 151         out.println("    return null;");
 152         out.println("  }");
 153         out.println("  void vararg1(String @A ... t) { } ");
 154         out.println("}");
 155         out.close();
 156         return f;
 157     }




  74 
  75     // test the result of Attributes.getIndex according to expectations
  76     // encoded in the method's name
  77     void test(ClassFile cf, String name, boolean visible) {
  78         int index = cf.attributes.getIndex(cf.constant_pool, name);
  79         if (index != -1) {
  80             Attribute attr = cf.attributes.get(index);
  81             assert attr instanceof RuntimeTypeAnnotations_attribute;
  82             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
  83             all += tAttr.annotations.length;
  84             if (visible)
  85                 visibles += tAttr.annotations.length;
  86             else
  87                 invisibles += tAttr.annotations.length;
  88         }
  89     }
  90 
  91     // test the result of Attributes.getIndex according to expectations
  92     // encoded in the method's name
  93     void test(ClassFile cf, Method m, String name, boolean visible) {
  94         Attribute attr = null;
  95         Code_attribute cAttr = null;
  96         RuntimeTypeAnnotations_attribute tAttr = null;
  97 
  98         // collect annotations attributes on method
  99         int index = m.attributes.getIndex(cf.constant_pool, name);
 100         if (index != -1) {
 101             attr = m.attributes.get(index);
 102             assert attr instanceof RuntimeTypeAnnotations_attribute;
 103             tAttr = (RuntimeTypeAnnotations_attribute)attr;
 104             all += tAttr.annotations.length;
 105             if (visible)
 106                 visibles += tAttr.annotations.length;
 107             else
 108                 invisibles += tAttr.annotations.length;
 109         }
 110         // collect annotations from method's code attribute
 111         index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
 112         if(index!= -1) {
 113             attr = m.attributes.get(index);
 114             assert attr instanceof Code_attribute;
 115             cAttr = (Code_attribute)attr;
 116             index = cAttr.attributes.getIndex(cf.constant_pool, name); 
 117             if(index!= -1) {
 118                 attr = cAttr.attributes.get(index);
 119                 assert attr instanceof RuntimeTypeAnnotations_attribute;
 120                 tAttr = (RuntimeTypeAnnotations_attribute)attr;
 121                 all += tAttr.annotations.length;
 122                 if (visible)
 123                     visibles += tAttr.annotations.length;
 124                 else
 125                     invisibles += tAttr.annotations.length;
 126                }
 127         }        
 128     }
 129 
 130     // test the result of Attributes.getIndex according to expectations
 131     // encoded in the method's name
 132     void test(ClassFile cf, Field m, String name, boolean visible) {
 133         int index = m.attributes.getIndex(cf.constant_pool, name);
 134         if (index != -1) {
 135             Attribute attr = m.attributes.get(index);
 136             assert attr instanceof RuntimeTypeAnnotations_attribute;
 137             RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
 138             all += tAttr.annotations.length;
 139             if (visible)
 140                 visibles += tAttr.annotations.length;
 141             else
 142                 invisibles += tAttr.annotations.length;
 143         }
 144     }
 145 
 146     File writeTestFile() throws IOException {
 147         File f = new File("TestPresence.java");
 148         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
 149         out.println("import java.util.*;");
 150         out.println("import java.lang.annotation.*;");
 151 
 152         out.println("class TestPresence<@TestPresence.A T extends @TestPresence.A List<@TestPresence.A String>> { ");
 153         out.println("  @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
 154         out.println("  @interface A { }");
 155 
 156         out.println("  Map<@A String, Map<@A String, @A String>> f1;");
 157 
 158         out.println("  <@A TM extends @A List<@A String>>");
 159         out.println("  Map<@A String, @A List<@A String>>");
 160         out.println("  method(@A TestPresence<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)");
 161         out.println("  throws @A Exception {");
 162         out.println("    @A String lc1 = null;");
 163         out.println("    @A List<@A String> lc2 = null;");
 164         out.println("    @A String @A [] [] @A[] lc3 = null;");
 165         out.println("    List<? extends @A List<@A String>> lc4 = null;");
 166         out.println("    Object lc5 = (@A List<@A String>) null;");
 167         out.println("    boolean lc6 = lc1 instanceof @A String;");
 168         out.println("    boolean lc7 = lc5 instanceof @A String @A [] @A [];");
 169         out.println("    new @A ArrayList<@A String>();");
 170         out.println("    Object lc8 = new @A String @A [4];");
 171         out.println("    try {");
 172         out.println("      Object lc10 = int.class;");
 173         out.println("    } catch (@A Exception e) { e.toString(); }");
 174         out.println("    return null;");
 175         out.println("  }");
 176         out.println("  void vararg1(String @A ... t) { } ");
 177         out.println("}");
 178         out.close();
 179         return f;
 180     }