Java Code Examples for java.lang.reflect.Method#getAnnotatedReturnType()

The following examples show how to use java.lang.reflect.Method#getAnnotatedReturnType() . 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: typescript-generator   File: ModelParser.java    License: MIT License 6 votes vote down vote up
protected static PropertyMember wrapMember(TypeParser typeParser, Member propertyMember, AnnotationGetter annotationGetter,
        String propertyName, Class<?> sourceClass) {
    if (propertyMember instanceof Field) {
        final Field field = (Field) propertyMember;
        return new PropertyMember(field, typeParser.getFieldType(field), field.getAnnotatedType(), annotationGetter);
    }
    if (propertyMember instanceof Method) {
        final Method method = (Method) propertyMember;
        return new PropertyMember(method, typeParser.getMethodReturnType(method), method.getAnnotatedReturnType(), annotationGetter);
    }
    throw new RuntimeException(String.format(
            "Unexpected member type '%s' in property '%s' in class '%s'",
            propertyMember != null ? propertyMember.getClass().getName() : null,
            propertyName,
            sourceClass.getName()));
}
 
Example 2
Source Project: graphql-spqr   File: ClassUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the exact annotated return type of the method declared by the given type, with type variables resolved (if possible)
 *
 * @param method        The method whose return type is to be resolved
 * @param declaringType The declaring annotated type against which to resolve the return type
 * @return The resolved annotated return type
 */
public static AnnotatedType getReturnType(Method method, AnnotatedType declaringType) {
    AnnotatedType exactDeclaringType = GenericTypeReflector.getExactSuperType(capture(declaringType), method.getDeclaringClass());
    if (isMissingTypeParameters(exactDeclaringType.getType())) {
        return method.getAnnotatedReturnType();
    }
    return GenericTypeReflector.getReturnType(method, declaringType);
}
 
Example 3
Source Project: vertx-codegen   File: TypeUse.java    License: Apache License 2.0 5 votes vote down vote up
public TypeUse.TypeInternal forReturn(ProcessingEnvironment env, ExecutableElement methodElt) {
  Method methodRef = getMethod(env, methodElt);
  if (methodRef == null) {
    return null;
  }
  AnnotatedType annotated = methodRef.getAnnotatedReturnType();
  return new ReflectType(annotated);
}
 
Example 4
Source Project: immutables   File: ValuesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void java8TypeAnnotation() throws Exception {
  Method method = ImmutableHasTypeAnnotation.class.getMethod("str");
  AnnotatedType returnType = method.getAnnotatedReturnType();
  check(returnType.getAnnotation(TypeA.class)).notNull();
  check(returnType.getAnnotation(TypeB.class)).notNull();
}
 
Example 5
Source Project: auto   File: MemoizedTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void nullableWithTypeAnnotationHasAnnotation() throws ReflectiveOperationException {
  Method nullable =
      AutoValue_MemoizedTest_Value.class.getDeclaredMethod("nullableWithTypeAnnotation");
  AnnotatedType returnType = nullable.getAnnotatedReturnType();
  assertThat(returnType.isAnnotationPresent(
                 org.checkerframework.checker.nullness.qual.Nullable.class))
      .isTrue();
}
 
Example 6
Source Project: Java-Coding-Problems   File: Main.java    License: MIT License 4 votes vote down vote up
public static void main(String[] args) throws ReflectiveOperationException {

        Class<Melon> clazz = Melon.class;

        System.out.println("Inspecting package annotations: ");
        Annotation[] pckgAnnotations = clazz.getPackage().getAnnotations();
        System.out.println("Package annotations: " + Arrays.toString(pckgAnnotations));

        System.out.println("\nInspecting class annotations: ");
        Annotation[] clazzAnnotations = clazz.getAnnotations();
        System.out.println("Class annotations: " + Arrays.toString(clazzAnnotations));
        Fruit fruitAnnotation = (Fruit) clazzAnnotations[0];
        System.out.println("@Fruit name: " + fruitAnnotation.name());
        System.out.println("@Fruit value: " + fruitAnnotation.value());

        System.out.println("\nInspecting methods annotations: ");
        Method methodEat = clazz.getDeclaredMethod("eat");
        Annotation[] methodAnnotations = methodEat.getDeclaredAnnotations();
        System.out.println("Method annotations: " + Arrays.toString(methodAnnotations));
        Ripe ripeAnnotation = (Ripe) methodAnnotations[0];
        System.out.println("@Ripe value: " + ripeAnnotation.value());

        System.out.println("\nInspecting annotations of the thrown exceptions: ");
        AnnotatedType[] exceptionsTypes = methodEat.getAnnotatedExceptionTypes();
        System.out.println("Exceptions types: " + Arrays.toString(exceptionsTypes));
        System.out.println("First exception type: " + exceptionsTypes[0].getType());
        System.out.println("Annotations of the first exception type: "
                + Arrays.toString(exceptionsTypes[0].getAnnotations()));

        System.out.println("\nInspecting annotations of the return type");
        Method methodSeeds = clazz.getDeclaredMethod("seeds");
        AnnotatedType returnType = methodSeeds.getAnnotatedReturnType();
        System.out.println("Return type: " + returnType.getType().getTypeName());
        System.out.println("Annotations of the return type: "
                + Arrays.toString(returnType.getAnnotations()));

        System.out.println("\nInspecting annotations of the method's parameters: ");
        Method methodSlice = clazz.getDeclaredMethod("slice", int.class);
        Annotation[][] paramAnnotations = methodSlice.getParameterAnnotations();
        Class<?>[] parameterTypes = methodSlice.getParameterTypes();

        int i = 0;
        for (Annotation[] annotations : paramAnnotations) {
            Class parameterType = parameterTypes[i++];

            System.out.println("Parameter type: " + parameterType.getName());
            for (Annotation annotation : annotations) {
                System.out.println("Annotation: " + annotation);
                System.out.println("Annotation name: "
                        + annotation.annotationType().getSimpleName());
            }
        }

        System.out.println("\nInspecting annotations of fields: ");
        Field weightField = clazz.getDeclaredField("weight");
        Annotation[] fieldAnnotations = weightField.getDeclaredAnnotations();
        Unit unitFieldAnnotation = (Unit) fieldAnnotations[0];
        System.out.println("@Unit value: " + unitFieldAnnotation.value());

        System.out.println("\nInspecting annotations of superclass: ");
        AnnotatedType superclassType = clazz.getAnnotatedSuperclass();
        System.out.println("Superclass type: " + superclassType.getType().getTypeName());
        System.out.println("Annotations: " + Arrays.toString(superclassType.getDeclaredAnnotations()));
        System.out.println("@Family annotation present: " + superclassType.isAnnotationPresent(Family.class));

        System.out.println("\nInspecting annotations of interfaces: ");
        AnnotatedType[] interfacesTypes = clazz.getAnnotatedInterfaces();
        System.out.println("Interfaces types: " + Arrays.toString(interfacesTypes));
        System.out.println("First interface type: " + interfacesTypes[0].getType());
        System.out.println("Annotations of the first exception type: "
                + Arrays.toString(interfacesTypes[0].getAnnotations()));

        System.out.println("\nGet annotations by type:");
        Fruit[] clazzFruitAnnotations = clazz.getAnnotationsByType(Fruit.class);
        for (Fruit clazzFruitAnnotation : clazzFruitAnnotations) {
            System.out.println("Fruit annotation name: " + clazzFruitAnnotation.name());
            System.out.println("Fruit annotation value: " + clazzFruitAnnotation.value());
        }

        System.out.println("\nGet a declared annotation:");
        Ripe methodRipeAnnotation = methodEat.getDeclaredAnnotation(Ripe.class);
        System.out.println("Shape annotation value: " + methodRipeAnnotation.value());
    }
 
Example 7
static OutputDefinition instanceOf(Method method, Class<?> outputClass, MessageCodec socketCodec) {
    if (!CommandDefinition.checkType(method.getGenericReturnType())) {
        throw new CommunicationDefinitionException("指令的参数与返回不能有擦拭类型与通配符类型");
    }
    int modifier = outputClass.getModifiers();
    if (!outputClass.isPrimitive() && Modifier.isAbstract(modifier)) {
        throw new CommunicationDefinitionException("指令的参数与返回不能为抽象");
    }
    if (Modifier.isInterface(modifier)) {
        throw new CommunicationDefinitionException("指令的参数与返回不能为接口");
    }

    OutputDefinition instance = new OutputDefinition();
    instance.contentFormat = MessageFormat.toByte(socketCodec.outputFormat(), socketCodec.outputZip());
    instance.contentType = outputClass;
    AnnotatedType annotatedType = method.getAnnotatedReturnType();
    Annotation[] annotations = annotatedType.getAnnotations();
    boolean hasBodyVariable = false;
    CommandVariable variable = null;
    for (Annotation annotation : annotations) {
        if (annotation instanceof CommandVariable) {
            if (variable != null) {
                throw new CommunicationDefinitionException();
            }
            variable = (CommandVariable) annotation;

            if (VariableType.MESSAGE_BODY.equals(variable.type())) {
                hasBodyVariable = true;
            }
        }
    }
    if (variable != null) {
        instance.outputVariable = VariableDefinition.instanceOf(annotatedType.getType(), variable, null);
    }

    if (instance.outputVariable == null && !(outputClass.equals(void.class) || outputClass.equals(Void.class))) {
        throw new CommunicationDefinitionException();
    }
    if (instance.outputVariable != null) {
        if (!hasBodyVariable && !(outputClass.equals(void.class) || outputClass.equals(Void.class))) {
            throw new CommunicationDefinitionException();
        }

        if (hasBodyVariable && !StringUtility.isBlank(instance.outputVariable.getVariable().property()) && outputClass.equals(void.class)) {
            throw new CommunicationDefinitionException();
        }
    }

    return instance;
}