test/tools/javap/typeAnnotations/TypeCasts.java

Print this page

        

*** 39,52 **** public void run() throws Exception { File javaFile = writeTestFile(); File classFile = compileTestFile(javaFile); ClassFile cf = ClassFile.read(classFile); - test(cf); - for (Field f : cf.fields) { - test(cf, f); - } for (Method m: cf.methods) { test(cf, m); } countAnnotations(); --- 39,48 ----
*** 54,134 **** if (errors > 0) throw new Exception(errors + " errors found"); System.out.println("PASSED"); } - void test(ClassFile cf) { - test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); - test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); - } - void test(ClassFile cf, Method m) { test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); } - void test(ClassFile cf, Field m) { - test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); - test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); - } // test the result of Attributes.getIndex according to expectations // encoded in the method's name - void test(ClassFile cf, String name, boolean visible) { - int index = cf.attributes.getIndex(cf.constant_pool, name); - if (index != -1) { - Attribute attr = cf.attributes.get(index); - assert attr instanceof RuntimeTypeAnnotations_attribute; - RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; - all += tAttr.annotations.length; - if (visible) - visibles += tAttr.annotations.length; - else - invisibles += tAttr.annotations.length; - } - } - - // test the result of Attributes.getIndex according to expectations - // encoded in the method's name void test(ClassFile cf, Method m, String name, boolean visible) { ! int index = m.attributes.getIndex(cf.constant_pool, name); ! if (index != -1) { ! Attribute attr = m.attributes.get(index); ! assert attr instanceof RuntimeTypeAnnotations_attribute; ! RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; ! all += tAttr.annotations.length; ! if (visible) ! visibles += tAttr.annotations.length; ! else ! invisibles += tAttr.annotations.length; ! } ! } ! // test the result of Attributes.getIndex according to expectations ! // encoded in the method's name ! void test(ClassFile cf, Field m, String name, boolean visible) { ! int index = m.attributes.getIndex(cf.constant_pool, name); ! if (index != -1) { ! Attribute attr = m.attributes.get(index); assert attr instanceof RuntimeTypeAnnotations_attribute; RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; all += tAttr.annotations.length; if (visible) visibles += tAttr.annotations.length; else invisibles += tAttr.annotations.length; } } File writeTestFile() throws IOException { File f = new File("Test.java"); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); out.println("import java.lang.annotation.*;"); out.println("class Test { "); ! out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})"); ! out.println(" @interface A { }"); out.println(" void emit() {"); out.println(" Object o = null;"); out.println(" String s = null;"); --- 50,98 ---- if (errors > 0) throw new Exception(errors + " errors found"); System.out.println("PASSED"); } void test(ClassFile cf, Method m) { test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); } // test the result of Attributes.getIndex according to expectations // encoded in the method's name void test(ClassFile cf, Method m, String name, boolean visible) { ! Attribute attr = null; ! Code_attribute cAttr = null; ! int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); ! if(index!= -1) { ! attr = m.attributes.get(index); ! assert attr instanceof Code_attribute; ! cAttr = (Code_attribute)attr; ! index = cAttr.attributes.getIndex(cf.constant_pool, name); ! if(index!= -1) { ! attr = cAttr.attributes.get(index); assert attr instanceof RuntimeTypeAnnotations_attribute; RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; all += tAttr.annotations.length; if (visible) visibles += tAttr.annotations.length; else invisibles += tAttr.annotations.length; } } + } + File writeTestFile() throws IOException { File f = new File("Test.java"); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); out.println("import java.lang.annotation.*;"); out.println("class Test { "); ! out.println(" @Target(ElementType.TYPE_USE) @interface A { }"); out.println(" void emit() {"); out.println(" Object o = null;"); out.println(" String s = null;");