< prev index next >

src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java

Print this page

        

*** 60,70 **** actualTypeAnnos, allOnSameTarget, decl); if (type instanceof Class) { return new AnnotatedTypeBaseImpl(type, ! addNesting(type, currentLoc), actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof TypeVariable) { return new AnnotatedTypeVariableImpl((TypeVariable)type, --- 60,70 ---- actualTypeAnnos, allOnSameTarget, decl); if (type instanceof Class) { return new AnnotatedTypeBaseImpl(type, ! currentLoc, actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof TypeVariable) { return new AnnotatedTypeVariableImpl((TypeVariable)type,
*** 72,82 **** actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof ParameterizedType) { return new AnnotatedParameterizedTypeImpl((ParameterizedType)type, ! addNesting(type, currentLoc), actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof WildcardType) { return new AnnotatedWildcardTypeImpl((WildcardType) type, --- 72,82 ---- actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof ParameterizedType) { return new AnnotatedParameterizedTypeImpl((ParameterizedType)type, ! currentLoc, actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof WildcardType) { return new AnnotatedWildcardTypeImpl((WildcardType) type,
*** 86,110 **** decl); } throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen."); } ! private static LocationInfo addNesting(Type type, LocationInfo addTo) { if (isArray(type)) return addTo; if (type instanceof Class) { Class<?> clz = (Class)type; if (clz.getEnclosingClass() == null) return addTo; if (Modifier.isStatic(clz.getModifiers())) ! return addNesting(clz.getEnclosingClass(), addTo); ! return addNesting(clz.getEnclosingClass(), addTo.pushInner()); } else if (type instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType)type; if (t.getOwnerType() == null) return addTo; ! return addNesting(t.getOwnerType(), addTo.pushInner()); } return addTo; } private static boolean isArray(Type t) { --- 86,110 ---- decl); } throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen."); } ! public static LocationInfo nestingForType(Type type, LocationInfo addTo) { if (isArray(type)) return addTo; if (type instanceof Class) { Class<?> clz = (Class)type; if (clz.getEnclosingClass() == null) return addTo; if (Modifier.isStatic(clz.getModifiers())) ! return nestingForType(clz.getEnclosingClass(), addTo); ! return nestingForType(clz.getEnclosingClass(), addTo.pushInner()); } else if (type instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType)type; if (t.getOwnerType() == null) return addTo; ! return nestingForType(t.getOwnerType(), addTo.pushInner()); } return addTo; } private static boolean isArray(Type t) {
*** 119,128 **** --- 119,129 ---- } static final AnnotatedType EMPTY_ANNOTATED_TYPE = new AnnotatedTypeBaseImpl(null, LocationInfo.BASE_LOCATION, new TypeAnnotation[0], new TypeAnnotation[0], null); static final AnnotatedType[] EMPTY_ANNOTATED_TYPE_ARRAY = new AnnotatedType[0]; + static final TypeAnnotation[] EMPTY_TYPE_ANNOTATION_ARRAY = new TypeAnnotation[0]; private static class AnnotatedTypeBaseImpl implements AnnotatedType { private final Type type; private final AnnotatedElement decl; private final LocationInfo location;
*** 175,184 **** --- 176,209 ---- @Override public final Type getType() { return type; } + @Override + public AnnotatedType getAnnotatedOwnerType() { + if (!(type instanceof Class<?>)) + throw new IllegalStateException("Can't compute owner"); + + Class<?> inner = (Class<?>)type; + Class<?> owner = inner.getDeclaringClass(); + if (owner == null) // top-level, local or anonymous + return null; + if (inner.isPrimitive() || inner == Void.TYPE) + return null; + + LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1)); + TypeAnnotation[]all = getTypeAnnotations(); + List<TypeAnnotation> l = new ArrayList<>(all.length); + + for (TypeAnnotation t : all) + if (t.getLocationInfo().isSameLocationInfo(outerLoc)) + l.add(t); + + return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl()); + + } + // Implementation details final LocationInfo getLocation() { return location; } final TypeAnnotation[] getTypeAnnotations() {
*** 196,212 **** super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); } @Override public AnnotatedType getAnnotatedGenericComponentType() { ! return AnnotatedTypeFactory.buildAnnotatedType(getComponentType(), ! getLocation().pushArray(), getTypeAnnotations(), getTypeAnnotations(), getDecl()); } private Type getComponentType() { Type t = getType(); if (t instanceof Class) { Class<?> c = (Class)t; return c.getComponentType(); --- 221,243 ---- super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl); } @Override public AnnotatedType getAnnotatedGenericComponentType() { ! Type t = getComponentType(); ! return AnnotatedTypeFactory.buildAnnotatedType(t, ! nestingForType(t, getLocation().pushArray()), getTypeAnnotations(), getTypeAnnotations(), getDecl()); } + @Override + public AnnotatedType getAnnotatedOwnerType() { + return null; + } + private Type getComponentType() { Type t = getType(); if (t instanceof Class) { Class<?> c = (Class)t; return c.getComponentType();
*** 225,234 **** --- 256,270 ---- @Override public AnnotatedType[] getAnnotatedBounds() { return getTypeVariable().getAnnotatedBounds(); } + @Override + public AnnotatedType getAnnotatedOwnerType() { + return null; + } + private TypeVariable<?> getTypeVariable() { return (TypeVariable)getType(); } }
*** 246,256 **** AnnotatedType[] res = new AnnotatedType[arguments.length]; Arrays.fill(res, EMPTY_ANNOTATED_TYPE); int initialCapacity = getTypeAnnotations().length; for (int i = 0; i < res.length; i++) { List<TypeAnnotation> l = new ArrayList<>(initialCapacity); ! LocationInfo newLoc = getLocation().pushTypeArg((byte)i); for (TypeAnnotation t : getTypeAnnotations()) if (t.getLocationInfo().isSameLocationInfo(newLoc)) l.add(t); res[i] = buildAnnotatedType(arguments[i], newLoc, --- 282,292 ---- AnnotatedType[] res = new AnnotatedType[arguments.length]; Arrays.fill(res, EMPTY_ANNOTATED_TYPE); int initialCapacity = getTypeAnnotations().length; for (int i = 0; i < res.length; i++) { List<TypeAnnotation> l = new ArrayList<>(initialCapacity); ! LocationInfo newLoc = nestingForType(arguments[i], getLocation().pushTypeArg((byte)i)); for (TypeAnnotation t : getTypeAnnotations()) if (t.getLocationInfo().isSameLocationInfo(newLoc)) l.add(t); res[i] = buildAnnotatedType(arguments[i], newLoc,
*** 259,268 **** --- 295,320 ---- getDecl()); } return res; } + @Override + public AnnotatedType getAnnotatedOwnerType() { + Type owner = getParameterizedType().getOwnerType(); + if (owner == null) + return null; + LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1)); + TypeAnnotation[]all = getTypeAnnotations(); + List<TypeAnnotation> l = new ArrayList<>(all.length); + + for (TypeAnnotation t : all) + if (t.getLocationInfo().isSameLocationInfo(outerLoc)) + l.add(t); + + return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl()); + } + private ParameterizedType getParameterizedType() { return (ParameterizedType)getType(); } }
*** 293,308 **** if (hasUpperBounds) return new AnnotatedType[0]; return getAnnotatedBounds(getWildcardType().getLowerBounds()); } private AnnotatedType[] getAnnotatedBounds(Type[] bounds) { AnnotatedType[] res = new AnnotatedType[bounds.length]; Arrays.fill(res, EMPTY_ANNOTATED_TYPE); - LocationInfo newLoc = getLocation().pushWildcard(); int initialCapacity = getTypeAnnotations().length; for (int i = 0; i < res.length; i++) { List<TypeAnnotation> l = new ArrayList<>(initialCapacity); for (TypeAnnotation t : getTypeAnnotations()) if (t.getLocationInfo().isSameLocationInfo(newLoc)) l.add(t); res[i] = buildAnnotatedType(bounds[i], --- 345,365 ---- if (hasUpperBounds) return new AnnotatedType[0]; return getAnnotatedBounds(getWildcardType().getLowerBounds()); } + @Override + public AnnotatedType getAnnotatedOwnerType() { + return null; + } + private AnnotatedType[] getAnnotatedBounds(Type[] bounds) { AnnotatedType[] res = new AnnotatedType[bounds.length]; Arrays.fill(res, EMPTY_ANNOTATED_TYPE); int initialCapacity = getTypeAnnotations().length; for (int i = 0; i < res.length; i++) { + LocationInfo newLoc = nestingForType(bounds[i], getLocation().pushWildcard()); List<TypeAnnotation> l = new ArrayList<>(initialCapacity); for (TypeAnnotation t : getTypeAnnotations()) if (t.getLocationInfo().isSameLocationInfo(newLoc)) l.add(t); res[i] = buildAnnotatedType(bounds[i],
< prev index next >