test/tools/javap/typeAnnotations/TypeCasts.java

changeset 1755
ddb4a2bfcd82
parent 1521
71f35e4b93a5
child 2525
2eb010b6cb22
equal deleted inserted replaced
1754:0384683c64be 1755:ddb4a2bfcd82
39 public void run() throws Exception { 39 public void run() throws Exception {
40 File javaFile = writeTestFile(); 40 File javaFile = writeTestFile();
41 File classFile = compileTestFile(javaFile); 41 File classFile = compileTestFile(javaFile);
42 42
43 ClassFile cf = ClassFile.read(classFile); 43 ClassFile cf = ClassFile.read(classFile);
44 test(cf);
45 for (Field f : cf.fields) {
46 test(cf, f);
47 }
48 for (Method m: cf.methods) { 44 for (Method m: cf.methods) {
49 test(cf, m); 45 test(cf, m);
50 } 46 }
51 47
52 countAnnotations(); 48 countAnnotations();
54 if (errors > 0) 50 if (errors > 0)
55 throw new Exception(errors + " errors found"); 51 throw new Exception(errors + " errors found");
56 System.out.println("PASSED"); 52 System.out.println("PASSED");
57 } 53 }
58 54
59 void test(ClassFile cf) {
60 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
61 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
62 }
63
64 void test(ClassFile cf, Method m) { 55 void test(ClassFile cf, Method m) {
65 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); 56 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
66 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); 57 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
67 } 58 }
68 59
69 void test(ClassFile cf, Field m) {
70 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
71 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
72 }
73
74 // test the result of Attributes.getIndex according to expectations
75 // encoded in the method's name
76 void test(ClassFile cf, String name, boolean visible) {
77 int index = cf.attributes.getIndex(cf.constant_pool, name);
78 if (index != -1) {
79 Attribute attr = cf.attributes.get(index);
80 assert attr instanceof RuntimeTypeAnnotations_attribute;
81 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
82 all += tAttr.annotations.length;
83 if (visible)
84 visibles += tAttr.annotations.length;
85 else
86 invisibles += tAttr.annotations.length;
87 }
88 }
89 60
90 // test the result of Attributes.getIndex according to expectations 61 // test the result of Attributes.getIndex according to expectations
91 // encoded in the method's name 62 // encoded in the method's name
92 void test(ClassFile cf, Method m, String name, boolean visible) { 63 void test(ClassFile cf, Method m, String name, boolean visible) {
93 int index = m.attributes.getIndex(cf.constant_pool, name); 64 Attribute attr = null;
94 if (index != -1) { 65 Code_attribute cAttr = null;
95 Attribute attr = m.attributes.get(index); 66
96 assert attr instanceof RuntimeTypeAnnotations_attribute; 67 int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
97 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; 68 if(index!= -1) {
98 all += tAttr.annotations.length; 69 attr = m.attributes.get(index);
99 if (visible) 70 assert attr instanceof Code_attribute;
100 visibles += tAttr.annotations.length; 71 cAttr = (Code_attribute)attr;
101 else 72 index = cAttr.attributes.getIndex(cf.constant_pool, name);
102 invisibles += tAttr.annotations.length; 73 if(index!= -1) {
74 attr = cAttr.attributes.get(index);
75 assert attr instanceof RuntimeTypeAnnotations_attribute;
76 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
77 all += tAttr.annotations.length;
78 if (visible)
79 visibles += tAttr.annotations.length;
80 else
81 invisibles += tAttr.annotations.length;
82 }
103 } 83 }
104 } 84 }
105 85
106 // test the result of Attributes.getIndex according to expectations
107 // encoded in the method's name
108 void test(ClassFile cf, Field m, String name, boolean visible) {
109 int index = m.attributes.getIndex(cf.constant_pool, name);
110 if (index != -1) {
111 Attribute attr = m.attributes.get(index);
112 assert attr instanceof RuntimeTypeAnnotations_attribute;
113 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
114 all += tAttr.annotations.length;
115 if (visible)
116 visibles += tAttr.annotations.length;
117 else
118 invisibles += tAttr.annotations.length;
119 }
120 }
121 86
122 87
123 File writeTestFile() throws IOException { 88 File writeTestFile() throws IOException {
124 File f = new File("Test.java"); 89 File f = new File("Test.java");
125 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 90 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
126 out.println("import java.lang.annotation.*;"); 91 out.println("import java.lang.annotation.*;");
127 out.println("class Test { "); 92 out.println("class Test { ");
128 out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})"); 93 out.println(" @Target(ElementType.TYPE_USE) @interface A { }");
129 out.println(" @interface A { }");
130 94
131 out.println(" void emit() {"); 95 out.println(" void emit() {");
132 out.println(" Object o = null;"); 96 out.println(" Object o = null;");
133 out.println(" String s = null;"); 97 out.println(" String s = null;");
134 98

mercurial