下面列出了java.lang.reflect.AnnotatedWildcardType#java.lang.reflect.AnnotatedArrayType 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
private void verifyArrayFieldTypeAnnotations(Class c)
throws NoSuchFieldException, NoSuchMethodException {
Annotation anno;
AnnotatedType at;
at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[0], anno, "array1");
arrayTA[0] = anno;
for (int i = 1; i <= 3; i++) {
at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
anno = at.getAnnotations()[0];
verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
arrayTA[i] = anno;
}
}
@Override
public Set<Type> findConstituentAbstractTypes(AnnotatedType javaType, BuildContext buildContext) {
if (Scalars.isScalar(javaType.getType())
|| ClassUtils.isSubPackage(ClassUtils.getRawType(javaType.getType()).getPackage(), "java.")
|| buildContext.scalarStrategy.isDirectlyDeserializable(javaType)) {
return Collections.emptySet();
}
if (javaType instanceof AnnotatedParameterizedType) {
Set<Type> abstractTypes = Arrays.stream(((AnnotatedParameterizedType) javaType).getAnnotatedActualTypeArguments())
.flatMap(arg -> findConstituentAbstractTypes(arg, buildContext).stream())
.collect(Collectors.toSet());
abstractTypes.addAll(findAbstract(javaType, buildContext));
return abstractTypes;
}
if (javaType instanceof AnnotatedArrayType) {
return findConstituentAbstractTypes(((AnnotatedArrayType) javaType).getAnnotatedGenericComponentType(), buildContext);
}
if (javaType instanceof AnnotatedWildcardType || javaType instanceof AnnotatedTypeVariable) {
throw TypeMappingException.ambiguousType(javaType.getType());
}
return findAbstract(javaType, buildContext);
}
@Override
public String generateTypeName(AnnotatedType type, MessageBundle messageBundle) {
if (type instanceof AnnotatedParameterizedType) {
return generateParameterizedName((AnnotatedParameterizedType) type, messageBundle);
}
if (type instanceof AnnotatedArrayType) {
return generateArrayName((AnnotatedArrayType) type, messageBundle);
}
Class<Object> rawType = ClassUtils.getRawType(type.getType());
if (rawType.getEnclosingClass() != null && hierarchicalNames && isIncluded(rawType)) {
//TODO Use AnnotatedType#getAnnotatedOwnerType instead of annotate(rawType.getEnclosingClass()) once available
String enclosingName = generateTypeName(GenericTypeReflector.annotate(rawType.getEnclosingClass()), messageBundle);
return enclosingName + hierarchicalNameSeparator + generateBaseName(type, messageBundle);
}
return generateBaseName(type, messageBundle);
}
public static boolean containsTypeAnnotation(AnnotatedType type, Class<? extends Annotation> annotation) {
if (type.isAnnotationPresent(annotation)) {
return true;
}
if (type instanceof AnnotatedParameterizedType) {
AnnotatedParameterizedType parameterizedType = ((AnnotatedParameterizedType) type);
return Arrays.stream(parameterizedType.getAnnotatedActualTypeArguments())
.anyMatch(param -> containsTypeAnnotation(param, annotation));
}
if (type instanceof AnnotatedTypeVariable) {
AnnotatedTypeVariable variable = ((AnnotatedTypeVariable) type);
return Arrays.stream(variable.getAnnotatedBounds())
.anyMatch(bound -> containsTypeAnnotation(bound, annotation));
}
if (type instanceof AnnotatedWildcardType) {
AnnotatedWildcardType wildcard = ((AnnotatedWildcardType) type);
return Stream.concat(
Arrays.stream(wildcard.getAnnotatedLowerBounds()),
Arrays.stream(wildcard.getAnnotatedUpperBounds()))
.anyMatch(param -> containsTypeAnnotation(param, annotation));
}
return type instanceof AnnotatedArrayType && containsTypeAnnotation(((AnnotatedArrayType) type).getAnnotatedGenericComponentType(), annotation);
}
@SuppressWarnings("unchecked")
public static <T extends AnnotatedType> T transformType(T type, UnaryOperator<T> transformer) {
if (type instanceof AnnotatedArrayType) {
return (T) TypeFactory.arrayOf(transformer.apply((T) ((AnnotatedArrayType) type).getAnnotatedGenericComponentType()), type.getAnnotations());
}
if (type.getType() instanceof Class) {
return type;
}
if (type instanceof AnnotatedParameterizedType) {
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) type;
AnnotatedType[] arguments = Arrays.stream(parameterizedType.getAnnotatedActualTypeArguments())
.map(param -> transformer.apply((T) param))
.toArray(AnnotatedType[]::new);
return (T) TypeFactory.parameterizedAnnotatedClass(GenericTypeReflector.erase(type.getType()), type.getAnnotations(), arguments);
}
throw new IllegalArgumentException("Can not find the mappable type for: " + type.getType().getTypeName());
}
private Type getBareType(AnnotatedType annotatedType) {
final Type type = annotatedType.getType();
if (isArrayOfPrimitiveType(type)) {
return type;
}
if (annotatedType instanceof AnnotatedParameterizedType) {
final AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) annotatedType;
final ParameterizedType parameterizedType = (ParameterizedType) type;
return new JParameterizedType(
parameterizedType.getRawType(),
getTypes(annotatedParameterizedType.getAnnotatedActualTypeArguments()),
parameterizedType.getOwnerType());
}
if (annotatedType instanceof AnnotatedArrayType) {
final AnnotatedArrayType annotatedArrayType = (AnnotatedArrayType) annotatedType;
return new JGenericArrayType(getType(annotatedArrayType.getAnnotatedGenericComponentType()));
}
return type;
}
@Override
public AnnotatedType getSubstituteType(AnnotatedType original) {
AnnotatedType component = ((AnnotatedArrayType) original).getAnnotatedGenericComponentType();
Class<?> raw = ClassUtils.getRawType(component.getType());
if (raw.isPrimitive()) {
component = GenericTypeReflector.annotate(GenericTypeReflector.box(raw), component.getAnnotations());
}
return TypeFactory.parameterizedAnnotatedClass(List.class, original.getAnnotations(), component);
}
@Override
public boolean supports(AnnotatedElement element, AnnotatedType type) {
return !Scalars.isScalar(type.getType()) && type instanceof AnnotatedArrayType;
}
private AnnotatedType getElementType(AnnotatedType arrayType) {
return ((AnnotatedArrayType) arrayType).getAnnotatedGenericComponentType();
}
public static AnnotatedType unionize(AnnotatedType[] types, MessageBundle messageBundle) {
Objects.requireNonNull(types);
if (types.length < 2) {
if (types.length == 1 && ClassUtils.isSuperClass(Union.class, types[0])) {
return types[0];
}
throw new IllegalArgumentException(SINGLE_TYPE_UNION_ERROR);
}
AnnotatedType t1 = types[0];
if (stream(types).anyMatch(t -> t.isAnnotationPresent(GraphQLUnion.class))) {
if (stream(types).allMatch(t -> t.isAnnotationPresent(GraphQLUnion.class) && nameEquals(t, t1, messageBundle))) {
return of(types);
} else {
throw new IllegalArgumentException("All union members must be explicitly annotated: " + Arrays.toString(types));
}
}
if (stream(types).allMatch(t -> t instanceof AnnotatedParameterizedType)) {
AnnotatedParameterizedType p1 = (AnnotatedParameterizedType) t1;
AnnotatedParameterizedType[] pTypes = stream(types)
.map(t -> (AnnotatedParameterizedType) t)
.toArray(AnnotatedParameterizedType[]::new);
AnnotatedType[] params = new AnnotatedType[p1.getAnnotatedActualTypeArguments().length];
for (int i = 0; i < p1.getAnnotatedActualTypeArguments().length; i++) {
final int j = i;
params[i] = unionize(stream(pTypes)
.map(p -> p.getAnnotatedActualTypeArguments()[j])
.toArray(AnnotatedType[]::new), messageBundle);
}
Class<?> rawType = ((Class<?>) ((ParameterizedType) p1.getType()).getRawType());
return TypeFactory.parameterizedAnnotatedClass(rawType, ClassUtils.getAllAnnotations(stream(types)), params);
}
if (stream(types).allMatch(t -> t instanceof AnnotatedArrayType)) {
AnnotatedType[] components = stream(types)
.map(type -> ((AnnotatedArrayType) type).getAnnotatedGenericComponentType())
.toArray(AnnotatedType[]::new);
return TypeFactory.arrayOf(unionize(components, messageBundle), ClassUtils.getAllAnnotations(stream(types)));
}
if (stream(types).allMatch(t -> types[0].getType().equals(t.getType()))) {
return types[0];
}
throw new IllegalArgumentException("Types are incompatible and can not be unionized: ");
}
protected String generateArrayName(AnnotatedArrayType type, MessageBundle messageBundle) {
return generateTypeName(type.getAnnotatedGenericComponentType(), messageBundle) + "Array";
}
private static AnnotatedType getCommonSuperType(List<AnnotatedType> types, Set<String> seenTypeCombos, AnnotatedType fallback) {
if (types == null || types.isEmpty()) {
throw new IllegalArgumentException("At least one type must be provided");
}
if (types.size() == 1) {
return types.get(0);
}
Annotation[] mergedAnnotations = getMergedAnnotations(types.toArray(new AnnotatedType[0]));
if (types.stream().map(AnnotatedType::getType).allMatch(type -> type.equals(types.get(0).getType()))) {
return GenericTypeReflector.replaceAnnotations(types.get(0), mergedAnnotations);
}
List<Class<?>> classes = types.stream().map(AnnotatedType::getType).map(ClassUtils::getRawType).collect(Collectors.toList());
String typeNames = types.stream().map(type -> type.getType().getTypeName()).sorted().collect(Collectors.joining(","));
if (seenTypeCombos.contains(typeNames)) {
return fallbackOrException(fallback);
}
seenTypeCombos.add(typeNames);
//deal with arrays first as they are special
if (types.stream().allMatch(type -> type instanceof AnnotatedArrayType)) {
List<AnnotatedType> componentTypes = types.stream()
.map(type -> ((AnnotatedArrayType) type).getAnnotatedGenericComponentType())
.collect(Collectors.toList());
AnnotatedType componentType = getCommonSuperType(componentTypes, seenTypeCombos, fallback);
return TypeFactory.arrayOf(componentType, mergedAnnotations);
}
Class<?> commonRawSuperType = getCommonSuperTypes(classes).get(0);
if (classes.stream().noneMatch(ROOT_TYPES::contains) && ROOT_TYPES.contains(commonRawSuperType)) {
return fallbackOrException(fallback);
}
List<AnnotatedType> normalizedTypes = types.stream()
.map(type -> GenericTypeReflector.getExactSuperType(type, commonRawSuperType))
.collect(Collectors.toList());
if (normalizedTypes.stream().anyMatch(type -> isMissingTypeParameters(type.getType()))) {
throw new TypeMappingException("Automatic type inference failed because some of the types are missing generic type parameter(s).");
}
if (normalizedTypes.stream().allMatch(type -> type.getType() instanceof Class)) {
return annotate(commonRawSuperType, mergedAnnotations);
}
if (normalizedTypes.stream().allMatch(type -> type instanceof AnnotatedParameterizedType)) {
AnnotatedType[] parameters = Arrays.stream(commonRawSuperType.getTypeParameters())
.map(param -> normalizedTypes.stream().map(type -> GenericTypeReflector.getTypeParameter(type, param)).collect(Collectors.toList()))
.map(paramTypes -> getCommonSuperType(paramTypes, seenTypeCombos, fallback))
.toArray(AnnotatedType[]::new);
return TypeFactory.parameterizedAnnotatedClass(commonRawSuperType, mergedAnnotations, parameters);
}
return fallbackOrException(fallback);
}