Java Code Examples for java.lang.reflect.AnnotatedType#getAnnotation()

The following examples show how to use java.lang.reflect.AnnotatedType#getAnnotation() . 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   File: UnionTypeMapper.java    License: Apache License 2.0 6 votes vote down vote up
private List<AnnotatedType> getPossibleJavaTypes(AnnotatedType javaType, BuildContext buildContext) {
    GraphQLUnion annotation = javaType.getAnnotation(GraphQLUnion.class);
    List<AnnotatedType> possibleTypes = Collections.emptyList();
    if (annotation.possibleTypes().length > 0) {
        possibleTypes = Arrays.stream(annotation.possibleTypes())
                .map(type -> GenericTypeReflector.getExactSubType(javaType, type))
                .collect(Collectors.toList());
    }
    if (possibleTypes.isEmpty()) {
        try {
            possibleTypes = annotation.possibleTypeFactory().newInstance().getPossibleTypes();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalArgumentException(annotation.possibleTypeFactory().getName() +
                    " must have a public default constructor", e);
        }
    }
    if (possibleTypes.isEmpty()) {
        possibleTypes = buildContext.implDiscoveryStrategy.findImplementations(javaType, annotation.possibleTypeAutoDiscovery(), annotation.scanPackages(), buildContext);
    }
    if (possibleTypes.isEmpty()) {
        throw new TypeMappingException("No possible types found for union type " + javaType.getType().getTypeName());
    }
    return possibleTypes;
}
 
Example 2
@Override
public Directive buildClientDirective(AnnotatedType directiveType, DirectiveBuilderParams params) {
    InputFieldBuilderParams fieldBuilderParams = InputFieldBuilderParams.builder()
            .withType(directiveType)
            .withEnvironment(params.getEnvironment())
            .withConcreteSubTypes(params.getConcreteSubTypes())
            .build();
    List<DirectiveArgument> arguments = params.getInputFieldBuilders().getInputFields(fieldBuilderParams).stream()
            .map(inputField -> new DirectiveArgument(
                    inputField.getName(),
                    inputField.getDescription(),
                    inputField.getTypedElement(),
                    null,
                    inputField.getDefaultValue(),
                    null))
            .collect(Collectors.toList());

    TypeInfoGenerator infoGenerator = params.getEnvironment().typeInfoGenerator;
    MessageBundle messageBundle = params.getEnvironment().messageBundle;
    GraphQLDirective meta = directiveType.getAnnotation(GraphQLDirective.class);
    Introspection.DirectiveLocation[] locations = (meta != null && Utils.isArrayNotEmpty(meta.locations())) ? meta.locations() : GraphQLDirective.ALL_CLIENT;
    return new Directive(
            infoGenerator.generateDirectiveTypeName(directiveType, messageBundle),
            infoGenerator.generateDirectiveTypeDescription(directiveType, messageBundle), locations, arguments);
}
 
Example 3
Source Project: graphql-spqr   File: UnionTypeMapper.java    License: Apache License 2.0 5 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 = getPossibleJavaTypes(javaType, env.buildContext);
    final String name = env.buildContext.typeInfoGenerator.generateTypeName(javaType, env.buildContext.messageBundle);
    return toGraphQLUnion(name, annotation.description(), javaType, possibleJavaTypes, env);
}
 
Example 4
Source Project: graphql-spqr   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);
}