Path: blob/master/test/jdk/java/lang/annotation/AnnotationToStringTest.java
41149 views
/*1* Copyright (c) 2016, 2019, 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 8162817 816892126* @summary Test of toString on normal annotations27*/2829// See also the sibling compile-time test30// test/langtools/tools/javac/processing/model/element/AnnotationToStringTest.java3132import java.lang.annotation.*;33import java.lang.reflect.*;34import java.util.*;3536/**37* The expected string values are stored in @ExpectedString38* annotations. The essence of the test is comparing the toString()39* result of annotations to the corresponding ExpectedString.value().40*/4142public class AnnotationToStringTest {43public static void main(String... args) throws Exception {44int failures = 0;4546failures += check(PrimHost.class.getAnnotation(ExpectedString.class).value(),47PrimHost.class.getAnnotation(MostlyPrimitive.class).toString());48failures += classyTest();49failures += arrayAnnotationTest();5051if (failures > 0)52throw new RuntimeException(failures + " failures");53}5455private static int check(String expected, String actual) {56if (!expected.equals(actual)) {57System.err.printf("ERROR: Expected ''%s'';%ngot ''%s''.\n",58expected, actual);59return 1;60} else61return 0;62}6364@ExpectedString(65"@MostlyPrimitive(c0='a', "+66"c1='\\'', " +67"b0=(byte)0x01, " +68"i0=1, " +69"i1=2, " +70"f0=1.0f, " +71"f1=0.0f/0.0f, " +72"d0=0.0, " +73"d1=1.0/0.0, " +74"l0=5L, " +75"l1=9223372036854775807L, " +76"l2=-9223372036854775808L, " +77"l3=-2147483648L, " +78"s0=\"Hello world.\", " +79"s1=\"a\\\"b\", " +80"class0=Obj[].class, " +81"classArray={Obj[].class})")82@MostlyPrimitive(83c0='a',84c1='\'',85b0=1,86i0=1,87i1=2,88f0=1.0f,89f1=Float.NaN,90d0=0.0,91d1=2.0/0.0,92l0=5,93l1=Long.MAX_VALUE,94l2=Long.MIN_VALUE,95l3=Integer.MIN_VALUE,96s0="Hello world.",97s1="a\"b",98class0=Obj[].class,99classArray={Obj[].class}100)101static class PrimHost{}102103private static int classyTest() {104int failures = 0;105for (Field f : AnnotationHost.class.getFields()) {106Annotation a = f.getAnnotation(Classy.class);107System.out.println(a);108failures += check(f.getAnnotation(ExpectedString.class).value(),109a.toString());110}111return failures;112}113114static class AnnotationHost {115@ExpectedString(116"@Classy(Obj.class)")117@Classy(Obj.class)118public int f0;119120@ExpectedString(121"@Classy(Obj[].class)")122@Classy(Obj[].class)123public int f1;124125@ExpectedString(126"@Classy(Obj[][].class)")127@Classy(Obj[][].class)128public int f2;129130@ExpectedString(131"@Classy(Obj[][][].class)")132@Classy(Obj[][][].class)133public int f3;134135@ExpectedString(136"@Classy(int.class)")137@Classy(int.class)138public int f4;139140@ExpectedString(141"@Classy(int[][][].class)")142@Classy(int[][][].class)143public int f5;144}145146/**147* Each field should have two annotations, the first being148* @ExpectedString and the second the annotation under test.149*/150private static int arrayAnnotationTest() {151int failures = 0;152for (Field f : ArrayAnnotationHost.class.getFields()) {153Annotation[] annotations = f.getAnnotations();154System.out.println(annotations[1]);155failures += check(((ExpectedString)annotations[0]).value(),156annotations[1].toString());157}158return failures;159}160161static class ArrayAnnotationHost {162@ExpectedString(163"@BooleanArray({true, false, true})")164@BooleanArray({true, false, true})165public boolean[] f0;166167@ExpectedString(168"@FloatArray({3.0f, 4.0f, 0.0f/0.0f, -1.0f/0.0f, 1.0f/0.0f})")169@FloatArray({3.0f, 4.0f, Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY})170public float[] f1;171172@ExpectedString(173"@DoubleArray({1.0, 2.0, 0.0/0.0, 1.0/0.0, -1.0/0.0})")174@DoubleArray({1.0, 2.0, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,})175public double[] f2;176177@ExpectedString(178"@ByteArray({(byte)0x0a, (byte)0x0b, (byte)0x0c})")179@ByteArray({10, 11, 12})180public byte[] f3;181182@ExpectedString(183"@ShortArray({0, 4, 5})")184@ShortArray({0, 4, 5})185public short[] f4;186187@ExpectedString(188"@CharArray({'a', 'b', 'c', '\\''})")189@CharArray({'a', 'b', 'c', '\''})190public char[] f5;191192@ExpectedString(193"@IntArray({1})")194@IntArray({1})195public int[] f6;196197@ExpectedString(198"@LongArray({-9223372036854775808L, -2147483649L, -2147483648L," +199" -2147483647L, 2147483648L, 9223372036854775807L})")200@LongArray({Long.MIN_VALUE, Integer.MIN_VALUE-1L, Integer.MIN_VALUE,201-Integer.MAX_VALUE, Integer.MAX_VALUE+1L, Long.MAX_VALUE})202public long[] f7;203204@ExpectedString(205"@StringArray({\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})")206@StringArray({"A", "B", "C", "\"Quote\""})207public String[] f8;208209@ExpectedString(210"@ClassArray({int.class, Obj[].class})")211@ClassArray({int.class, Obj[].class})212public Class<?>[] f9;213214@ExpectedString(215"@EnumArray({SOURCE})")216@EnumArray({RetentionPolicy.SOURCE})217public RetentionPolicy[] f10;218}219}220221// ------------ Supporting types ------------222223class Obj {}224225@Retention(RetentionPolicy.RUNTIME)226@interface ExpectedString {227String value();228}229230@Retention(RetentionPolicy.RUNTIME)231@interface Classy {232Class<?> value();233}234235@Retention(RetentionPolicy.RUNTIME)236@interface BooleanArray {237boolean[] value();238}239240@Retention(RetentionPolicy.RUNTIME)241@interface FloatArray {242float[] value();243}244245@Retention(RetentionPolicy.RUNTIME)246@interface DoubleArray {247double[] value();248}249250@Retention(RetentionPolicy.RUNTIME)251@interface ByteArray {252byte[] value();253}254255@Retention(RetentionPolicy.RUNTIME)256@interface ShortArray {257short[] value();258}259260@Retention(RetentionPolicy.RUNTIME)261@interface CharArray {262char[] value();263}264265@Retention(RetentionPolicy.RUNTIME)266@interface IntArray {267int[] value();268}269270@Retention(RetentionPolicy.RUNTIME)271@interface LongArray {272long[] value();273}274275@Retention(RetentionPolicy.RUNTIME)276@interface ClassArray {277Class<?>[] value() default {int.class, Obj[].class};278}279280@Retention(RetentionPolicy.RUNTIME)281@interface StringArray {282String[] value();283}284285@Retention(RetentionPolicy.RUNTIME)286@interface EnumArray {287RetentionPolicy[] value();288}289290@Retention(RetentionPolicy.RUNTIME)291@interface MostlyPrimitive {292char c0();293char c1();294byte b0();295int i0();296int i1();297float f0();298float f1();299double d0();300double d1();301long l0();302long l1();303long l2();304long l3();305String s0();306String s1();307Class<?> class0();308Class<?>[] classArray();309}310311312