1 /*
   2  * Copyright (c) 2003, 2013, 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 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      */
  55     private final Map<String, Object> memberDefaults;
  56 
  57     /**
  58      * Member name -> Method object mapping. This (and its assoicated
  59      * accessor) are used only to generate AnnotationTypeMismatchExceptions.
  60      */
  61     private final Map<String, Method> members;
  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      * @throw IllegalArgumentException if the specified class object for
  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      * @throw IllegalArgumentException if the specified class object for
 102      *     does not represent a valid annotation type
 103      */
 104     private AnnotationType(final Class<? extends Annotation> annotationClass) {
 105         if (!annotationClass.isAnnotation())
 106             throw new IllegalArgumentException("Not an annotation type");
 107 
 108         // Initialize memberTypes and defaultValues
 109         Method[] methods =
 110             AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
 111                 public Method[] run() {
 112                     Method[] methods = annotationClass.getDeclaredMethods();
 113                     AccessibleObject.setAccessible(methods, true);
 114                     return methods;
 115                 }
 116             });
 117 
 118         memberTypes = new HashMap<String,Class<?>>(methods.length+1, 1.0f);
 119         memberDefaults = new HashMap<String, Object>(0);
 120         members = new HashMap<String, Method>(methods.length+1, 1.0f);
 121 
 122         for (Method method :  methods) {
 123             if (method.getParameterTypes().length != 0)
 124                 throw new IllegalArgumentException(method + " has params");
 125             String name = method.getName();
 126             Class<?> type = method.getReturnType();
 127             memberTypes.put(name, invocationHandlerReturnType(type));
 128             members.put(name, method);
 129 
 130             Object defaultValue = method.getDefaultValue();
 131             if (defaultValue != null)
 132                 memberDefaults.put(name, defaultValue);
 133         }
 134 
 135         // Initialize retention, & inherited fields.  Special treatment
 136         // of the corresponding annotation types breaks infinite recursion.
 137         if (annotationClass != Retention.class &&
 138             annotationClass != Inherited.class) {
 139             JavaLangAccess jla = sun.misc.SharedSecrets.getJavaLangAccess();
 140             Map<Class<? extends Annotation>, Annotation> metaAnnotations =
 141                 AnnotationParser.parseSelectAnnotations(
 142                     jla.getRawClassAnnotations(annotationClass),
 143                     jla.getConstantPool(annotationClass),
 144                     annotationClass,
 145                     Retention.class, Inherited.class
 146                 );
 147             Retention ret = (Retention) metaAnnotations.get(Retention.class);
 148             retention = (ret == null ? RetentionPolicy.CLASS : ret.value());
 149             inherited = metaAnnotations.containsKey(Inherited.class);
 150         }
 151         else {
 152             retention = RetentionPolicy.RUNTIME;
 153             inherited = false;
 154         }
 155     }
 156 
 157     /**
 158      * Returns the type that must be returned by the invocation handler
 159      * of a dynamic proxy in order to have the dynamic proxy return
 160      * the specified type (which is assumed to be a legal member type
 161      * for an annotation).
 162      */
 163     public static Class<?> invocationHandlerReturnType(Class<?> type) {
 164         // Translate primitives to wrappers
 165         if (type == byte.class)
 166             return Byte.class;
 167         if (type == char.class)
 168             return Character.class;
 169         if (type == double.class)
 170             return Double.class;
 171         if (type == float.class)
 172             return Float.class;
 173         if (type == int.class)
 174             return Integer.class;
 175         if (type == long.class)
 176             return Long.class;
 177         if (type == short.class)
 178             return Short.class;
 179         if (type == boolean.class)
 180             return Boolean.class;
 181 
 182         // Otherwise, just return declared type
 183         return type;
 184     }
 185 
 186     /**
 187      * Returns member types for this annotation type
 188      * (member name -> type mapping).
 189      */
 190     public Map<String, Class<?>> memberTypes() {
 191         return memberTypes;
 192     }
 193 
 194     /**
 195      * Returns members of this annotation type
 196      * (member name -> associated Method object mapping).
 197      */
 198     public Map<String, Method> members() {
 199         return members;
 200     }
 201 
 202     /**
 203      * Returns the default values for this annotation type
 204      * (Member name -> default value mapping).
 205      */
 206     public Map<String, Object> memberDefaults() {
 207         return memberDefaults;
 208     }
 209 
 210     /**
 211      * Returns the retention policy for this annotation type.
 212      */
 213     public RetentionPolicy retention() {
 214         return retention;
 215     }
 216 
 217     /**
 218      * Returns true if this this annotation type is inherited.
 219      */
 220     public boolean isInherited() {
 221         return inherited;
 222     }
 223 
 224     /**
 225      * For debugging.
 226      */
 227     public String toString() {
 228         return "Annotation Type:\n" +
 229                "   Member types: " + memberTypes + "\n" +
 230                "   Member defaults: " + memberDefaults + "\n" +
 231                "   Retention policy: " + retention + "\n" +
 232                "   Inherited: " + inherited;
 233     }
 234 }