Mercurial > hg > Members > kono > jpf-core
comparison src/tests/gov/nasa/jpf/test/vm/basic/AnnotationTest.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
1 /* | |
2 * Copyright (C) 2014, United States Government, as represented by the | |
3 * Administrator of the National Aeronautics and Space Administration. | |
4 * All rights reserved. | |
5 * | |
6 * The Java Pathfinder core (jpf-core) platform is licensed under the | |
7 * Apache License, Version 2.0 (the "License"); you may not use this file except | |
8 * in compliance with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0. | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, software | |
13 * distributed under the License is distributed on an "AS IS" BASIS, | |
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 * See the License for the specific language governing permissions and | |
16 * limitations under the License. | |
17 */ | |
18 package gov.nasa.jpf.test.vm.basic; | |
19 | |
20 import gov.nasa.jpf.ListenerAdapter; | |
21 import gov.nasa.jpf.jvm.bytecode.GETFIELD; | |
22 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction; | |
23 import gov.nasa.jpf.util.test.TestJPF; | |
24 import gov.nasa.jpf.vm.AnnotationInfo; | |
25 import gov.nasa.jpf.vm.FieldInfo; | |
26 import gov.nasa.jpf.vm.Instruction; | |
27 import gov.nasa.jpf.vm.ThreadInfo; | |
28 import gov.nasa.jpf.vm.VM; | |
29 import gov.nasa.jpf.vm.MethodInfo; | |
30 | |
31 import java.lang.annotation.Annotation; | |
32 import java.lang.annotation.Inherited; | |
33 import java.lang.annotation.Retention; | |
34 import java.lang.annotation.RetentionPolicy; | |
35 import java.lang.reflect.Method; | |
36 | |
37 import org.junit.Test; | |
38 | |
39 | |
40 public class AnnotationTest extends TestJPF { | |
41 | |
42 @Test //---------------------------------------------------------------------- | |
43 @A1("foo") | |
44 public void testStringValueOk () { | |
45 if (verifyNoPropertyViolation()) { | |
46 try { | |
47 java.lang.reflect.Method method = | |
48 AnnotationTest.class.getMethod("testStringValueOk"); | |
49 A1 annotation = method.getAnnotation(A1.class); | |
50 | |
51 assert ("foo".equals(annotation.value())); | |
52 | |
53 } catch (SecurityException e) { | |
54 e.printStackTrace(); | |
55 } catch (NoSuchMethodException e) { | |
56 e.printStackTrace(); | |
57 } | |
58 } | |
59 } | |
60 | |
61 @Retention(RetentionPolicy.RUNTIME) | |
62 @interface A1 { | |
63 String value(); | |
64 } | |
65 | |
66 | |
67 @Test //---------------------------------------------------------------------- | |
68 @A2({"foo", "boo"}) | |
69 public void testStringArrayValueOk () { | |
70 if (verifyNoPropertyViolation()) { | |
71 try { | |
72 java.lang.reflect.Method method = | |
73 AnnotationTest.class.getMethod("testStringArrayValueOk"); | |
74 A2 annotation = method.getAnnotation(A2.class); | |
75 | |
76 Object v = annotation.value(); | |
77 assert v instanceof String[]; | |
78 | |
79 String[] a = (String[])v; | |
80 assert a.length == 2; | |
81 | |
82 assert "foo".equals(a[0]); | |
83 assert "boo".equals(a[1]); | |
84 | |
85 } catch (SecurityException e) { | |
86 e.printStackTrace(); | |
87 } catch (NoSuchMethodException e) { | |
88 e.printStackTrace(); | |
89 } | |
90 } | |
91 } | |
92 | |
93 @Retention(RetentionPolicy.RUNTIME) | |
94 @interface A2 { | |
95 String[] value(); | |
96 } | |
97 | |
98 @Test //---------------------------------------------------------------------- | |
99 @A3(Long.MAX_VALUE) | |
100 public void testLongValueOk () { | |
101 if (verifyNoPropertyViolation()) { | |
102 try { | |
103 java.lang.reflect.Method method = | |
104 AnnotationTest.class.getMethod("testLongValueOk"); | |
105 A3 annotation = method.getAnnotation(A3.class); | |
106 | |
107 assert (annotation.value() == Long.MAX_VALUE); | |
108 } catch (SecurityException e) { | |
109 e.printStackTrace(); | |
110 } catch (NoSuchMethodException e) { | |
111 e.printStackTrace(); | |
112 } | |
113 } | |
114 } | |
115 | |
116 @Retention(RetentionPolicy.RUNTIME) | |
117 @interface A3 { | |
118 long value(); | |
119 } | |
120 | |
121 | |
122 @Test //---------------------------------------------------------------------- | |
123 @A4(a="one",b=42.0) | |
124 public void testNamedParamsOk () { | |
125 if (verifyNoPropertyViolation()) { | |
126 try { | |
127 java.lang.reflect.Method method = | |
128 AnnotationTest.class.getMethod("testNamedParamsOk"); | |
129 A4 annotation = method.getAnnotation(A4.class); | |
130 | |
131 assert ("one".equals(annotation.a())); | |
132 assert ( 42.0 == annotation.b()); | |
133 | |
134 System.out.println(annotation); | |
135 | |
136 } catch (SecurityException e) { | |
137 e.printStackTrace(); | |
138 } catch (NoSuchMethodException e) { | |
139 e.printStackTrace(); | |
140 } | |
141 } | |
142 } | |
143 | |
144 @Retention(RetentionPolicy.RUNTIME) | |
145 @interface A4 { | |
146 String a(); | |
147 double b(); | |
148 } | |
149 | |
150 | |
151 @Test //---------------------------------------------------------------------- | |
152 @A5(b="foo") | |
153 public void testPartialDefaultParamsOk () { | |
154 if (verifyNoPropertyViolation()) { | |
155 try { | |
156 java.lang.reflect.Method method = | |
157 AnnotationTest.class.getMethod("testPartialDefaultParamsOk"); | |
158 A5 annotation = method.getAnnotation(A5.class); | |
159 | |
160 assert ("whatever".equals(annotation.a())); | |
161 | |
162 System.out.println(annotation); | |
163 | |
164 } catch (SecurityException e) { | |
165 e.printStackTrace(); | |
166 } catch (NoSuchMethodException e) { | |
167 e.printStackTrace(); | |
168 } | |
169 } | |
170 } | |
171 | |
172 @Retention(RetentionPolicy.RUNTIME) | |
173 @interface A5 { | |
174 String a() default "whatever"; | |
175 String b(); | |
176 } | |
177 | |
178 @Test //---------------------------------------------------------------------- | |
179 @A6 | |
180 public void testSingleDefaultParamOk () { | |
181 if (verifyNoPropertyViolation()) { | |
182 try { | |
183 java.lang.reflect.Method method = | |
184 AnnotationTest.class.getMethod("testSingleDefaultParamOk"); | |
185 A6 annotation = method.getAnnotation(A6.class); | |
186 | |
187 assert ("whatever".equals(annotation.value())); | |
188 | |
189 System.out.println(annotation); | |
190 | |
191 } catch (SecurityException e) { | |
192 e.printStackTrace(); | |
193 } catch (NoSuchMethodException e) { | |
194 e.printStackTrace(); | |
195 } | |
196 } | |
197 } | |
198 | |
199 @Retention(RetentionPolicy.RUNTIME) | |
200 @interface A6 { | |
201 String value() default "whatever"; | |
202 } | |
203 | |
204 @A6 | |
205 @Test | |
206 public void testAnnotationClass() throws ClassNotFoundException, NoSuchMethodException { | |
207 if (verifyNoPropertyViolation()) { | |
208 Class clazz = Class.forName("gov.nasa.jpf.test.vm.basic.AnnotationTest"); | |
209 Method method = clazz.getDeclaredMethod("testAnnotationClass"); | |
210 Annotation annotations[] = method.getAnnotations(); | |
211 | |
212 for (int i=0; i<annotations.length; i++){ | |
213 System.out.printf(" a[%d] = %s\n", i, annotations[i].toString()); | |
214 } | |
215 | |
216 assertEquals(2, annotations.length); | |
217 assertNotNull(annotations[0]); | |
218 assertNotNull(annotations[1]); | |
219 | |
220 assertTrue(annotations[0] instanceof A6); | |
221 assertTrue(annotations[1] instanceof Test); | |
222 } | |
223 } | |
224 | |
225 //-------------------------------------------------------------------- | |
226 | |
227 public enum MyEnum { | |
228 ONE, TWO | |
229 } | |
230 | |
231 @Retention(RetentionPolicy.RUNTIME) | |
232 @interface A7 { | |
233 MyEnum value(); | |
234 } | |
235 | |
236 @Test | |
237 @A7(MyEnum.ONE) | |
238 public void testEnumValue() throws ClassNotFoundException, NoSuchMethodException { | |
239 if (verifyNoPropertyViolation()){ | |
240 Class clazz = Class.forName("gov.nasa.jpf.test.vm.basic.AnnotationTest"); // Any class outside of this file will do. | |
241 Method method = clazz.getDeclaredMethod("testEnumValue"); // Any method with an annotation will do. | |
242 Annotation annotations[] = method.getAnnotations(); | |
243 | |
244 assertEquals(2, annotations.length); | |
245 assertNotNull(annotations[1]); | |
246 | |
247 assertTrue(annotations[1] instanceof A7); | |
248 A7 ann = (A7)annotations[1]; | |
249 assertTrue( ann.value() == MyEnum.ONE); | |
250 } | |
251 } | |
252 | |
253 //-------------------------------------------------------------------- | |
254 | |
255 @Retention(RetentionPolicy.RUNTIME) | |
256 @interface A8 { | |
257 Class value(); | |
258 } | |
259 | |
260 @Test | |
261 @A8(AnnotationTest.class) | |
262 public void testClassValue() throws ClassNotFoundException, NoSuchMethodException { | |
263 if (verifyNoPropertyViolation()){ | |
264 Class clazz = Class.forName("gov.nasa.jpf.test.vm.basic.AnnotationTest"); // Any class outside of this file will do. | |
265 Method method = clazz.getDeclaredMethod("testClassValue"); // Any method with an annotation will do. | |
266 Annotation annotations[] = method.getAnnotations(); | |
267 | |
268 assertEquals(2, annotations.length); | |
269 assertNotNull(annotations[1]); | |
270 | |
271 assertTrue(annotations[1] instanceof A8); | |
272 A8 ann = (A8)annotations[1]; | |
273 assertTrue( ann.value() == AnnotationTest.class); | |
274 } | |
275 } | |
276 | |
277 @Retention(RetentionPolicy.RUNTIME) | |
278 @interface A11 { | |
279 Class<?>[] value(); | |
280 } | |
281 | |
282 @Test | |
283 @A11({ AnnotationTest.class, Class.class }) | |
284 public void testClassArrayValueOk() throws ClassNotFoundException, SecurityException, NoSuchMethodException { | |
285 if (verifyNoPropertyViolation()) { | |
286 Class<?> clazz = Class.forName(AnnotationTest.class.getName()); | |
287 Method method = clazz.getDeclaredMethod("testClassArrayValueOk"); | |
288 Annotation[] annotations = method.getAnnotations(); | |
289 assertEquals(2, annotations.length); | |
290 assertNotNull(annotations[1]); | |
291 | |
292 assertTrue(annotations[1] instanceof A11); | |
293 A11 ann = (A11) annotations[1]; | |
294 assertTrue(ann.value()[0] == AnnotationTest.class); | |
295 assertTrue(ann.value()[1] == Class.class); | |
296 } | |
297 } | |
298 | |
299 //------------------------------------------------------------------- | |
300 static class MyClass { | |
301 @A1("the answer") | |
302 int data = 42; | |
303 } | |
304 | |
305 public static class DataListener extends ListenerAdapter { | |
306 | |
307 @Override | |
308 public void executeInstruction(VM vm, ThreadInfo ti, Instruction insnToExecute){ | |
309 if (insnToExecute instanceof GETFIELD){ | |
310 FieldInfo fi = ((GETFIELD)insnToExecute).getFieldInfo(); | |
311 if (fi.getName().equals("data")){ | |
312 AnnotationInfo ai = fi.getAnnotation("gov.nasa.jpf.test.vm.basic.AnnotationTest$A1"); | |
313 System.out.println("annotation for " + fi.getFullName() + " = " + ai); | |
314 | |
315 if (ai != null){ | |
316 String val = ai.getValueAsString("value"); | |
317 System.out.println(" value = " + val); | |
318 | |
319 if (val == null || !val.equals("the answer")){ | |
320 fail("wrong @A1 value = " + val); | |
321 } | |
322 } else { | |
323 fail("no @A1 annotation for field " + fi.getFullName()); | |
324 } | |
325 } | |
326 } | |
327 } | |
328 } | |
329 | |
330 @Test | |
331 public void testFieldAnnotation(){ | |
332 if (verifyNoPropertyViolation("+listener=.test.vm.basic.AnnotationTest$DataListener")){ | |
333 MyClass obj = new MyClass(); | |
334 int d = obj.data; | |
335 } | |
336 } | |
337 | |
338 //------------------------------------------------------------------- | |
339 public static class ArgListener extends ListenerAdapter { | |
340 | |
341 @Override | |
342 public void executeInstruction (VM vm, ThreadInfo ti, Instruction insnToExecute){ | |
343 if (insnToExecute instanceof JVMInvokeInstruction){ | |
344 MethodInfo mi = ((JVMInvokeInstruction)insnToExecute).getInvokedMethod(); | |
345 if (mi.getName().equals("foo")){ | |
346 System.out.println("-- called method: " + mi.getUniqueName()); | |
347 | |
348 AnnotationInfo[][] pai = mi.getParameterAnnotations(); | |
349 | |
350 assert pai != null : "no parameter annotations found"; | |
351 assert pai.length == 2 : "wrong number of parameter annotation arrays: " + pai.length; | |
352 assert pai[0] != null : "no parameter annotation for first argument found"; | |
353 assert pai[0].length == 1 : "wrong number of annotations for first argument: "+ pai[0].length; | |
354 assert pai[1] != null : "no parameter annotation for second argument found"; | |
355 assert pai[1].length == 0 : "wrong number of annotations for first argument: "+ pai[1].length; | |
356 | |
357 for (int i=0; i<pai.length; i++){ | |
358 System.out.println("-- annotations for parameter: " + i); | |
359 AnnotationInfo[] ai = pai[i]; | |
360 if (ai != null && ai.length > 0) { | |
361 for (int j = 0; j < ai.length; j++) { | |
362 assert (ai[i] != null) : "null annotation for paramter: " + j; | |
363 System.out.println(ai[i].asString()); | |
364 } | |
365 } else { | |
366 System.out.println("none"); | |
367 } | |
368 } | |
369 } | |
370 } | |
371 } | |
372 } | |
373 | |
374 public void foo (@A1("arghh") MyClass x, String s){ | |
375 // nothing | |
376 } | |
377 | |
378 @Test | |
379 public void testParameterAnnotation(){ | |
380 if (verifyNoPropertyViolation("+listener=.test.vm.basic.AnnotationTest$ArgListener")){ | |
381 MyClass obj = new MyClass(); | |
382 foo( obj, "blah"); | |
383 } | |
384 } | |
385 | |
386 //--------------------------------------------------------------- | |
387 | |
388 @Retention(RetentionPolicy.RUNTIME) | |
389 @Inherited | |
390 public @interface A9 { | |
391 } | |
392 | |
393 @Retention(RetentionPolicy.RUNTIME) | |
394 public @interface A10 { | |
395 } | |
396 | |
397 @A9() | |
398 public static class Parent { | |
399 } | |
400 | |
401 @A10 | |
402 public static class Child1 extends Parent { | |
403 } | |
404 | |
405 public static class Child2 extends Child1 { | |
406 } | |
407 | |
408 @Test | |
409 public void getAnnotationsTest () { | |
410 if (verifyNoPropertyViolation()) { | |
411 assertTrue(Parent.class.getAnnotations().length == 1); | |
412 assertTrue(Child1.class.getAnnotations().length == 2); | |
413 assertTrue(Child2.class.getAnnotations().length == 1); | |
414 } | |
415 } | |
416 | |
417 | |
418 //--------------------------------------------------------------- | |
419 // test for RuntimeVisibleAnnotations attributes that in turn have | |
420 // element_value entries | |
421 @Retention(RetentionPolicy.RUNTIME) | |
422 @interface A12 { // this one has the string value | |
423 String value(); | |
424 } | |
425 | |
426 @Retention(RetentionPolicy.RUNTIME) | |
427 @A12("Whatever") | |
428 @interface A13 { | |
429 // this one has a RuntimeVisibleAnnotation attribute for A11 with a | |
430 // String entry value | |
431 } | |
432 | |
433 @A13 // causes loading of @C | |
434 @Test | |
435 public void testRecursiveRuntimeVisibleAnnotationValue(){ | |
436 if (verifyNoPropertyViolation()){ | |
437 // nothing to do other than just causing the loading of A12 | |
438 } | |
439 } | |
440 | |
441 | |
442 //--------------------------------------------------------------- | |
443 // test of char annotations | |
444 @Retention(RetentionPolicy.RUNTIME) | |
445 public @interface A14 { | |
446 char value(); | |
447 } | |
448 | |
449 @Test | |
450 @A14('x') | |
451 public void testCharAnnotation(){ | |
452 if (verifyNoPropertyViolation()){ | |
453 try { | |
454 Class<?> clazz = Class.forName(AnnotationTest.class.getName()); | |
455 Method method = clazz.getDeclaredMethod("testCharAnnotation"); | |
456 Annotation[] annotations = method.getAnnotations(); | |
457 assertEquals(2, annotations.length); | |
458 assertNotNull(annotations[1]); | |
459 | |
460 assertTrue(annotations[1] instanceof A14); | |
461 A14 ann = (A14) annotations[1]; | |
462 assertTrue(ann.value() == 'x'); | |
463 | |
464 } catch (Throwable t){ | |
465 t.printStackTrace(); | |
466 fail("unexpected exception: " + t); | |
467 } | |
468 } | |
469 } | |
470 | |
471 //--------------------------------------------------------------- | |
472 // test of char annotations | |
473 @Retention(RetentionPolicy.RUNTIME) | |
474 public @interface A15 { | |
475 float value(); | |
476 } | |
477 | |
478 @Test | |
479 @A15(12.34f) | |
480 public void testFloatAnnotation(){ | |
481 if (verifyNoPropertyViolation()){ | |
482 try { | |
483 Class<?> clazz = Class.forName(AnnotationTest.class.getName()); | |
484 Method method = clazz.getDeclaredMethod("testFloatAnnotation"); | |
485 Annotation[] annotations = method.getAnnotations(); | |
486 assertEquals(2, annotations.length); | |
487 assertNotNull(annotations[1]); | |
488 | |
489 assertTrue(annotations[1] instanceof A15); | |
490 A15 ann = (A15) annotations[1]; | |
491 assertTrue(Math.abs(ann.value() - 12.34f) < 0.00001); | |
492 | |
493 } catch (Throwable t){ | |
494 t.printStackTrace(); | |
495 fail("unexpected exception: " + t); | |
496 } | |
497 } | |
498 } | |
499 | |
500 // test of char annotations | |
501 @Retention(RetentionPolicy.RUNTIME) | |
502 public @interface A16 { | |
503 double value(); | |
504 } | |
505 | |
506 @Test | |
507 @A16(Double.MAX_VALUE) | |
508 public void testDoubleAnnotation(){ | |
509 if (verifyNoPropertyViolation()){ | |
510 try { | |
511 Class<?> clazz = Class.forName(AnnotationTest.class.getName()); | |
512 Method method = clazz.getDeclaredMethod("testDoubleAnnotation"); | |
513 Annotation[] annotations = method.getAnnotations(); | |
514 assertEquals(2, annotations.length); | |
515 assertNotNull(annotations[1]); | |
516 | |
517 assertTrue(annotations[1] instanceof A16); | |
518 A16 ann = (A16) annotations[1]; | |
519 assertTrue(ann.value() == Double.MAX_VALUE); | |
520 | |
521 } catch (Throwable t){ | |
522 t.printStackTrace(); | |
523 fail("unexpected exception: " + t); | |
524 } | |
525 } | |
526 } | |
527 | |
528 // test of char annotations | |
529 @Retention(RetentionPolicy.RUNTIME) | |
530 public @interface A17 { | |
531 long value(); | |
532 } | |
533 | |
534 @Test | |
535 @A17(Long.MAX_VALUE) | |
536 public void testLongAnnotation(){ | |
537 if (verifyNoPropertyViolation()){ | |
538 try { | |
539 Class<?> clazz = Class.forName(AnnotationTest.class.getName()); | |
540 Method method = clazz.getDeclaredMethod("testLongAnnotation"); | |
541 Annotation[] annotations = method.getAnnotations(); | |
542 assertEquals(2, annotations.length); | |
543 assertNotNull(annotations[1]); | |
544 | |
545 assertTrue(annotations[1] instanceof A17); | |
546 A17 ann = (A17) annotations[1]; | |
547 assertTrue(ann.value() == Long.MAX_VALUE); | |
548 | |
549 } catch (Throwable t){ | |
550 t.printStackTrace(); | |
551 fail("unexpected exception: " + t); | |
552 } | |
553 } | |
554 } | |
555 | |
556 } |