jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
changeset 34708 4a1e3728135c
parent 31139 d48c3edf6a55
equal deleted inserted replaced
34707:5866a10ac337 34708:4a1e3728135c
    60                     actualTypeAnnos,
    60                     actualTypeAnnos,
    61                     allOnSameTarget,
    61                     allOnSameTarget,
    62                     decl);
    62                     decl);
    63         if (type instanceof Class) {
    63         if (type instanceof Class) {
    64             return new AnnotatedTypeBaseImpl(type,
    64             return new AnnotatedTypeBaseImpl(type,
    65                     addNesting(type, currentLoc),
    65                     currentLoc,
    66                     actualTypeAnnos,
    66                     actualTypeAnnos,
    67                     allOnSameTarget,
    67                     allOnSameTarget,
    68                     decl);
    68                     decl);
    69         } else if (type instanceof TypeVariable) {
    69         } else if (type instanceof TypeVariable) {
    70             return new AnnotatedTypeVariableImpl((TypeVariable)type,
    70             return new AnnotatedTypeVariableImpl((TypeVariable)type,
    72                     actualTypeAnnos,
    72                     actualTypeAnnos,
    73                     allOnSameTarget,
    73                     allOnSameTarget,
    74                     decl);
    74                     decl);
    75         } else if (type instanceof ParameterizedType) {
    75         } else if (type instanceof ParameterizedType) {
    76             return new AnnotatedParameterizedTypeImpl((ParameterizedType)type,
    76             return new AnnotatedParameterizedTypeImpl((ParameterizedType)type,
    77                     addNesting(type, currentLoc),
    77                     currentLoc,
    78                     actualTypeAnnos,
    78                     actualTypeAnnos,
    79                     allOnSameTarget,
    79                     allOnSameTarget,
    80                     decl);
    80                     decl);
    81         } else if (type instanceof WildcardType) {
    81         } else if (type instanceof WildcardType) {
    82             return new AnnotatedWildcardTypeImpl((WildcardType) type,
    82             return new AnnotatedWildcardTypeImpl((WildcardType) type,
    86                     decl);
    86                     decl);
    87         }
    87         }
    88         throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen.");
    88         throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen.");
    89     }
    89     }
    90 
    90 
    91     private static LocationInfo addNesting(Type type, LocationInfo addTo) {
    91     public static LocationInfo nestingForType(Type type, LocationInfo addTo) {
    92         if (isArray(type))
    92         if (isArray(type))
    93             return addTo;
    93             return addTo;
    94         if (type instanceof Class) {
    94         if (type instanceof Class) {
    95             Class<?> clz = (Class)type;
    95             Class<?> clz = (Class)type;
    96             if (clz.getEnclosingClass() == null)
    96             if (clz.getEnclosingClass() == null)
    97                 return addTo;
    97                 return addTo;
    98             if (Modifier.isStatic(clz.getModifiers()))
    98             if (Modifier.isStatic(clz.getModifiers()))
    99                 return addNesting(clz.getEnclosingClass(), addTo);
    99                 return nestingForType(clz.getEnclosingClass(), addTo);
   100             return addNesting(clz.getEnclosingClass(), addTo.pushInner());
   100             return nestingForType(clz.getEnclosingClass(), addTo.pushInner());
   101         } else if (type instanceof ParameterizedType) {
   101         } else if (type instanceof ParameterizedType) {
   102             ParameterizedType t = (ParameterizedType)type;
   102             ParameterizedType t = (ParameterizedType)type;
   103             if (t.getOwnerType() == null)
   103             if (t.getOwnerType() == null)
   104                 return addTo;
   104                 return addTo;
   105             return addNesting(t.getOwnerType(), addTo.pushInner());
   105             return nestingForType(t.getOwnerType(), addTo.pushInner());
   106         }
   106         }
   107         return addTo;
   107         return addTo;
   108     }
   108     }
   109 
   109 
   110     private static boolean isArray(Type t) {
   110     private static boolean isArray(Type t) {
   116             return true;
   116             return true;
   117         }
   117         }
   118         return false;
   118         return false;
   119     }
   119     }
   120 
   120 
       
   121     static final TypeAnnotation[] EMPTY_TYPE_ANNOTATION_ARRAY = new TypeAnnotation[0];
   121     static final AnnotatedType EMPTY_ANNOTATED_TYPE = new AnnotatedTypeBaseImpl(null, LocationInfo.BASE_LOCATION,
   122     static final AnnotatedType EMPTY_ANNOTATED_TYPE = new AnnotatedTypeBaseImpl(null, LocationInfo.BASE_LOCATION,
   122                                                             new TypeAnnotation[0], new TypeAnnotation[0], null);
   123             EMPTY_TYPE_ANNOTATION_ARRAY, EMPTY_TYPE_ANNOTATION_ARRAY, null);
   123     static final AnnotatedType[] EMPTY_ANNOTATED_TYPE_ARRAY = new AnnotatedType[0];
   124     static final AnnotatedType[] EMPTY_ANNOTATED_TYPE_ARRAY = new AnnotatedType[0];
   124 
   125 
   125     private static class AnnotatedTypeBaseImpl implements AnnotatedType {
   126     private static class AnnotatedTypeBaseImpl implements AnnotatedType {
   126         private final Type type;
   127         private final Type type;
   127         private final AnnotatedElement decl;
   128         private final AnnotatedElement decl;
   175         @Override
   176         @Override
   176         public final Type getType() {
   177         public final Type getType() {
   177             return type;
   178             return type;
   178         }
   179         }
   179 
   180 
       
   181         @Override
       
   182         public AnnotatedType getAnnotatedOwnerType() {
       
   183             if (!(type instanceof Class<?>))
       
   184                 throw new IllegalStateException("Can't compute owner");
       
   185 
       
   186             Class<?> inner = (Class<?>)type;
       
   187             Class<?> owner = inner.getDeclaringClass();
       
   188             if (owner == null) // top-level, local or anonymous
       
   189                 return null;
       
   190             if (inner.isPrimitive() || inner == Void.TYPE)
       
   191                 return null;
       
   192 
       
   193             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
       
   194             TypeAnnotation[]all = getTypeAnnotations();
       
   195             List<TypeAnnotation> l = new ArrayList<>(all.length);
       
   196 
       
   197             for (TypeAnnotation t : all)
       
   198                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
       
   199                     l.add(t);
       
   200 
       
   201             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
       
   202 
       
   203         }
       
   204 
   180         // Implementation details
   205         // Implementation details
   181         final LocationInfo getLocation() {
   206         final LocationInfo getLocation() {
   182             return location;
   207             return location;
   183         }
   208         }
   184         final TypeAnnotation[] getTypeAnnotations() {
   209         final TypeAnnotation[] getTypeAnnotations() {
   196             super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl);
   221             super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl);
   197         }
   222         }
   198 
   223 
   199         @Override
   224         @Override
   200         public AnnotatedType getAnnotatedGenericComponentType() {
   225         public AnnotatedType getAnnotatedGenericComponentType() {
   201             return AnnotatedTypeFactory.buildAnnotatedType(getComponentType(),
   226             Type t = getComponentType();
   202                                                            getLocation().pushArray(),
   227             return AnnotatedTypeFactory.buildAnnotatedType(t,
   203                                                            getTypeAnnotations(),
   228                     nestingForType(t, getLocation().pushArray()),
   204                                                            getTypeAnnotations(),
   229                     getTypeAnnotations(),
   205                                                            getDecl());
   230                     getTypeAnnotations(),
       
   231                     getDecl());
       
   232         }
       
   233 
       
   234         @Override
       
   235         public AnnotatedType getAnnotatedOwnerType() {
       
   236             return null;
   206         }
   237         }
   207 
   238 
   208         private Type getComponentType() {
   239         private Type getComponentType() {
   209             Type t = getType();
   240             Type t = getType();
   210             if (t instanceof Class) {
   241             if (t instanceof Class) {
   225         @Override
   256         @Override
   226         public AnnotatedType[] getAnnotatedBounds() {
   257         public AnnotatedType[] getAnnotatedBounds() {
   227             return getTypeVariable().getAnnotatedBounds();
   258             return getTypeVariable().getAnnotatedBounds();
   228         }
   259         }
   229 
   260 
       
   261         @Override
       
   262         public AnnotatedType getAnnotatedOwnerType() {
       
   263             return null;
       
   264         }
       
   265 
   230         private TypeVariable<?> getTypeVariable() {
   266         private TypeVariable<?> getTypeVariable() {
   231             return (TypeVariable)getType();
   267             return (TypeVariable)getType();
   232         }
   268         }
   233     }
   269     }
   234 
   270 
   246             AnnotatedType[] res = new AnnotatedType[arguments.length];
   282             AnnotatedType[] res = new AnnotatedType[arguments.length];
   247             Arrays.fill(res, EMPTY_ANNOTATED_TYPE);
   283             Arrays.fill(res, EMPTY_ANNOTATED_TYPE);
   248             int initialCapacity = getTypeAnnotations().length;
   284             int initialCapacity = getTypeAnnotations().length;
   249             for (int i = 0; i < res.length; i++) {
   285             for (int i = 0; i < res.length; i++) {
   250                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
   286                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
   251                 LocationInfo newLoc = getLocation().pushTypeArg((byte)i);
   287                 LocationInfo newLoc = nestingForType(arguments[i], getLocation().pushTypeArg((byte)i));
   252                 for (TypeAnnotation t : getTypeAnnotations())
   288                 for (TypeAnnotation t : getTypeAnnotations())
   253                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
   289                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
   254                         l.add(t);
   290                         l.add(t);
   255                 res[i] = buildAnnotatedType(arguments[i],
   291                 res[i] = buildAnnotatedType(arguments[i],
   256                                             newLoc,
   292                         newLoc,
   257                                             l.toArray(new TypeAnnotation[0]),
   293                         l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
   258                                             getTypeAnnotations(),
   294                         getTypeAnnotations(),
   259                                             getDecl());
   295                         getDecl());
   260             }
   296             }
   261             return res;
   297             return res;
       
   298         }
       
   299 
       
   300         @Override
       
   301         public AnnotatedType getAnnotatedOwnerType() {
       
   302             Type owner = getParameterizedType().getOwnerType();
       
   303             if (owner == null)
       
   304                 return null;
       
   305             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
       
   306             TypeAnnotation[]all = getTypeAnnotations();
       
   307             List<TypeAnnotation> l = new ArrayList<>(all.length);
       
   308 
       
   309             for (TypeAnnotation t : all)
       
   310                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
       
   311                     l.add(t);
       
   312 
       
   313             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
   262         }
   314         }
   263 
   315 
   264         private ParameterizedType getParameterizedType() {
   316         private ParameterizedType getParameterizedType() {
   265             return (ParameterizedType)getType();
   317             return (ParameterizedType)getType();
   266         }
   318         }
   277 
   329 
   278         @Override
   330         @Override
   279         public AnnotatedType[] getAnnotatedUpperBounds() {
   331         public AnnotatedType[] getAnnotatedUpperBounds() {
   280             if (!hasUpperBounds()) {
   332             if (!hasUpperBounds()) {
   281                 return new AnnotatedType[] { buildAnnotatedType(Object.class,
   333                 return new AnnotatedType[] { buildAnnotatedType(Object.class,
   282                                                                 LocationInfo.BASE_LOCATION,
   334                         LocationInfo.BASE_LOCATION,
   283                                                                 new TypeAnnotation[0],
   335                         EMPTY_TYPE_ANNOTATION_ARRAY,
   284                                                                 new TypeAnnotation[0],
   336                         EMPTY_TYPE_ANNOTATION_ARRAY,
   285                                                                 null)
   337                         null)
   286                                            };
   338                 };
   287             }
   339             }
   288             return getAnnotatedBounds(getWildcardType().getUpperBounds());
   340             return getAnnotatedBounds(getWildcardType().getUpperBounds());
   289         }
   341         }
   290 
   342 
   291         @Override
   343         @Override
   293             if (hasUpperBounds)
   345             if (hasUpperBounds)
   294                 return new AnnotatedType[0];
   346                 return new AnnotatedType[0];
   295             return getAnnotatedBounds(getWildcardType().getLowerBounds());
   347             return getAnnotatedBounds(getWildcardType().getLowerBounds());
   296         }
   348         }
   297 
   349 
       
   350         @Override
       
   351         public AnnotatedType getAnnotatedOwnerType() {
       
   352             return null;
       
   353         }
       
   354 
   298         private AnnotatedType[] getAnnotatedBounds(Type[] bounds) {
   355         private AnnotatedType[] getAnnotatedBounds(Type[] bounds) {
   299             AnnotatedType[] res = new AnnotatedType[bounds.length];
   356             AnnotatedType[] res = new AnnotatedType[bounds.length];
   300             Arrays.fill(res, EMPTY_ANNOTATED_TYPE);
   357             Arrays.fill(res, EMPTY_ANNOTATED_TYPE);
   301             LocationInfo newLoc = getLocation().pushWildcard();
       
   302             int initialCapacity = getTypeAnnotations().length;
   358             int initialCapacity = getTypeAnnotations().length;
   303             for (int i = 0; i < res.length; i++) {
   359             for (int i = 0; i < res.length; i++) {
       
   360                 LocationInfo newLoc = nestingForType(bounds[i], getLocation().pushWildcard());
   304                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
   361                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
   305                 for (TypeAnnotation t : getTypeAnnotations())
   362                 for (TypeAnnotation t : getTypeAnnotations())
   306                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
   363                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
   307                         l.add(t);
   364                         l.add(t);
   308                 res[i] = buildAnnotatedType(bounds[i],
   365                 res[i] = buildAnnotatedType(bounds[i],
   309                                             newLoc,
   366                         newLoc,
   310                                             l.toArray(new TypeAnnotation[0]),
   367                         l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
   311                                             getTypeAnnotations(),
   368                         getTypeAnnotations(),
   312                                             getDecl());
   369                         getDecl());
   313             }
   370             }
   314             return res;
   371             return res;
   315         }
   372         }
   316 
   373 
   317         private WildcardType getWildcardType() {
   374         private WildcardType getWildcardType() {