Close

Java Reflection - Constructor.getAnnotatedReturnType() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Constructor

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Constructorjava.lang.reflect.ConstructorLogicBig

Method:

public AnnotatedType getAnnotatedReturnType()

Returns an AnnotatedType object (containing information about Java 8 type annotations) that represents the use of a type to specify the return type of the constructor.


Examples


Constructor without any type annotation

package com.logicbig.example.constructor;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.util.Arrays;

public class GetAnnotatedReturnTypeExample {
private static class Test {
Test() {}
}

public static void main(String... args) throws NoSuchMethodException {
System.out.println("Example Constructor:- Test(){..};");
Constructor<Test> m = Test.class.getDeclaredConstructor();
AnnotatedType annotatedReturnType = m.getAnnotatedReturnType();
printAnnotatedType(annotatedReturnType);
}

private static void printAnnotatedType(AnnotatedType annotatedType) {
System.out.println("Type: " + annotatedType.getType().getTypeName());
System.out.println("Annotations: " +
Arrays.toString(annotatedType.getAnnotations()));
System.out.println("Declared Annotations: " +
Arrays.toString(annotatedType.getDeclaredAnnotations()));
AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9
System.out.println("Annotated owner type: " + annotatedOwnerType);
System.out.println("AnnotatedType class: " + annotatedType.getClass().getName());
System.out.println("AnnotatedType class implementing interfaces: " +
Arrays.toString(annotatedType.getClass().getInterfaces()));
}
}

Output

Example Constructor:- Test(){..};
Type: com.logicbig.example.constructor.GetAnnotatedReturnTypeExample$Test
Annotations: []
Declared Annotations: []
Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2c9f444d
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




Constructor with return type annotation

package com.logicbig.example.constructor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.util.Arrays;

public class GetAnnotatedReturnTypeExample2 {
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface ReadOnly {}

private static class Test {
@ReadOnly Test() {}
}

public static void main(String... args) throws NoSuchMethodException {
System.out.println("Example Constructor:- @ReadOnly Test(){..};");
Constructor<Test> m = Test.class.getDeclaredConstructor();
AnnotatedType annotatedReturnType = m.getAnnotatedReturnType();
printAnnotatedType(annotatedReturnType);
}

private static void printAnnotatedType(AnnotatedType annotatedType) {
System.out.println("Type: " + annotatedType.getType().getTypeName());
System.out.println("Annotations: " +
Arrays.toString(annotatedType.getAnnotations()));
System.out.println("Declared Annotations: " +
Arrays.toString(annotatedType.getDeclaredAnnotations()));
AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9
System.out.println("Annotated owner type: " + annotatedOwnerType);
System.out.println("AnnotatedType class: " + annotatedType.getClass().getName());
System.out.println("AnnotatedType class implementing interfaces: " +
Arrays.toString(annotatedType.getClass().getInterfaces()));
}
}

Output

Example Constructor:- @ReadOnly Test(){..};
Type: com.logicbig.example.constructor.GetAnnotatedReturnTypeExample2$Test
Annotations: [@com.logicbig.example.constructor.GetAnnotatedReturnTypeExample2$ReadOnly()]
Declared Annotations: [@com.logicbig.example.constructor.GetAnnotatedReturnTypeExample2$ReadOnly()]
Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@41c23d81
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




See Also