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]