1 /*
   2  * Copyright (c) 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 package java.lang.reflect;
  26 
  27 import java.lang.annotation.*;
  28 import java.util.Objects;
  29 import sun.reflect.annotation.AnnotationSupport;
  30 import sun.reflect.annotation.AnnotationType;
  31 
  32 /**
  33  * This class provides static utility methods to be called by
  34  * implementations of {@link AnnotatedElement}, including the default
  35  * methods in that interface.
  36  */
  37 class AnnotatedElementSupport {
  38     public static <T extends Annotation> T[] getAnnotationsByTypeDeclaredOrNot(AnnotatedElement element,
  39                                                                                Class<T> annotationClass,
  40                                                                                boolean declared) {
  41         Objects.requireNonNull(annotationClass);
  42         T annotation = (declared ?
  43                         element.getDeclaredAnnotation(annotationClass):
  44                         element.getAnnotation(annotationClass));
  45         @SuppressWarnings("unchecked")
  46         T[] annotations =  (T[]) Array.newInstance(annotationClass, 0);
  47         int resultSize = 0;
  48 
  49         // If the annotation type is directly present, return it boxed
  50         // in an array.
  51         if (annotation != null) {
  52             resultSize = 1;
  53         } else { // Otherwise, if the annotation type is repeatable,
  54                  // look through a container if one is present
  55             Repeatable repeatable = annotationClass.getAnnotation(Repeatable.class);
  56             if (repeatable != null) { // T is a repeatable annotation type
  57                 Class<? extends Annotation> containerType = repeatable.value();
  58                 Annotation container = (declared ?
  59                                         element.getDeclaredAnnotation(containerType):
  60                                         element.getAnnotation(containerType));
  61                 if (container != null) {
  62                     annotations = AnnotationSupport.getValueArray(container);
  63                     resultSize = annotations.length;
  64                     if (resultSize == 1)
  65                         annotation = annotations[0];
  66                 }
  67             }
  68         }
  69 
  70         @SuppressWarnings("unchecked")
  71         T[] returnValue =  (T[]) Array.newInstance(annotationClass, resultSize);
  72         switch (resultSize) {
  73         case 0:
  74             break; // Nothing else do to
  75         case 1:
  76             returnValue[0] = annotation;
  77             break;
  78         default:
  79             for (int i = 0; i < resultSize; i++) {
  80                 returnValue[i] = annotations[i];
  81             }
  82             break;
  83         }
  84         return returnValue;
  85     }
  86 }