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

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 23,39 **** * questions. */ package sun.reflect.generics.reflectiveObjects; ! import java.lang.annotation.Annotation; import java.lang.reflect.Array; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Objects; ! import sun.reflect.generics.factory.GenericsFactory; import sun.reflect.generics.tree.FieldTypeSignature; import sun.reflect.generics.visitor.Reifier; /** --- 23,44 ---- * questions. */ package sun.reflect.generics.reflectiveObjects; ! import java.lang.annotation.*; ! import java.lang.reflect.AnnotatedType; import java.lang.reflect.Array; import java.lang.reflect.GenericDeclaration; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; + import java.util.LinkedHashMap; + import java.util.Map; import java.util.Objects; ! import sun.reflect.annotation.AnnotationSupport; ! import sun.reflect.annotation.TypeAnnotationParser; ! import sun.reflect.annotation.AnnotationType; import sun.reflect.generics.factory.GenericsFactory; import sun.reflect.generics.tree.FieldTypeSignature; import sun.reflect.generics.visitor.Reifier; /**
*** 186,226 **** public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { Objects.requireNonNull(annotationClass); return false; } public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return null; } public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return null; } @SuppressWarnings("unchecked") public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! // safe because annotationClass is the class for T ! return (T[])Array.newInstance(annotationClass, 0); } @SuppressWarnings("unchecked") public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! // safe because annotationClass is the class for T ! return (T[])Array.newInstance(annotationClass, 0); } public Annotation[] getAnnotations() { ! // Since zero-length, don't need defensive clone ! return EMPTY_ANNOTATION_ARRAY; } public Annotation[] getDeclaredAnnotations() { ! // Since zero-length, don't need defensive clone ! return EMPTY_ANNOTATION_ARRAY; } private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0]; } --- 191,261 ---- public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { Objects.requireNonNull(annotationClass); return false; } + @SuppressWarnings("unchecked") public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return (T)mapAnnotations(getAnnotations()).get(annotationClass); } public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return getAnnotation(annotationClass); } @SuppressWarnings("unchecked") public <T extends Annotation> T[] getAnnotations(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return AnnotationSupport.getMultipleAnnotations(mapAnnotations(getAnnotations()), annotationClass); } @SuppressWarnings("unchecked") public <T extends Annotation> T[] getDeclaredAnnotations(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); ! return getAnnotations(annotationClass); } public Annotation[] getAnnotations() { ! int myIndex = typeVarIndex(); ! if (myIndex < 0) ! return EMPTY_ANNOTATION_ARRAY; // This should not happen ! return TypeAnnotationParser.parseTypeParameterAnnotations(getGenericDeclaration(), myIndex); } public Annotation[] getDeclaredAnnotations() { ! return getAnnotations(); ! } ! ! public AnnotatedType[] getAnnotatedBounds() { ! return TypeAnnotationParser.parseAnnotatedBounds(getBounds(), ! getGenericDeclaration(), ! typeVarIndex()); } private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0]; + + private int typeVarIndex() { + TypeVariable<?>[] tVars = getGenericDeclaration().getTypeParameters(); + int i = -1; + for (TypeVariable<?> v : tVars) { + i++; + if (equals(v)) + return i; + } + return -1; + } + + private static Map<Class<? extends Annotation>, Annotation> mapAnnotations(Annotation[] annos) { + Map<Class<? extends Annotation>, Annotation> result = + new LinkedHashMap<Class<? extends Annotation>, Annotation>(); + for (Annotation a : annos) { + Class<? extends Annotation> klass = a.annotationType(); + AnnotationType type = AnnotationType.getInstance(klass); + if (type.retention() == RetentionPolicy.RUNTIME) + if (result.put(klass, a) != null) + throw new AnnotationFormatError("Duplicate annotation for class: "+klass+": " + a); + } + return result; + } }