2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.jvm.ClassFile;
23 import gov.nasa.jpf.util.test.TestJPF;
24 import gov.nasa.jpf.vm.AbstractTypeAnnotationInfo;
25 import gov.nasa.jpf.vm.ClassInfo;
26 import gov.nasa.jpf.vm.FieldInfo;
27 import gov.nasa.jpf.vm.LocalVarInfo;
28 import gov.nasa.jpf.vm.MethodInfo;
29 import gov.nasa.jpf.vm.VM;
30 import java.lang.annotation.ElementType;
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.annotation.Target;
34 import org.junit.Test;
38 * regression test for Java 8 type annotations (JSR 308)
40 public class TypeAnnotationTest extends TestJPF {
42 //--- test type annotations
43 @Retention(RetentionPolicy.RUNTIME)
44 @Target(ElementType.TYPE_USE)
47 //--- test class hierarchy
52 public class Anno8 extends @MyTA Base implements @MyTA Ifc {
56 @MyTA int baz (@MyTA int a, int b){
62 //--- listener to check annotations are set
63 public static class Listener extends ListenerAdapter {
65 protected int numberOfTargetTypes (AbstractTypeAnnotationInfo[] annos, int targetType){
67 for (AbstractTypeAnnotationInfo tai : annos){
68 if (tai.getTargetType() == targetType){
76 public void classLoaded(VM vm, ClassInfo loadedClass) {
77 if (loadedClass.getName().equals("java8.TypeAnnotationTest$Anno8")){
78 System.out.println("checking loaded class " + loadedClass.getName() + " for type annotations..");
80 // <2do> - needs more tests..
82 System.out.println("--- super types");
83 AbstractTypeAnnotationInfo[] tais = loadedClass.getTypeAnnotations();
84 for (AbstractTypeAnnotationInfo tai : tais){
85 System.out.println(" " + tai);
87 assertTrue(tais.length == 2);
88 assertTrue( numberOfTargetTypes(tais, ClassFile.CLASS_EXTENDS) == 2); // base and interface
90 System.out.println("--- fields");
91 FieldInfo fi = loadedClass.getDeclaredInstanceField("data");
92 tais = fi.getTypeAnnotations();
93 for (AbstractTypeAnnotationInfo tai : tais){
94 System.out.println(" " + tai);
96 assertTrue(tais.length == 1);
97 assertTrue( numberOfTargetTypes(tais, ClassFile.FIELD) == 1);
99 System.out.println("--- methods");
100 MethodInfo mi = loadedClass.getMethod("baz(II)I", false);
101 tais = mi.getTypeAnnotations();
102 for (AbstractTypeAnnotationInfo tai : tais){
103 System.out.println(" " + tai);
105 assertTrue(tais.length == 3);
106 assertTrue( numberOfTargetTypes(tais, ClassFile.METHOD_RETURN) == 1);
107 assertTrue( numberOfTargetTypes(tais, ClassFile.METHOD_FORMAL_PARAMETER) == 1);
108 assertTrue( numberOfTargetTypes(tais, ClassFile.LOCAL_VARIABLE) == 1);
110 LocalVarInfo lv = mi.getLocalVar("x", 4);
111 System.out.println("--- local var " + lv);
112 tais = lv.getTypeAnnotations();
113 for (AbstractTypeAnnotationInfo tai : tais){
114 System.out.println(" " + tai);
116 assertTrue(tais.length == 1);
117 assertTrue( numberOfTargetTypes(tais, ClassFile.LOCAL_VARIABLE) == 1);
124 public void testBasicTypeAnnotations (){
125 if (verifyNoPropertyViolation("+listener=java8.TypeAnnotationTest$Listener")){
126 Anno8 anno8 = new Anno8();