1 /* 2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 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 34 /** 35 * Represents an annotation type at run time. Used to type-check annotations 36 * and apply member defaults. 37 * 38 * @author Josh Bloch 39 * @since 1.5 40 */ 41 public class AnnotationType { 42 /** 43 * Member name -> type mapping. Note that primitive types 44 * are represented by the class objects for the corresponding wrapper 45 * types. This matches the return value that must be used for a 46 * dynamic proxy, allowing for a simple isInstance test. 47 */ 48 private final Map<String, Class<?>> memberTypes; 49 50 /** 51 * Member name -> default value mapping. 52 */ 53 private final Map<String, Object> memberDefaults; 54 55 /** 56 * Member name -> Method object mapping. This (and its assoicated 57 * accessor) are used only to generate AnnotationTypeMismatchExceptions. 58 */ 59 private final Map<String, Method> members; 60 61 /** 62 * The retention policy for this annotation type. 63 */ 64 private RetentionPolicy retention = RetentionPolicy.RUNTIME;; 65 66 /** 67 * Whether this annotation type is inherited. 68 */ 69 private boolean inherited = false; 70 71 /** 72 * Returns an AnnotationType instance for the specified annotation type. 73 * 74 * @throw IllegalArgumentException if the specified class object for 75 * does not represent a valid annotation type 76 */ 77 public static synchronized AnnotationType getInstance( 78 Class<? extends Annotation> annotationClass) 79 { 80 AnnotationType result = sun.misc.SharedSecrets.getJavaLangAccess(). 81 getAnnotationType(annotationClass); 82 if (result == null) 83 result = new AnnotationType((Class<? extends Annotation>) annotationClass); 84 85 return result; 86 } 87 88 /** 89 * Sole constructor. 90 * 91 * @param annotationClass the class object for the annotation type 92 * @throw IllegalArgumentException if the specified class object for 93 * does not represent a valid annotation type 94 */ 95 private AnnotationType(final Class<? extends Annotation> annotationClass) { 96 if (!annotationClass.isAnnotation()) 97 throw new IllegalArgumentException("Not an annotation type"); 98 99 Method[] methods = 100 AccessController.doPrivileged(new PrivilegedAction<Method[]>() { 101 public Method[] run() { 102 // Initialize memberTypes and defaultValues 103 return annotationClass.getDeclaredMethods(); 104 } 105 }); 106 107 memberTypes = new HashMap<String,Class<?>>(methods.length+1, 1.0f); 108 memberDefaults = new HashMap<String, Object>(0); 109 members = new HashMap<String, Method>(methods.length+1, 1.0f); 110 111 for (Method method : methods) { 112 if (method.getParameterTypes().length != 0) 113 throw new IllegalArgumentException(method + " has params"); 114 String name = method.getName(); 115 Class<?> type = method.getReturnType(); 116 memberTypes.put(name, invocationHandlerReturnType(type)); 117 members.put(name, method); 118 119 Object defaultValue = method.getDefaultValue(); 120 if (defaultValue != null) 121 memberDefaults.put(name, defaultValue); 122 123 members.put(name, method); 124 } 125 126 sun.misc.SharedSecrets.getJavaLangAccess(). 127 setAnnotationType(annotationClass, this); 128 129 // Initialize retention, & inherited fields. Special treatment 130 // of the corresponding annotation types breaks infinite recursion. 131 if (annotationClass != Retention.class && 132 annotationClass != Inherited.class) { 133 Retention ret = annotationClass.getAnnotation(Retention.class); 134 retention = (ret == null ? RetentionPolicy.CLASS : ret.value()); 135 inherited = annotationClass.isAnnotationPresent(Inherited.class); 136 } 137 } 138 139 /** 140 * Returns the type that must be returned by the invocation handler 141 * of a dynamic proxy in order to have the dynamic proxy return 142 * the specified type (which is assumed to be a legal member type 143 * for an annotation). 144 */ 145 public static Class<?> invocationHandlerReturnType(Class<?> type) { 146 // Translate primitives to wrappers 147 if (type == byte.class) 148 return Byte.class; 149 if (type == char.class) 150 return Character.class; 151 if (type == double.class) 152 return Double.class; 153 if (type == float.class) 154 return Float.class; 155 if (type == int.class) 156 return Integer.class; 157 if (type == long.class) 158 return Long.class; 159 if (type == short.class) 160 return Short.class; 161 if (type == boolean.class) 162 return Boolean.class; 163 164 // Otherwise, just return declared type 165 return type; 166 } 167 168 /** 169 * Returns member types for this annotation type 170 * (member name -> type mapping). 171 */ 172 public Map<String, Class<?>> memberTypes() { 173 return memberTypes; 174 } 175 176 /** 177 * Returns members of this annotation type 178 * (member name -> associated Method object mapping). 179 */ 180 public Map<String, Method> members() { 181 return members; 182 } 183 184 /** 185 * Returns the default values for this annotation type 186 * (Member name -> default value mapping). 187 */ 188 public Map<String, Object> memberDefaults() { 189 return memberDefaults; 190 } 191 192 /** 193 * Returns the retention policy for this annotation type. 194 */ 195 public RetentionPolicy retention() { 196 return retention; 197 } 198 199 /** 200 * Returns true if this this annotation type is inherited. 201 */ 202 public boolean isInherited() { 203 return inherited; 204 } 205 206 /** 207 * For debugging. 208 */ 209 public String toString() { 210 StringBuffer s = new StringBuffer("Annotation Type:" + "\n"); 211 s.append(" Member types: " + memberTypes + "\n"); 212 s.append(" Member defaults: " + memberDefaults + "\n"); 213 s.append(" Retention policy: " + retention + "\n"); 214 s.append(" Inherited: " + inherited); 215 return s.toString(); 216 } 217 }