test/tools/javap/typeAnnotations/NewArray.java

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

mercurial