Path: blob/master/test/langtools/tools/javap/AnnoTest.java
41144 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815669426* @summary javap should render annotations in a friendly way27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.lang.annotation.*;32import javax.lang.model.element.ElementKind;3334public class AnnoTest {35public static void main(String... args) throws Exception {36new AnnoTest().run();37}3839void run() throws Exception {40String testClasses = System.getProperty("test.classes");41String out = javap("-v", "-classpath", testClasses, A.class.getName());4243String nl = System.getProperty("line.separator");44out = out.replaceAll(nl, "\n");4546if (out.contains("\n\n\n"))47error("double blank line found");4849expect(out,50"RuntimeVisibleAnnotations:\n" +51" 0: #21(#22=B#23)\n" +52" AnnoTest$ByteAnno(\n" +53" value=(byte) 42\n" +54" )\n" +55" 1: #24(#22=S#25)\n" +56" AnnoTest$ShortAnno(\n" +57" value=(short) 3\n" +58" )");59expect(out,60"RuntimeInvisibleAnnotations:\n" +61" 0: #27(#22=[J#28,J#30,J#32,J#34,J#36])\n" +62" AnnoTest$ArrayAnno(\n" +63" value=[1l,2l,3l,4l,5l]\n" +64" )\n" +65" 1: #38(#22=Z#39)\n" +66" AnnoTest$BooleanAnno(\n" +67" value=false\n" +68" )\n" +69" 2: #40(#41=c#42)\n" +70" AnnoTest$ClassAnno(\n" +71" type=class Ljava/lang/Object;\n" +72" )\n" +73" 3: #43(#44=e#45.#46)\n" +74" AnnoTest$EnumAnno(\n" +75" kind=Ljavax/lang/model/element/ElementKind;.PACKAGE\n" +76" )\n" +77" 4: #47(#22=I#48)\n" +78" AnnoTest$IntAnno(\n" +79" value=2\n" +80" )\n" +81" 5: #49()\n" +82" AnnoTest$IntDefaultAnno\n" +83" 6: #50(#51=s#52)\n" +84" AnnoTest$NameAnno(\n" +85" name=\"NAME\"\n" +86" )\n" +87" 7: #53(#54=D#55,#57=F#58)\n" +88" AnnoTest$MultiAnno(\n" +89" d=3.14159d\n" +90" f=2.71828f\n" +91" )\n" +92" 8: #59()\n" +93" AnnoTest$SimpleAnno\n" +94" 9: #60(#22=@#47(#22=I#61))\n" +95" AnnoTest$AnnoAnno(\n" +96" value=@AnnoTest$IntAnno(\n" +97" value=5\n" +98" )\n" +99" )");100expect(out,101"RuntimeInvisibleTypeAnnotations:\n" +102" 0: #63(): CLASS_EXTENDS, type_index=0\n" +103" AnnoTest$TypeAnno");104105if (errors > 0)106throw new Exception(errors + " errors found");107}108109String javap(String... args) throws Exception {110StringWriter sw = new StringWriter();111int rc;112try (PrintWriter out = new PrintWriter(sw)) {113rc = com.sun.tools.javap.Main.run(args, out);114}115System.out.println(sw.toString());116if (rc < 0)117throw new Exception("javap exited, rc=" + rc);118return sw.toString();119}120121void expect(String text, String expect) {122if (!text.contains(expect))123error("expected text not found");124}125126void error(String msg) {127System.out.println("Error: " + msg);128errors++;129}130131int errors;132133/* Simple test classes to run through javap. */134public @interface SimpleAnno { }135public @interface BooleanAnno { boolean value(); }136public @interface IntAnno { int value(); }137public @interface IntDefaultAnno { int value() default 3; }138139@Retention(RetentionPolicy.RUNTIME)140public @interface ByteAnno { byte value(); }141142@Retention(RetentionPolicy.RUNTIME)143public @interface ShortAnno { short value(); }144145public @interface NameAnno { String name(); }146public @interface ArrayAnno { long[] value(); }147public @interface EnumAnno { ElementKind kind(); }148public @interface ClassAnno { Class<?> type(); }149public @interface MultiAnno { float f(); double d(); }150151public @interface AnnoAnno { IntAnno value(); }152153@Target(ElementType.TYPE_USE)154public @interface TypeAnno { }155156@ArrayAnno({1, 2, 3, 4, 5})157@BooleanAnno(false)158@ByteAnno(42)159@ClassAnno(type = Object.class)160@EnumAnno(kind = ElementKind.PACKAGE)161@IntAnno(2)162@IntDefaultAnno163@NameAnno(name = "NAME")164@MultiAnno(d = 3.14159, f = 2.71828f)165@ShortAnno(3)166@SimpleAnno167@AnnoAnno(@IntAnno(5))168public abstract class A implements @TypeAnno Runnable { }169}170171172