Fixes default method resolution (#159)
[jpf-core.git] / src / tests / java8 / TypeAnnotationTest.java
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
19 package java8;
20
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;
35
36
37 /**
38  * regression test for Java 8 type annotations (JSR 308)
39  */
40 public class TypeAnnotationTest extends TestJPF {
41
42   //--- test type annotations
43   @Retention(RetentionPolicy.RUNTIME)
44   @Target(ElementType.TYPE_USE)
45   @interface MyTA {}
46
47   //--- test class hierarchy
48   
49   interface Ifc {}
50   static class Base {}
51   
52   public class Anno8 extends @MyTA Base implements @MyTA Ifc {
53
54     @MyTA int data;
55
56     @MyTA int baz (@MyTA int a, int b){
57       @MyTA int x = a + b;
58       return x;
59     }
60   }
61   
62   //--- listener to check annotations are set
63   public static class Listener extends ListenerAdapter {
64     
65     protected int numberOfTargetTypes (AbstractTypeAnnotationInfo[] annos, int targetType){
66       int n = 0;
67       for (AbstractTypeAnnotationInfo tai : annos){
68         if (tai.getTargetType() == targetType){
69           n++;
70         }
71       }
72       return n;
73     }
74     
75     @Override
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..");
79         
80         // <2do> - needs more tests..
81         
82         System.out.println("--- super types");
83         AbstractTypeAnnotationInfo[] tais = loadedClass.getTypeAnnotations();
84         for (AbstractTypeAnnotationInfo tai : tais){
85           System.out.println("  " + tai);
86         }
87         assertTrue(tais.length == 2);
88         assertTrue( numberOfTargetTypes(tais, ClassFile.CLASS_EXTENDS) == 2); // base and interface
89         
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);
95         }
96         assertTrue(tais.length == 1);
97         assertTrue( numberOfTargetTypes(tais, ClassFile.FIELD) == 1);
98         
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);
104         }
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);
109         
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);
115         }
116         assertTrue(tais.length == 1);
117         assertTrue( numberOfTargetTypes(tais, ClassFile.LOCAL_VARIABLE) == 1);
118         
119       }
120     }
121   }
122   
123   @Test
124   public void testBasicTypeAnnotations (){
125     if (verifyNoPropertyViolation("+listener=java8.TypeAnnotationTest$Listener")){
126       Anno8 anno8 = new Anno8();
127     }
128   }
129   
130 }