jdk/src/share/classes/sun/reflect/annotation/AnnotationSupport.java
author darcy
Tue, 26 Feb 2013 17:38:29 -0800
changeset 16037 1cf21ce98a25
parent 15511 8f45487ac694
child 21316 ca0a7cd228c9
permissions -rw-r--r--
8008279: Remove InvalidContainerAnnotationError.java Reviewed-by: jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     1
/*
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     4
 *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    10
 *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    15
 * accompanied this code).
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    16
 *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    20
 *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    23
 * questions.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    24
 */
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    25
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    26
package sun.reflect.annotation;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    27
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    28
import java.lang.annotation.*;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    29
import java.lang.reflect.*;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    30
import java.util.ArrayList;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    31
import java.util.Collections;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    32
import java.util.List;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    33
import java.util.Map;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    34
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    35
public final class AnnotationSupport {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    36
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    37
     * Finds and returns all annotation of the type indicated by
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    38
     * {@code annotationClass} from the {@code Map} {@code
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    39
     * annotationMap}. Looks into containers of the {@code
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    40
     * annotationClass} (as specified by an the {@code
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    41
     * annotationClass} type being meta-annotated with an {@code
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    42
     * Repeatable} annotation).
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    43
     *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    44
     * @param annotationMap the {@code Map} used to store annotations indexed by their type
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    45
     * @param annotationClass the type of annotation to search for
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    46
     *
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    47
     * @return an array of instances of {@code annotationClass} or an empty array if none were found
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    48
     */
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    49
    public static  <A extends Annotation> A[] getMultipleAnnotations(
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    50
            final Map<Class<? extends Annotation>, Annotation> annotationMap,
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    51
            final Class<A> annotationClass) {
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    52
        final List<A> res = new ArrayList<A>();
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    53
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    54
        @SuppressWarnings("unchecked")
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    55
        final A candidate = (A)annotationMap.get(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    56
        if (candidate != null) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    57
            res.add(candidate);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    58
        }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    59
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    60
        final Class<? extends Annotation> containerClass = getContainer(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    61
        if (containerClass != null) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    62
            res.addAll(unpackAll(annotationMap.get(containerClass), annotationClass));
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    63
        }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    64
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    65
        @SuppressWarnings("unchecked") // should be safe annotationClass is a token for A
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    66
        final A[] emptyTemplateArray = (A[])Array.newInstance(annotationClass, 0);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    67
        return res.isEmpty() ? emptyTemplateArray : res.toArray(emptyTemplateArray);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    68
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    69
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    70
    /** Helper to get the container, or null if none, of an annotation. */
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    71
    private static <A extends Annotation> Class<? extends Annotation> getContainer(Class<A> annotationClass) {
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    72
        Repeatable containingAnnotation = annotationClass.getDeclaredAnnotation(Repeatable.class);
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    73
        return (containingAnnotation == null) ? null : containingAnnotation.value();
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    74
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    75
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    76
    /** Reflectively look up and get the returned array from the the
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    77
     * invocation of the value() element on an instance of an
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    78
     * Annotation.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    79
     */
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    80
    private static  <A extends Annotation> A[] getValueArray(Annotation containerInstance) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    81
        try {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    82
            // the spec tells us the container must have an array-valued
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    83
            // value element. Get the AnnotationType, get the "value" element
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    84
            // and invoke it to get the contents.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    85
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    86
            Class<? extends Annotation> containerClass = containerInstance.annotationType();
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    87
            AnnotationType annoType = AnnotationType.getInstance(containerClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    88
            if (annoType == null)
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
    89
                throw new AnnotationFormatError(containerInstance + " is an invalid container for repeating annotations");
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    90
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    91
            Method m = annoType.members().get("value");
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    92
            if (m == null)
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
    93
                throw new AnnotationFormatError(containerInstance +
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
    94
                                                          " is an invalid container for repeating annotations");
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    95
            m.setAccessible(true);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    96
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    97
            @SuppressWarnings("unchecked") // not provably safe, but we catch the ClassCastException
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    98
            A[] a = (A[])m.invoke(containerInstance); // this will erase to (Annotation[]) but we
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
    99
                                                      // do a runtime cast on the return-value
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   100
                                                      // in the methods that call this method
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   101
            return a;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   102
        } catch (IllegalAccessException | // couldnt loosen security
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   103
                 IllegalArgumentException | // parameters doesn't match
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   104
                 InvocationTargetException | // the value method threw an exception
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   105
                 ClassCastException e) { // well, a cast failed ...
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
   106
            throw new AnnotationFormatError(
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   107
                    containerInstance + " is an invalid container for repeating annotations",
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
   108
                    e);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   109
        }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   110
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   111
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   112
    /* Sanity check type of and return a list of all the annotation
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   113
     * instances of type {@code annotationClass} from {@code
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   114
     * containerInstance}.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   115
     */
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   116
    private static <A extends Annotation> List<A> unpackAll(Annotation containerInstance,
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   117
                                                            Class<A> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   118
        if (containerInstance == null) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   119
            return Collections.emptyList(); // container not present
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   120
        }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   121
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   122
        try {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   123
            A[] a = getValueArray(containerInstance);
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   124
            List<A> l = new ArrayList<>(a.length);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   125
            for (int i  = 0; i < a.length; i++)
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   126
                l.add(annotationClass.cast(a[i]));
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   127
            return l;
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   128
        } catch (ClassCastException |
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   129
                 NullPointerException e) {
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
   130
            throw new AnnotationFormatError(
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   131
                    String.format("%s is an invalid container for repeating annotations of type: %s",
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14693
diff changeset
   132
                        containerInstance, annotationClass),
16037
1cf21ce98a25 8008279: Remove InvalidContainerAnnotationError.java
darcy
parents: 15511
diff changeset
   133
                    e);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   134
        }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   135
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
diff changeset
   136
}