8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.annotation;
27
28 import sun.misc.JavaLangAccess;
29
30 import java.lang.annotation.*;
31 import java.lang.reflect.*;
32 import java.util.*;
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35
36 /**
37 * Represents an annotation type at run time. Used to type-check annotations
38 * and apply member defaults.
39 *
40 * @author Josh Bloch
41 * @since 1.5
42 */
43 public class AnnotationType {
44 /**
45 * Member name -> type mapping. Note that primitive types
46 * are represented by the class objects for the corresponding wrapper
47 * types. This matches the return value that must be used for a
48 * dynamic proxy, allowing for a simple isInstance test.
49 */
50 private final Map<String, Class<?>> memberTypes;
51
52 /**
53 * Member name -> default value mapping.
54 */
62
63 /**
64 * The retention policy for this annotation type.
65 */
66 private final RetentionPolicy retention;
67
68 /**
69 * Whether this annotation type is inherited.
70 */
71 private final boolean inherited;
72
73 /**
74 * Returns an AnnotationType instance for the specified annotation type.
75 *
76 * @throws IllegalArgumentException if the specified class object
77 * does not represent a valid annotation type
78 */
79 public static AnnotationType getInstance(
80 Class<? extends Annotation> annotationClass)
81 {
82 JavaLangAccess jla = sun.misc.SharedSecrets.getJavaLangAccess();
83 AnnotationType result = jla.getAnnotationType(annotationClass); // volatile read
84 if (result == null) {
85 result = new AnnotationType(annotationClass);
86 // try to CAS the AnnotationType: null -> result
87 if (!jla.casAnnotationType(annotationClass, null, result)) {
88 // somebody was quicker -> read it's result
89 result = jla.getAnnotationType(annotationClass);
90 assert result != null;
91 }
92 }
93
94 return result;
95 }
96
97 /**
98 * Sole constructor.
99 *
100 * @param annotationClass the class object for the annotation type
101 * @throws IllegalArgumentException if the specified class object for
102 * does not represent a valid annotation type
117 memberDefaults = new HashMap<>(0);
118 members = new HashMap<>(methods.length+1, 1.0f);
119
120 for (Method method : methods) {
121 if (method.getParameterTypes().length != 0)
122 throw new IllegalArgumentException(method + " has params");
123 String name = method.getName();
124 Class<?> type = method.getReturnType();
125 memberTypes.put(name, invocationHandlerReturnType(type));
126 members.put(name, method);
127
128 Object defaultValue = method.getDefaultValue();
129 if (defaultValue != null)
130 memberDefaults.put(name, defaultValue);
131 }
132
133 // Initialize retention, & inherited fields. Special treatment
134 // of the corresponding annotation types breaks infinite recursion.
135 if (annotationClass != Retention.class &&
136 annotationClass != Inherited.class) {
137 JavaLangAccess jla = sun.misc.SharedSecrets.getJavaLangAccess();
138 Map<Class<? extends Annotation>, Annotation> metaAnnotations =
139 AnnotationParser.parseSelectAnnotations(
140 jla.getRawClassAnnotations(annotationClass),
141 jla.getConstantPool(annotationClass),
142 annotationClass,
143 Retention.class, Inherited.class
144 );
145 Retention ret = (Retention) metaAnnotations.get(Retention.class);
146 retention = (ret == null ? RetentionPolicy.CLASS : ret.value());
147 inherited = metaAnnotations.containsKey(Inherited.class);
148 }
149 else {
150 retention = RetentionPolicy.RUNTIME;
151 inherited = false;
152 }
153 }
154
155 /**
156 * Returns the type that must be returned by the invocation handler
157 * of a dynamic proxy in order to have the dynamic proxy return
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.annotation;
27
28 import java.lang.annotation.*;
29 import java.lang.reflect.*;
30 import java.util.*;
31 import java.security.AccessController;
32 import java.security.PrivilegedAction;
33 import jdk.internal.misc.SharedSecrets;
34 import jdk.internal.misc.JavaLangAccess;
35
36 /**
37 * Represents an annotation type at run time. Used to type-check annotations
38 * and apply member defaults.
39 *
40 * @author Josh Bloch
41 * @since 1.5
42 */
43 public class AnnotationType {
44 /**
45 * Member name -> type mapping. Note that primitive types
46 * are represented by the class objects for the corresponding wrapper
47 * types. This matches the return value that must be used for a
48 * dynamic proxy, allowing for a simple isInstance test.
49 */
50 private final Map<String, Class<?>> memberTypes;
51
52 /**
53 * Member name -> default value mapping.
54 */
62
63 /**
64 * The retention policy for this annotation type.
65 */
66 private final RetentionPolicy retention;
67
68 /**
69 * Whether this annotation type is inherited.
70 */
71 private final boolean inherited;
72
73 /**
74 * Returns an AnnotationType instance for the specified annotation type.
75 *
76 * @throws IllegalArgumentException if the specified class object
77 * does not represent a valid annotation type
78 */
79 public static AnnotationType getInstance(
80 Class<? extends Annotation> annotationClass)
81 {
82 JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
83 AnnotationType result = jla.getAnnotationType(annotationClass); // volatile read
84 if (result == null) {
85 result = new AnnotationType(annotationClass);
86 // try to CAS the AnnotationType: null -> result
87 if (!jla.casAnnotationType(annotationClass, null, result)) {
88 // somebody was quicker -> read it's result
89 result = jla.getAnnotationType(annotationClass);
90 assert result != null;
91 }
92 }
93
94 return result;
95 }
96
97 /**
98 * Sole constructor.
99 *
100 * @param annotationClass the class object for the annotation type
101 * @throws IllegalArgumentException if the specified class object for
102 * does not represent a valid annotation type
117 memberDefaults = new HashMap<>(0);
118 members = new HashMap<>(methods.length+1, 1.0f);
119
120 for (Method method : methods) {
121 if (method.getParameterTypes().length != 0)
122 throw new IllegalArgumentException(method + " has params");
123 String name = method.getName();
124 Class<?> type = method.getReturnType();
125 memberTypes.put(name, invocationHandlerReturnType(type));
126 members.put(name, method);
127
128 Object defaultValue = method.getDefaultValue();
129 if (defaultValue != null)
130 memberDefaults.put(name, defaultValue);
131 }
132
133 // Initialize retention, & inherited fields. Special treatment
134 // of the corresponding annotation types breaks infinite recursion.
135 if (annotationClass != Retention.class &&
136 annotationClass != Inherited.class) {
137 JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
138 Map<Class<? extends Annotation>, Annotation> metaAnnotations =
139 AnnotationParser.parseSelectAnnotations(
140 jla.getRawClassAnnotations(annotationClass),
141 jla.getConstantPool(annotationClass),
142 annotationClass,
143 Retention.class, Inherited.class
144 );
145 Retention ret = (Retention) metaAnnotations.get(Retention.class);
146 retention = (ret == null ? RetentionPolicy.CLASS : ret.value());
147 inherited = metaAnnotations.containsKey(Inherited.class);
148 }
149 else {
150 retention = RetentionPolicy.RUNTIME;
151 inherited = false;
152 }
153 }
154
155 /**
156 * Returns the type that must be returned by the invocation handler
157 * of a dynamic proxy in order to have the dynamic proxy return
|