8156694: javap should render annotations in a friendly way
authorjjg
Tue, 21 Nov 2017 13:06:43 -0800
changeset 47875 93bba74ed8a3
parent 47873 7944849362f3
child 47876 5d4864563be7
8156694: javap should render annotations in a friendly way Reviewed-by: mcimadamore
src/jdk.jdeps/share/classes/com/sun/tools/javap/AnnotationWriter.java
src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java
test/langtools/tools/javap/AnnoTest.java
test/langtools/tools/javap/typeAnnotations/InvisibleParameterAnnotationsTest.java
--- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/AnnotationWriter.java	Tue Nov 21 10:26:45 2017 +0100
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/AnnotationWriter.java	Tue Nov 21 13:06:43 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. 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
@@ -61,24 +61,45 @@
 
     public void write(Annotation annot) {
         write(annot, false);
+        println();
+        indent(+1);
+        write(annot, true);
+        indent(-1);
     }
 
     public void write(Annotation annot, boolean resolveIndices) {
         writeDescriptor(annot.type_index, resolveIndices);
-        boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
-        if (showParens)
+        if (resolveIndices) {
+            boolean showParens = annot.num_element_value_pairs > 0;
+            if (showParens) {
+                println("(");
+                indent(+1);
+            }
+            for (int i = 0; i < annot.num_element_value_pairs; i++) {
+                write(annot.element_value_pairs[i], true);
+                println();
+            }
+            if (showParens) {
+                indent(-1);
+                print(")");
+            }
+        } else {
             print("(");
-        for (int i = 0; i < annot.num_element_value_pairs; i++) {
-            if (i > 0)
-                print(",");
-            write(annot.element_value_pairs[i], resolveIndices);
+            for (int i = 0; i < annot.num_element_value_pairs; i++) {
+                if (i > 0)
+                    print(",");
+                write(annot.element_value_pairs[i], false);
+            }
+            print(")");
         }
-        if (showParens)
-            print(")");
     }
 
     public void write(TypeAnnotation annot) {
         write(annot, true, false);
+        println();
+        indent(+1);
+        write(annot.annotation, true);
+        indent(-1);
     }
 
     public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {
@@ -194,10 +215,6 @@
         }
     }
 
-    public void write(Annotation.element_value_pair pair) {
-        write(pair, false);
-    }
-
     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
         writeIndex(pair.element_name_index, resolveIndices);
         print("=");
@@ -206,6 +223,10 @@
 
     public void write(Annotation.element_value value) {
         write(value, false);
+        println();
+        indent(+1);
+        write(value, true);
+        indent(-1);
     }
 
     public void write(Annotation.element_value value, boolean resolveIndices) {
@@ -240,39 +261,79 @@
             value.accept(this, resolveIndices);
         }
 
+        @Override
         public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
-            if (resolveIndices)
-                writeIndex(ev.const_value_index, resolveIndices);
-            else
+            if (resolveIndices) {
+                int index = ev.const_value_index;
+                switch (ev.tag) {
+                    case 'B':
+                        print("(byte) ");
+                        print(constantWriter.stringValue(index));
+                        break;
+                    case 'C':
+                        print("'");
+                        print(constantWriter.charValue(index));
+                        print("'");
+                        break;
+                    case 'D':
+                    case 'F':
+                    case 'I':
+                    case 'J':
+                        print(constantWriter.stringValue(index));
+                        break;
+                    case 'S':
+                        print("(short) ");
+                        print(constantWriter.stringValue(index));
+                        break;
+                    case 'Z':
+                        print(constantWriter.booleanValue(index));
+                        break;
+                    case 's':
+                        print("\"");
+                        print(constantWriter.stringValue(index));
+                        print("\"");
+                        break;
+                    default:
+                        print(((char) ev.tag) + "#" + ev.const_value_index);
+                        break;
+                }
+            } else {
                 print(((char) ev.tag) + "#" + ev.const_value_index);
+            }
             return null;
         }
 
+        @Override
         public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
             if (resolveIndices) {
                 writeIndex(ev.type_name_index, resolveIndices);
                 print(".");
                 writeIndex(ev.const_name_index, resolveIndices);
-            } else
+            } else {
                 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
+            }
             return null;
         }
 
+        @Override
         public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
             if (resolveIndices) {
+                print("class ");
                 writeIndex(ev.class_info_index, resolveIndices);
-                print(".class");
-            } else
+            } else {
                 print(((char) ev.tag) + "#" + ev.class_info_index);
+            }
             return null;
         }
 
+        @Override
         public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
             print((char) ev.tag);
             AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
             return null;
         }
 
+        @Override
         public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
             print("[");
             for (int i = 0; i < ev.num_values; i++) {
@@ -286,6 +347,6 @@
 
     }
 
-    private ClassWriter classWriter;
-    private ConstantWriter constantWriter;
+    private final ClassWriter classWriter;
+    private final ConstantWriter constantWriter;
 }
--- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java	Tue Nov 21 10:26:45 2017 +0100
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java	Tue Nov 21 13:06:43 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. 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
@@ -253,6 +253,38 @@
         }
     }
 
+    String booleanValue(int constant_pool_index) {
+        ClassFile classFile = classWriter.getClassFile();
+        try {
+            CPInfo info = classFile.constant_pool.get(constant_pool_index);
+            if (info instanceof CONSTANT_Integer_info) {
+                int value = ((CONSTANT_Integer_info) info).value;
+               switch (value) {
+                   case 0: return "false";
+                   case 1: return "true";
+               }
+            }
+            return "#" + constant_pool_index;
+        } catch (ConstantPool.InvalidIndex e) {
+            return report(e);
+        }
+    }
+
+    String charValue(int constant_pool_index) {
+        ClassFile classFile = classWriter.getClassFile();
+        try {
+            CPInfo info = classFile.constant_pool.get(constant_pool_index);
+            if (info instanceof CONSTANT_Integer_info) {
+                int value = ((CONSTANT_Integer_info) info).value;
+                return String.valueOf((char) value);
+            } else {
+                return "#" + constant_pool_index;
+            }
+        } catch (ConstantPool.InvalidIndex e) {
+            return report(e);
+        }
+    }
+
     String stringValue(int constant_pool_index) {
         ClassFile classFile = classWriter.getClassFile();
         try {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javap/AnnoTest.java	Tue Nov 21 13:06:43 2017 -0800
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8156694
+ * @summary javap should render annotations in a friendly way
+ * @modules jdk.jdeps/com.sun.tools.javap
+ */
+
+import java.io.*;
+import java.lang.annotation.*;
+import javax.lang.model.element.ElementKind;
+
+public class AnnoTest {
+    public static void main(String... args) throws Exception {
+        new AnnoTest().run();
+    }
+
+    void run() throws Exception {
+        String testClasses = System.getProperty("test.classes");
+        String out = javap("-v", "-classpath", testClasses, A.class.getName());
+
+        String nl = System.getProperty("line.separator");
+        out = out.replaceAll(nl, "\n");
+
+        if (out.contains("\n\n\n"))
+            error("double blank line found");
+
+        expect(out,
+                "RuntimeVisibleAnnotations:\n" +
+                "  0: #18(#19=B#20)\n" +
+                "    AnnoTest$ByteAnno(\n" +
+                "      value=(byte) 42\n" +
+                "    )\n" +
+                "  1: #23(#19=S#24)\n" +
+                "    AnnoTest$ShortAnno(\n" +
+                "      value=(short) 3\n" +
+                "    )");
+        expect(out,
+                "RuntimeInvisibleAnnotations:\n" +
+                "  0: #28(#19=[J#29,J#31,J#33,J#35,J#37])\n" +
+                "    AnnoTest$ArrayAnno(\n" +
+                "      value=[1l,2l,3l,4l,5l]\n" +
+                "    )\n" +
+                "  1: #41(#19=Z#42)\n" +
+                "    AnnoTest$BooleanAnno(\n" +
+                "      value=false\n" +
+                "    )\n" +
+                "  2: #45(#46=c#47)\n" +
+                "    AnnoTest$ClassAnno(\n" +
+                "      type=class Ljava/lang/Object;\n" +
+                "    )\n" +
+                "  3: #50(#51=e#52.#53)\n" +
+                "    AnnoTest$EnumAnno(\n" +
+                "      kind=Ljavax/lang/model/element/ElementKind;.PACKAGE\n" +
+                "    )\n" +
+                "  4: #56(#19=I#57)\n" +
+                "    AnnoTest$IntAnno(\n" +
+                "      value=2\n" +
+                "    )\n" +
+                "  5: #60()\n" +
+                "    AnnoTest$IntDefaultAnno\n" +
+                "  6: #63(#64=s#65)\n" +
+                "    AnnoTest$NameAnno(\n" +
+                "      name=\"NAME\"\n" +
+                "    )\n" +
+                "  7: #68(#69=D#70,#72=F#73)\n" +
+                "    AnnoTest$MultiAnno(\n" +
+                "      d=3.14159d\n" +
+                "      f=2.71828f\n" +
+                "    )\n" +
+                "  8: #76()\n" +
+                "    AnnoTest$SimpleAnno\n" +
+                "  9: #79(#19=@#56(#19=I#80))\n" +
+                "    AnnoTest$AnnoAnno(\n" +
+                "      value=@AnnoTest$IntAnno(\n" +
+                "        value=5\n" +
+                "      )\n" +
+                "    )");
+        expect(out,
+                "RuntimeInvisibleTypeAnnotations:\n" +
+                "  0: #84(): CLASS_EXTENDS, type_index=0\n" +
+                "    AnnoTest$TypeAnno");
+
+        if (errors > 0)
+            throw new Exception(errors + " errors found");
+    }
+
+    String javap(String... args) throws Exception {
+        StringWriter sw = new StringWriter();
+        int rc;
+        try (PrintWriter out = new PrintWriter(sw)) {
+            rc = com.sun.tools.javap.Main.run(args, out);
+        }
+        System.out.println(sw.toString());
+        if (rc < 0)
+            throw new Exception("javap exited, rc=" + rc);
+        return sw.toString();
+    }
+
+    void expect(String text, String expect) {
+        if (!text.contains(expect))
+            error("expected text not found");
+    }
+
+    void error(String msg) {
+        System.out.println("Error: " + msg);
+        errors++;
+    }
+
+    int errors;
+
+    /* Simple test classes to run through javap. */
+    public @interface SimpleAnno { }
+    public @interface BooleanAnno { boolean value(); }
+    public @interface IntAnno { int value(); }
+    public @interface IntDefaultAnno { int value() default 3; }
+
+    @Retention(RetentionPolicy.RUNTIME)
+    public @interface ByteAnno { byte value(); }
+
+    @Retention(RetentionPolicy.RUNTIME)
+    public @interface ShortAnno { short value(); }
+
+    public @interface NameAnno { String name(); }
+    public @interface ArrayAnno { long[] value(); }
+    public @interface EnumAnno { ElementKind kind(); }
+    public @interface ClassAnno { Class<?> type(); }
+    public @interface MultiAnno { float f(); double d(); }
+
+    public @interface AnnoAnno { IntAnno value(); }
+
+    @Target(ElementType.TYPE_USE)
+    public @interface TypeAnno { }
+
+    @ArrayAnno({1, 2, 3, 4, 5})
+    @BooleanAnno(false)
+    @ByteAnno(42)
+    @ClassAnno(type = Object.class)
+    @EnumAnno(kind = ElementKind.PACKAGE)
+    @IntAnno(2)
+    @IntDefaultAnno
+    @NameAnno(name = "NAME")
+    @MultiAnno(d = 3.14159, f = 2.71828f)
+    @ShortAnno(3)
+    @SimpleAnno
+    @AnnoAnno(@IntAnno(5))
+    public abstract class A implements @TypeAnno Runnable { }
+}
--- a/test/langtools/tools/javap/typeAnnotations/InvisibleParameterAnnotationsTest.java	Tue Nov 21 10:26:45 2017 +0100
+++ b/test/langtools/tools/javap/typeAnnotations/InvisibleParameterAnnotationsTest.java	Tue Nov 21 13:06:43 2017 -0800
@@ -65,9 +65,11 @@
             "      parameter 0:\n" +
             "      parameter 1:\n" +
             "        0: #16()\n" +
+            "          Sample$VisAnno\n" +
             "    RuntimeInvisibleParameterAnnotations:\n" +
             "      parameter 0:\n" +
             "        0: #18()\n" +
+            "          Sample$InvisAnno\n" +
             "      parameter 1:";
 
     public static void main(String[] args) throws Exception {
@@ -78,6 +80,7 @@
                 .options("-v")
                 .classes("Sample.class")
                 .run()
+                .writeAll()
                 .getOutputLines(Task.OutputKind.DIRECT);
 
         List<String> expectedList = tb.split(ExpectedSubstring, "\n");