Fixing a few bugs in the statistics printout.
[jpf-core.git] / src / classes / sun / reflect / annotation / AnnotationType.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 sun.reflect.annotation;
20
21 import java.lang.annotation.Inherited;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import sun.misc.SharedSecrets;
29
30 /**
31  * this is a placeholder for a Java 6 class, which we only have here to
32  * support both Java 1.5 and 6 with the same set of env/ classes
33  *
34  * this is Java only, so it's a drag we have to add this, but since it is outside
35  * java.* and doesn't refer to Java 6 stuff outside the sun.misc.SharedSecrets
36  * we bite the bullet and add it (for now)
37  *
38  * <2do> THIS IS GOING AWAY AS SOON AS WE OFFICIALLY SWITCH TO JAVA 6
39  */
40 public class AnnotationType {
41
42   private RetentionPolicy retention = RetentionPolicy.RUNTIME;
43   private boolean inherited = false;
44
45   // caches
46   private final Map<String, Class<?>> memberTypes = new HashMap<String, Class<?>>();
47   private final Map<String, Object> memberDefaults = new HashMap<String, Object>();
48   private final Map<String, Method> members = new HashMap<String, Method>();
49
50
51   public static synchronized AnnotationType getInstance (Class<?> annotationClass) {
52     AnnotationType at = SharedSecrets.getJavaLangAccess().getAnnotationType(annotationClass);
53     if (at == null) {
54       at = new AnnotationType(annotationClass);
55     }
56     return at;
57   }
58
59   private AnnotationType(final Class<?> annoCls) {
60     if (!annoCls.isAnnotation()) {
61       throw new IllegalArgumentException("Not an annotation type");
62     }
63
64     Method[] methods = annoCls.getDeclaredMethods();
65
66     for (Method m : methods) {
67       if (m.getParameterTypes().length == 0) {
68         // cache name -> method assoc
69         String mname = m.getName();
70         members.put(mname, m);
71
72         // cache member type
73         Class<?> type = m.getReturnType();
74         memberTypes.put(mname, invocationHandlerReturnType(type));
75
76         // cache member default val (if any)
77         Object val = m.getDefaultValue();
78         if (val != null) {
79           memberDefaults.put(mname, val);
80         }
81       } else {
82         // probably an exception
83       }
84     }
85
86     if ((annoCls != Retention.class) && (annoCls != Inherited.class)) { // don't get recursive
87       inherited = annoCls.isAnnotationPresent(Inherited.class);
88
89       Retention r = annoCls.getAnnotation(Retention.class);
90       if (r == null) {
91         retention = RetentionPolicy.CLASS;
92       } else {
93         retention = r.value();
94       }
95     }
96
97     SharedSecrets.getJavaLangAccess().setAnnotationType(annoCls, this);
98   }
99
100   public static Class<?> invocationHandlerReturnType (Class<?> type) {
101     // return box types for builtins
102     if (type == boolean.class) {
103       return Boolean.class;
104     } else if (type == byte.class) {
105       return Byte.class;
106     } else if (type == char.class) {
107       return Character.class;
108     } else if (type == short.class) {
109       return Short.class;
110     } else if (type == int.class) {
111       return Integer.class;
112     } else if (type == long.class) {
113       return Long.class;
114     } else if (type == float.class) {
115       return Float.class;
116     } else if (type == double.class) {
117       return Double.class;
118     } else {
119       return type;
120     }
121   }
122
123   public Map<String, Class<?>> memberTypes() {
124     return memberTypes;
125   }
126
127   public Map<String, Method> members() {
128     return members;
129   }
130
131   public Map<String, Object> memberDefaults() {
132     return memberDefaults;
133   }
134
135   public RetentionPolicy retention() {
136     return retention;
137   }
138
139   public boolean isInherited() {
140     return inherited;
141   }
142
143 }