changeset 404:ce5281969d4d

support array class literal - only output known location type annotations
author mali
date Tue, 24 Mar 2009 22:41:22 -0400
parents 6fe31dab5a9c
children 19ffb3fcb045
files src/share/classes/com/sun/tools/classfile/ExtendedAnnotation.java src/share/classes/com/sun/tools/javac/comp/TargetType.java src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java test/tools/javap/typeAnnotations/ClassLiterals.java
diffstat 4 files changed, 182 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/classfile/ExtendedAnnotation.java	Tue Mar 24 17:41:07 2009 -0400
+++ b/src/share/classes/com/sun/tools/classfile/ExtendedAnnotation.java	Tue Mar 24 22:41:22 2009 -0400
@@ -64,7 +64,7 @@
     private static TypeAnnotations.Position read_position(ClassReader cr) throws IOException {
         // Copied from ClassReader
         TypeAnnotations.Position position = new TypeAnnotations.Position();
-        int tag = cr.readUnsignedByte();
+        int tag = (byte)cr.readUnsignedByte();  // cast to introduce signess
         TargetType type = TargetType.fromTargetTypeValue(tag);
 
         position.type = type;
--- a/src/share/classes/com/sun/tools/javac/comp/TargetType.java	Tue Mar 24 17:41:07 2009 -0400
+++ b/src/share/classes/com/sun/tools/javac/comp/TargetType.java	Tue Mar 24 22:41:22 2009 -0400
@@ -135,7 +135,7 @@
     WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, EnumSet.of(HasBound, HasLocation)),
 
     CLASS_LITERAL(0x1E, EnumSet.noneOf(TargetAttribute.class)),
-    //@Deprecated CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, EnumSet.of(HasLocation)),
+    CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, EnumSet.of(HasLocation)),
 
     METHOD_TYPE_PARAMETER(0x20, EnumSet.of(HasParameter)),
     //@Deprecated METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, EnumSet.of(HasLocation, HasParameter)),
--- a/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Mar 24 17:41:07 2009 -0400
+++ b/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Mar 24 22:41:22 2009 -0400
@@ -36,6 +36,7 @@
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.code.Type.*;
+import com.sun.tools.javac.comp.TargetType;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.util.List;
 
@@ -775,7 +776,7 @@
         ListBuffer<Pair<Attribute.Compound, TypeAnnotations.Position>> invisibles = ListBuffer.lb();
 
         for (TypeAnnotations ta : typeAnnos) {
-            if (ta.annotations.isEmpty()) continue;
+            if (ta.annotations.isEmpty() || ta.position.type == TargetType.UNKNOWN) continue;
             for (Attribute.Compound a : ta.annotations) {
                 switch (getRetention(a.type.tsym)) {
                 case SOURCE: break;
@@ -909,10 +910,11 @@
     }
 
     void writeTypeAnnotation(Attribute.Compound c, TypeAnnotations.Position p) {
-        if (debugJSR308)
-            System.out.println("writing " + c + " at " + p);
-        writeCompoundAttribute(c);
-        writePosition(p);
+        // ignore UNKNOWN attributes - improve testing
+      if (debugJSR308)
+        System.out.println("writing " + c + " at " + p);
+      writeCompoundAttribute(c);
+      writePosition(p);
     }
 
     void writePosition(TypeAnnotations.Position p) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javap/typeAnnotations/ClassLiterals.java	Tue Mar 24 22:41:22 2009 -0400
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+import com.sun.tools.classfile.*;
+
+/*
+ * @test ClassLiterals
+ * @summary test that all type annotations are present in the classfile
+ */
+
+public class ClassLiterals {
+    public static void main(String[] args) throws Exception {
+        new ClassLiterals().run();
+    }
+
+    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();
+
+        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("Testa.java");
+        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
+        out.println("import java.util.*;");
+        out.println("class Testa { ");
+        out.println("  @interface A { }");
+
+        out.println(" void test() {");
+        out.println("  Object a = @A String.class;");
+        out.println("  Object b = @A String @A [] @A [].class;");
+        out.println(" }");
+        out.println("}");
+
+        out.close();
+        return f;
+    }
+
+    File compileTestFile(File f) {
+        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
+        if (rc != 0)
+            throw new Error("compilation failed. rc=" + rc);
+        String path = f.getPath();
+        return new File(path.substring(0, path.length() - 5) + ".class");
+    }
+
+    void countAnnotations() {
+        int expected_visibles = 0, expected_invisibles = 4;
+        int expected_all = expected_visibles + expected_invisibles;
+
+        if (expected_all != all) {
+            errors++;
+            System.err.println("expected " + expected_all
+                    + " annotations but found " + all);
+        }
+
+        if (expected_visibles != visibles) {
+            errors++;
+            System.err.println("expected " + expected_visibles
+                    + " visibles annotations but found " + visibles);
+        }
+
+        if (expected_invisibles != invisibles) {
+            errors++;
+            System.err.println("expected " + expected_invisibles
+                    + " invisibles annotations but found " + invisibles);
+        }
+
+    }
+
+    int errors;
+    int all;
+    int visibles;
+    int invisibles;
+}