1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* Copyright (c) 2016, 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 8162817 8168921
* @summary Test of toString on normal annotations
*/
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
/**
* The expected string values are stored in @ExpectedString
* annotations. The essence of the test is comparing the toString()
* result of annotations to the corresponding ExpectedString.value().
*/
public class AnnotationToStringTest {
public static void main(String... args) throws Exception {
int failures = 0;
failures += check(PrimHost.class.getAnnotation(ExpectedString.class).value(),
PrimHost.class.getAnnotation(MostlyPrimitive.class).toString());
failures += classyTest();
failures += arrayAnnotationTest();
if (failures > 0)
throw new RuntimeException(failures + " failures");
}
private static int check(String expected, String actual) {
if (!expected.equals(actual)) {
System.err.printf("ERROR: Expected ''%s'';%ngot ''%s''.\n",
expected, actual);
return 1;
} else
return 0;
}
@ExpectedString(
"@MostlyPrimitive(c0='a', "+
"c1='\\'', " +
"i0=1, " +
"i1=2, " +
"f0=1.0f, " +
"f1=0.0f/0.0f, " +
"d0=0.0, " +
"d1=1.0/0.0, " +
"l0=5, " +
"l1=9223372036854775807L, " +
"l2=-9223372036854775808L, " +
"l3=-2147483648, " +
"s0=\"Hello world.\", " +
"s1=\"a\\\"b\", " +
"class0=Obj[].class)")
@MostlyPrimitive(
c0='a',
c1='\'',
i0=1,
i1=2,
f0=1.0f,
f1=Float.NaN,
d0=0.0,
d1=2.0/0.0,
l0=5,
l1=Long.MAX_VALUE,
l2=Long.MIN_VALUE,
l3=Integer.MIN_VALUE,
s0="Hello world.",
s1="a\"b",
class0=Obj[].class
)
static class PrimHost{}
private static int classyTest() {
int failures = 0;
for (Field f : AnnotationHost.class.getFields()) {
Annotation a = f.getAnnotation(Classy.class);
System.out.println(a);
failures += check(f.getAnnotation(ExpectedString.class).value(),
a.toString());
}
return failures;
}
static class AnnotationHost {
@ExpectedString(
"@Classy(value=Obj.class)")
@Classy(value=Obj.class)
public int f0;
@ExpectedString(
"@Classy(value=Obj[].class)")
@Classy(value=Obj[].class)
public int f1;
@ExpectedString(
"@Classy(value=Obj[][].class)")
@Classy(value=Obj[][].class)
public int f2;
@ExpectedString(
"@Classy(value=Obj[][][].class)")
@Classy(value=Obj[][][].class)
public int f3;
@ExpectedString(
"@Classy(value=int.class)")
@Classy(value=int.class)
public int f4;
@ExpectedString(
"@Classy(value=int[][][].class)")
@Classy(value=int[][][].class)
public int f5;
}
/**
* Each field should have two annotations, the first being
* @ExpectedString and the second the annotation under test.
*/
private static int arrayAnnotationTest() {
int failures = 0;
for (Field f : ArrayAnnotationHost.class.getFields()) {
Annotation[] annotations = f.getAnnotations();
System.out.println(annotations[1]);
failures += check(((ExpectedString)annotations[0]).value(),
annotations[1].toString());
}
return failures;
}
static class ArrayAnnotationHost {
@ExpectedString(
"@BooleanArray(value={true, false, true})")
@BooleanArray(value={true, false, true})
public boolean[] f0;
@ExpectedString(
"@FloatArray(value={3.0f, 4.0f, 0.0f/0.0f, -1.0f/0.0f, 1.0f/0.0f})")
@FloatArray(value={3.0f, 4.0f, Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY})
public float[] f1;
@ExpectedString(
"@DoubleArray(value={1.0, 2.0, 0.0/0.0, 1.0/0.0, -1.0/0.0})")
@DoubleArray(value={1.0, 2.0, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,})
public double[] f2;
@ExpectedString(
"@ByteArray(value={10, 11, 12})")
@ByteArray(value={10, 11, 12})
public byte[] f3;
@ExpectedString(
"@ShortArray(value={0, 4, 5})")
@ShortArray(value={0, 4, 5})
public short[] f4;
@ExpectedString(
"@CharArray(value={'a', 'b', 'c', '\\''})")
@CharArray(value={'a', 'b', 'c', '\''})
public char[] f5;
@ExpectedString(
"@IntArray(value={1})")
@IntArray(value={1})
public int[] f6;
@ExpectedString(
"@LongArray(value={-9223372036854775808L, -2147483649L, -2147483648," +
" -2147483647, 2147483648L, 9223372036854775807L})")
@LongArray(value={Long.MIN_VALUE, Integer.MIN_VALUE-1L, Integer.MIN_VALUE,
-Integer.MAX_VALUE, Integer.MAX_VALUE+1L, Long.MAX_VALUE})
public long[] f7;
@ExpectedString(
"@StringArray(value={\"A\", \"B\", \"C\", \"\\\"Quote\\\"\"})")
@StringArray(value={"A", "B", "C", "\"Quote\""})
public String[] f8;
@ExpectedString(
"@ClassArray(value={int.class, Obj[].class})")
@ClassArray(value={int.class, Obj[].class})
public Class<?>[] f9;
@ExpectedString(
"@EnumArray(value={SOURCE})")
@EnumArray(value={RetentionPolicy.SOURCE})
public RetentionPolicy[] f10;
}
}
// ------------ Supporting types ------------
class Obj {}
@Retention(RetentionPolicy.RUNTIME)
@interface ExpectedString {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface Classy {
Class<?> value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface BooleanArray {
boolean[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface FloatArray {
float[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface DoubleArray {
double[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface ByteArray {
byte[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface ShortArray {
short[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface CharArray {
char[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface IntArray {
int[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface LongArray {
long[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface ClassArray {
Class<?>[] value() default {int.class, Obj[].class};
}
@Retention(RetentionPolicy.RUNTIME)
@interface StringArray {
String[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface EnumArray {
RetentionPolicy[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@interface MostlyPrimitive {
char c0();
char c1();
int i0();
int i1();
float f0();
float f1();
double d0();
double d1();
long l0();
long l1();
long l2();
long l3();
String s0();
String s1();
Class<?> class0();
}
|