Close

Java Reflection - Class.getAnnotatedSuperclass() Examples

Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public AnnotatedType getAnnotatedSuperclass()

Returns an AnnotatedType object that represent the use of Java 8 type annotations on superclass declaration specified in 'extends' clause .


Examples


package com.logicbig.example.clazz;

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

public class GetAnnotatedSuperclassExample {

public static void main(String... args) {
AnnotatedType annotatedType = MyThread.class.getAnnotatedSuperclass();
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()));

}

private static class MyThread extends Thread {}
}

Output

Type: java.lang.Thread
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




package com.logicbig.example.clazz;

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.util.Arrays;

public class GetAnnotatedSuperclassExample2 {

public static void main(String... args) {
AnnotatedType annotatedType = MyThread.class.getAnnotatedSuperclass();
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()));

}

private static class MyThread extends @Daemon Thread {}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE})
private @interface Daemon {
}
}

Output

Type: java.lang.Thread
Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample2$Daemon()]
Declared Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample2$Daemon()]
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




package com.logicbig.example.clazz;

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

public class GetAnnotatedSuperclassExample3<T>
extends ArrayList<@GetAnnotatedSuperclassExample3.PrimeNumber T> {

public static void main(String... args) {
AnnotatedType annotatedType = GetAnnotatedSuperclassExample3.class.getAnnotatedSuperclass();
printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) {
System.out.println("-- casting to AnnotatedParameterizedType --");
System.out.println("-- AnnotatedParameterizedType#annotatedActualTypeArguments() --");
AnnotatedType[] annotatedActualTypeArguments =
((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments();
for (AnnotatedType annotatedActualTypeArgument : annotatedActualTypeArguments) {
System.out.println("-- annotatedActualTypeArgument --");
printAnnotatedType(annotatedActualTypeArgument);
}
}
}

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()));
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE})
public static @interface PrimeNumber {
}
}

Output

Type: java.util.ArrayList<T>
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType]
-- casting to AnnotatedParameterizedType --
-- AnnotatedParameterizedType#annotatedActualTypeArguments() --
-- annotatedActualTypeArgument --
Type: T
Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample3$PrimeNumber()]
Declared Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample3$PrimeNumber()]
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable]




package com.logicbig.example.clazz;

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

public class GetAnnotatedSuperclassExample4<K extends Number, V>
extends HashMap<@GetAnnotatedSuperclassExample4.PrimeNumber K, V> {

public static void main(String... args) {
AnnotatedType annotatedType = GetAnnotatedSuperclassExample4.class.getAnnotatedSuperclass();
printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) {
System.out.println("-- casting to AnnotatedParameterizedType --");
System.out.println("-- AnnotatedParameterizedType#annotatedActualTypeArguments() --");
AnnotatedType[] annotatedActualTypeArguments =
((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments();
for (AnnotatedType annotatedActualTypeArgument : annotatedActualTypeArguments) {
System.out.println("-- annotatedActualTypeArgument --");
printAnnotatedType(annotatedActualTypeArgument);
}
}
}

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()));
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE})
public static @interface PrimeNumber {}
}

Output

Type: java.util.HashMap<K, V>
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType]
-- casting to AnnotatedParameterizedType --
-- AnnotatedParameterizedType#annotatedActualTypeArguments() --
-- annotatedActualTypeArgument --
Type: K
Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample4$PrimeNumber()]
Declared Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample4$PrimeNumber()]
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable]
-- annotatedActualTypeArgument --
Type: V
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable]

package com.logicbig.example.clazz;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.AnnotatedTypeVariable;
import java.util.ArrayList;
import java.util.Arrays;

public class GetAnnotatedSuperclassExample5<T extends Number> extends
ArrayList<T @GetAnnotatedSuperclassExample5.PrimeNumber []> {

public static void main(String... args) {
AnnotatedType annotatedType = GetAnnotatedSuperclassExample5.class.getAnnotatedSuperclass();
printAnnotatedType(annotatedType);
if (annotatedType instanceof AnnotatedParameterizedType) {
System.out.println("-- casting to AnnotatedParameterizedType --");
System.out.println("-- AnnotatedParameterizedType#annotatedActualTypeArguments() --");
AnnotatedType[] annotatedActualTypeArguments =
((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments();
for (AnnotatedType annotatedActualTypeArgument : annotatedActualTypeArguments) {
System.out.println("-- annotatedActualTypeArgument --");
printAnnotatedType(annotatedActualTypeArgument);
if (annotatedActualTypeArgument instanceof AnnotatedArrayType) {
System.out.println("-- casting to AnnotatedArrayType --");
System.out.println("-- AnnotatedArrayType#getAnnotatedGenericComponentType() --");
AnnotatedType annotatedGenericComponentType =
((AnnotatedArrayType) annotatedActualTypeArgument)
.getAnnotatedGenericComponentType();
printAnnotatedType(annotatedGenericComponentType);
if (annotatedGenericComponentType instanceof AnnotatedTypeVariable) {
System.out.println("-- casting to AnnotatedTypeVariable --");
System.out.println("-- AnnotatedTypeVariable#getAnnotatedBounds() --");
AnnotatedType[] annotatedBounds =
((AnnotatedTypeVariable) annotatedGenericComponentType)
.getAnnotatedBounds();
for (AnnotatedType annotatedBound : annotatedBounds) {
System.out.println("-- annotatedBound --");
printAnnotatedType(annotatedBound);
}
}
}
}
}
}

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()));
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE})
public static @interface PrimeNumber {
}
}

Output

Type: java.util.ArrayList<T[]>
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType]
-- casting to AnnotatedParameterizedType --
-- AnnotatedParameterizedType#annotatedActualTypeArguments() --
-- annotatedActualTypeArgument --
Type: T[]
Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample5$PrimeNumber()]
Declared Annotations: [@com.logicbig.example.clazz.GetAnnotatedSuperclassExample5$PrimeNumber()]
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedArrayType]
-- casting to AnnotatedArrayType --
-- AnnotatedArrayType#getAnnotatedGenericComponentType() --
Type: T
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable]
-- casting to AnnotatedTypeVariable --
-- AnnotatedTypeVariable#getAnnotatedBounds() --
-- annotatedBound --
Type: java.lang.Number
Annotations: []
Declared Annotations: []
Annotated owner type: null
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




See Also