Java Code Examples for java.lang.reflect.AnnotatedParameterizedType

The following examples show how to use java.lang.reflect.AnnotatedParameterizedType. These examples are extracted from open source projects. 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 Project: graphql-spqr   Source File: AutoScanAbstractInputHandler.java    License: 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 2
Source Project: graphql-spqr   Source File: DefaultTypeInfoGenerator.java    License: 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 3
Source Project: graphql-spqr   Source File: DefaultTypeInfoGenerator.java    License: Apache License 2.0 6 votes vote down vote up
protected String generateParameterizedName(AnnotatedParameterizedType type, MessageBundle messageBundle) {
    ParameterizedType parameterizedType = (ParameterizedType) type.getType();
    Class<?> rawType = ClassUtils.getRawType(type.getType());
    StringBuilder genericName = new StringBuilder();
    if (parameterizedType.getOwnerType() != null && hierarchicalNames && isIncluded(rawType)) {
        //TODO Use AnnotatedParameterizedType#getAnnotatedOwnerType instead of annotate(pType.getOwnerType()) once available
        String enclosingName = generateTypeName(GenericTypeReflector.annotate(parameterizedType.getOwnerType()), messageBundle);
        genericName.append(enclosingName).append(hierarchicalNameSeparator);
    }

    String baseName = generateBaseName(type, messageBundle);
    genericName.append(baseName);
    Arrays.stream(type.getAnnotatedActualTypeArguments())
            .map(t -> generateTypeName(t, messageBundle))
            .forEach(argName -> genericName.append(genericTypeSeparator).append(argName));
    return genericName.toString();
}
 
Example 4
Source Project: graphql-spqr   Source File: ClassUtils.java    License: 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 Project: graphql-spqr   Source File: ClassUtils.java    License: 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 6
Source Project: typescript-generator   Source File: TypeParser.java    License: 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 7
private boolean cascadeValid(PropertyModel propertyModel, TypeContext typeContext) {
    PropertyItem parent = propertyModel.getParentPropertyItem();
    if (parent == null) return true;

    Valid validAnno = parent.getAnnotation(Valid.class);
    if (validAnno != null)
        return true;

    AnnotatedType annotatedType = null;
    if (parent.getField() != null) {
        annotatedType = parent.getField().getAnnotatedType();
    } else if (parent.getGetMethod() != null) {
        annotatedType = parent.getGetMethod().getAnnotatedReturnType();
    } else if (parent.getSetMethod() != null) {
        annotatedType = parent.getGetMethod().getAnnotatedParameterTypes()[0];
    }
    if (annotatedType != null) {
        if (annotatedType instanceof AnnotatedParameterizedType) {
            AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) annotatedType;
            if (annotatedParameterizedType.getType() instanceof ParameterizedType) {
                Class clazz = (Class) ((ParameterizedType) annotatedParameterizedType.getType()).getRawType();
                if (List.class.isAssignableFrom(clazz)) {
                    if (annotatedParameterizedType.getAnnotatedActualTypeArguments()[0].getAnnotation(Valid.class) != null)
                        return true;
                }
            }
        }
    }
    return false;
}
 
Example 8
Source Project: jsonschema-generator   Source File: FieldScope.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <A extends Annotation> A getContainerItemAnnotation(Class<A> annotationClass) {
    AnnotatedType annotatedType = this.getRawMember().getAnnotatedType();
    if (annotatedType instanceof AnnotatedParameterizedType) {
        AnnotatedType[] typeArguments = ((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments();
        if (typeArguments.length > 0) {
            return typeArguments[0].getAnnotation(annotationClass);
        }
    }
    return null;
}
 
Example 9
Source Project: jsonschema-generator   Source File: MethodScope.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <A extends Annotation> A getContainerItemAnnotation(Class<A> annotationClass) {
    AnnotatedType annotatedReturnType = this.getRawMember().getAnnotatedReturnType();
    if (annotatedReturnType instanceof AnnotatedParameterizedType) {
        AnnotatedType[] typeArguments = ((AnnotatedParameterizedType) annotatedReturnType).getAnnotatedActualTypeArguments();
        if (typeArguments.length > 0) {
            return typeArguments[0].getAnnotation(annotationClass);
        }
    }
    return null;
}
 
Example 10
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 11
Source Project: jdk8u60   Source File: RedefineAnnotations.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 12
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 13
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 14
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 15
public void checkAnnotations(AnnotatedType type, String expected) {
    String actual = Arrays.asList(((AnnotatedParameterizedType) type)
                                  .getAnnotations())
                                  .toString()
                                   + "," +
                    Arrays.asList(((AnnotatedParameterizedType) type)
                                   .getAnnotatedActualTypeArguments()[0].getAnnotations())
                                   .toString();

    if (!actual.equals(expected))
        throw new AssertionError("Unexpected annotations" + actual);
}
 
Example 16
Source Project: hottub   Source File: RedefineAnnotations.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void verifyMapFieldTypeAnnotations(Class c)
    throws NoSuchFieldException, NoSuchMethodException {

    Annotation anno;
    AnnotatedType atBase;
    AnnotatedType atParameter;
    atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();

    anno = atBase.getAnnotations()[0];
    verifyTestAnn(mapTA[0], anno, "map1");
    mapTA[0] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[0];
    anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[1], anno, "map2");
    mapTA[1] = anno;

    anno =
        ((AnnotatedWildcardType) atParameter).
        getAnnotatedUpperBounds()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[2], anno, "map3");
    mapTA[2] = anno;

    atParameter =
        ((AnnotatedParameterizedType) atBase).
        getAnnotatedActualTypeArguments()[1];
    anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
    verifyTestAnn(mapTA[3], anno, "map4");
    mapTA[3] = anno;

    anno =
        ((AnnotatedParameterizedType) atParameter).
        getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
    verifyTestAnn(mapTA[4], anno, "map5");
    mapTA[4] = anno;
}
 
Example 17
Source Project: graphql-spqr   Source File: InputFieldDiscoveryTest.java    License: Apache License 2.0 5 votes vote down vote up
private void assertTypesMerged(Set<InputField> fields) {
    Optional<InputField> field1 = fields.stream().filter(field -> field.getName().equals("field1")).findFirst();
    Optional<InputField> field2 = fields.stream().filter(field -> field.getName().equals("field2")).findFirst();
    Optional<InputField> field3 = fields.stream().filter(field -> field.getName().equals("field3")).findFirst();
    assertTrue(field1.isPresent() && field2.isPresent() && field3.isPresent());
    AnnotatedType type1 = field1.get().getTypedElement().getJavaType();
    assertTrue(type1.isAnnotationPresent(GraphQLNonNull.class) && type1.isAnnotationPresent(GraphQLId.class));
    AnnotatedType type2 = field2.get().getTypedElement().getJavaType();
    assertTrue(type2.isAnnotationPresent(GraphQLNonNull.class) && type2.isAnnotationPresent(GraphQLId.class));
    AnnotatedType type3 = field3.get().getTypedElement().getJavaType();
    assertTrue(type3.isAnnotationPresent(GraphQLNonNull.class));
    AnnotatedType type31 = ((AnnotatedParameterizedType) type3).getAnnotatedActualTypeArguments()[0];
    assertTrue(type31.isAnnotationPresent(GraphQLNonNull.class) && type31.isAnnotationPresent(GraphQLScalar.class));
}
 
Example 18
Source Project: vertx-codegen   Source File: TypeUse.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String rawName() {
  if (annotatedType instanceof AnnotatedParameterizedType) {
    return ((ParameterizedType)(annotatedType.getType())).getRawType().getTypeName();
  } else {
    return null;
  }
}
 
Example 19
Source Project: smallrye-graphql   Source File: MethodInfo.java    License: Apache License 2.0 4 votes vote down vote up
private AnnotatedType[] returnTypeAnnotations() {
    if (method.getAnnotatedReturnType() instanceof AnnotatedParameterizedType)
        return ((AnnotatedParameterizedType) method.getAnnotatedReturnType()).getAnnotatedActualTypeArguments();
    else
        return new AnnotatedType[0];
}
 
Example 20
Source Project: graphql-spqr   Source File: UnionInlineMapper.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLOutputType toGraphQLType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    GraphQLUnion annotation = javaType.getAnnotation(GraphQLUnion.class);
    List<AnnotatedType> possibleJavaTypes = Arrays.asList(((AnnotatedParameterizedType) javaType).getAnnotatedActualTypeArguments());
    return toGraphQLUnion(env.buildContext.interpolate(annotation.name()), env.buildContext.interpolate(annotation.description()), javaType, possibleJavaTypes, env);
}
 
Example 21
Source Project: graphql-spqr   Source File: Union.java    License: 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 22
Source Project: graphql-spqr   Source File: ClassUtils.java    License: 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);
}
 
Example 23
Source Project: vertx-codegen   Source File: TypeUse.java    License: Apache License 2.0 4 votes vote down vote up
public TypeInternal getArgAt(int index) {
  AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) annotatedType;
  return new ReflectType(annotatedParameterizedType.getAnnotatedActualTypeArguments()[index]);
}