src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2012, 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.generics.reflectiveObjects;
  27 
  28 import java.lang.annotation.Annotation;

  29 import java.lang.reflect.Array;
  30 import java.lang.reflect.GenericDeclaration;
  31 import java.lang.reflect.Type;
  32 import java.lang.reflect.TypeVariable;


  33 import java.util.Objects;
  34 


  35 import sun.reflect.generics.factory.GenericsFactory;
  36 import sun.reflect.generics.tree.FieldTypeSignature;
  37 import sun.reflect.generics.visitor.Reifier;
  38 
  39 /**
  40  * Implementation of <tt>java.lang.reflect.TypeVariable</tt> interface
  41  * for core reflection.
  42  */
  43 public class TypeVariableImpl<D extends GenericDeclaration>
  44     extends LazyReflectiveObjectGenerator implements TypeVariable<D> {
  45     D genericDeclaration;
  46     private String name;
  47     // upper bounds - evaluated lazily
  48     private Type[] bounds;
  49 
  50     // The ASTs for the bounds. We are required to evaluate the bounds
  51     // lazily, so we store these at least until we are first asked
  52     // for the bounds. This also neatly solves the
  53     // problem with F-bounds - you can't reify them before the formal
  54     // is defined.


 171                  genericDeclaration.equals(thatDecl)) &&
 172                 (name == null ?
 173                  thatName == null :
 174                  name.equals(thatName));
 175 
 176         } else
 177             return false;
 178     }
 179 
 180     @Override
 181     public int hashCode() {
 182         return genericDeclaration.hashCode() ^ name.hashCode();
 183     }
 184 
 185     // Currently vacuous implementations of AnnotatedElement methods.
 186     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
 187         Objects.requireNonNull(annotationClass);
 188         return false;
 189     }
 190 

 191     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 192         Objects.requireNonNull(annotationClass);
 193         return null;
 194     }
 195 
 196     public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
 197         Objects.requireNonNull(annotationClass);
 198         return null;
 199     }
 200 
 201     @SuppressWarnings("unchecked")
 202     public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
 203         Objects.requireNonNull(annotationClass);
 204         // safe because annotationClass is the class for T
 205         return (T[])Array.newInstance(annotationClass, 0);
 206     }
 207 
 208     @SuppressWarnings("unchecked")
 209     public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) {
 210         Objects.requireNonNull(annotationClass);
 211         // safe because annotationClass is the class for T
 212         return (T[])Array.newInstance(annotationClass, 0);
 213     }
 214 
 215     public Annotation[] getAnnotations() {
 216         // Since zero-length, don't need defensive clone
 217         return EMPTY_ANNOTATION_ARRAY;


 218     }
 219 
 220     public Annotation[] getDeclaredAnnotations() {
 221         // Since zero-length, don't need defensive clone
 222         return EMPTY_ANNOTATION_ARRAY;





 223     }
 224 
 225     private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
























 226 }
   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.generics.reflectiveObjects;
  27 
  28 import java.lang.annotation.*;
  29 import java.lang.reflect.AnnotatedType;
  30 import java.lang.reflect.Array;
  31 import java.lang.reflect.GenericDeclaration;
  32 import java.lang.reflect.Type;
  33 import java.lang.reflect.TypeVariable;
  34 import java.util.LinkedHashMap;
  35 import java.util.Map;
  36 import java.util.Objects;
  37 import sun.reflect.annotation.AnnotationSupport;
  38 import sun.reflect.annotation.TypeAnnotationParser;
  39 import sun.reflect.annotation.AnnotationType;
  40 import sun.reflect.generics.factory.GenericsFactory;
  41 import sun.reflect.generics.tree.FieldTypeSignature;
  42 import sun.reflect.generics.visitor.Reifier;
  43 
  44 /**
  45  * Implementation of <tt>java.lang.reflect.TypeVariable</tt> interface
  46  * for core reflection.
  47  */
  48 public class TypeVariableImpl<D extends GenericDeclaration>
  49     extends LazyReflectiveObjectGenerator implements TypeVariable<D> {
  50     D genericDeclaration;
  51     private String name;
  52     // upper bounds - evaluated lazily
  53     private Type[] bounds;
  54 
  55     // The ASTs for the bounds. We are required to evaluate the bounds
  56     // lazily, so we store these at least until we are first asked
  57     // for the bounds. This also neatly solves the
  58     // problem with F-bounds - you can't reify them before the formal
  59     // is defined.


 176                  genericDeclaration.equals(thatDecl)) &&
 177                 (name == null ?
 178                  thatName == null :
 179                  name.equals(thatName));
 180 
 181         } else
 182             return false;
 183     }
 184 
 185     @Override
 186     public int hashCode() {
 187         return genericDeclaration.hashCode() ^ name.hashCode();
 188     }
 189 
 190     // Currently vacuous implementations of AnnotatedElement methods.
 191     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
 192         Objects.requireNonNull(annotationClass);
 193         return false;
 194     }
 195 
 196     @SuppressWarnings("unchecked")
 197     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 198         Objects.requireNonNull(annotationClass);
 199         return (T)mapAnnotations(getAnnotations()).get(annotationClass);
 200     }
 201 
 202     public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
 203         Objects.requireNonNull(annotationClass);
 204         return getAnnotation(annotationClass);
 205     }
 206 
 207     @SuppressWarnings("unchecked")
 208     public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) {
 209         Objects.requireNonNull(annotationClass);
 210         return AnnotationSupport.getMultipleAnnotations(mapAnnotations(getAnnotations()), annotationClass);

 211     }
 212 
 213     @SuppressWarnings("unchecked")
 214     public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) {
 215         Objects.requireNonNull(annotationClass);
 216         return getAnnotations(annotationClass);

 217     }
 218 
 219     public Annotation[] getAnnotations() {
 220         int myIndex = typeVarIndex();
 221         if (myIndex < 0)
 222             return EMPTY_ANNOTATION_ARRAY; // This should not happen
 223         return TypeAnnotationParser.parseTypeParameterAnnotations(getGenericDeclaration(), myIndex);
 224     }
 225 
 226     public Annotation[] getDeclaredAnnotations() {
 227         return getAnnotations();
 228     }
 229 
 230     public AnnotatedType[] getAnnotatedBounds() {
 231         return TypeAnnotationParser.parseAnnotatedBounds(getBounds(),
 232                                                          getGenericDeclaration(),
 233                                                          typeVarIndex());
 234     }
 235 
 236     private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
 237 
 238     private int typeVarIndex() {
 239         TypeVariable<?>[] tVars = getGenericDeclaration().getTypeParameters();
 240         int i = -1;
 241         for (TypeVariable<?> v : tVars) {
 242             i++;
 243             if (equals(v))
 244                 return i;
 245 }
 246         return -1;
 247     }
 248 
 249     private static Map<Class<? extends Annotation>, Annotation> mapAnnotations(Annotation[] annos) {
 250         Map<Class<? extends Annotation>, Annotation> result =
 251             new LinkedHashMap<Class<? extends Annotation>, Annotation>();
 252         for (Annotation a : annos) {
 253             Class<? extends Annotation> klass = a.annotationType();
 254             AnnotationType type = AnnotationType.getInstance(klass);
 255             if (type.retention() == RetentionPolicy.RUNTIME)
 256                 if (result.put(klass, a) != null)
 257                     throw new AnnotationFormatError("Duplicate annotation for class: "+klass+": " + a);
 258         }
 259         return result;
 260     }
 261 }