java.lang.reflect.AnnotatedArrayType Java Examples

The following examples show how to use java.lang.reflect.AnnotatedArrayType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedefineAnnotations.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #2
Source File: TypeParser.java    From typescript-generator with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: ClassUtils.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #4
Source File: ClassUtils.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: AutoScanAbstractInputHandler.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: RedefineAnnotations.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #8
Source File: RedefineAnnotations.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #9
Source File: RedefineAnnotations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #10
Source File: RedefineAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #11
Source File: RedefineAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example #12
Source File: ArrayAdapter.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: ArrayAdapter.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(AnnotatedElement element, AnnotatedType type) {
    return !Scalars.isScalar(type.getType()) && type instanceof AnnotatedArrayType;
}
 
Example #14
Source File: ArrayAdapter.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private AnnotatedType getElementType(AnnotatedType arrayType) {
    return ((AnnotatedArrayType) arrayType).getAnnotatedGenericComponentType();
}
 
Example #15
Source File: Union.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
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: ");
}
 
Example #16
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
protected String generateArrayName(AnnotatedArrayType type, MessageBundle messageBundle) {
    return generateTypeName(type.getAnnotatedGenericComponentType(), messageBundle) + "Array";
}
 
Example #17
Source File: ClassUtils.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
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);
}