Close

Java Reflection - Class.getAnnotatedInterfaces() Examples

Code Snippets 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[] getAnnotatedInterfaces()

Returns an array of AnnotatedType objects that represent the use of Java 8 type annotations on interfaces. The interfaces are either specified in 'implements..' clause (if this is a class) or 'extends .. ' clause (if this is an interface).


Examples


Without any type annotations:

package com.logicbig.example.clazz;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.Arrays;

public class GetAnnotatedInterfacesExample {

public static void main(String... args) {
for (AnnotatedType annotatedType : MyClass.class.getAnnotatedInterfaces()) {
Type type = annotatedType.getType();
System.out.println("Type :" + type);

Annotation[] annotations = annotatedType.getAnnotations();
System.out.println(Arrays.toString(annotations));

Annotation[] declaredAnnotations = annotatedType.getDeclaredAnnotations();
System.out.println(Arrays.toString(declaredAnnotations));
}
}

private static class MyClass implements Serializable {
}
}

Output

Type :interface java.io.Serializable
[]
[]




With a type annotation:

package com.logicbig.example.clazz;

import java.io.Serializable;
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.Arrays;

public class GetAnnotatedInterfacesExample2 {

public static void main(String... args) {
AnnotatedType[] ais = MyClass.class.getAnnotatedInterfaces();
for (AnnotatedType annotatedType : ais) {
Type type = annotatedType.getType();
System.out.println("Type :" + type);

System.out.println("-- type annotations --");
Annotation[] annotations = annotatedType.getAnnotations();
System.out.println(Arrays.toString(annotations));
Annotation[] declaredAnnotations = annotatedType.getDeclaredAnnotations();
System.out.println(Arrays.toString(declaredAnnotations));
}
}

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

private static class MyClass implements @FileSystem Serializable {
}
}

Output

Type :interface java.io.Serializable
-- type annotations --
[@com.logicbig.example.clazz.GetAnnotatedInterfacesExample2$FileSystem()]
[@com.logicbig.example.clazz.GetAnnotatedInterfacesExample2$FileSystem()]




See Also