Thomas Darimont
Erfahrenes Mitglied
Hallo,
hier mal ein kleines Beispiel wie man mit Reflection unter Java 8 Annotations bei der Typ Verwendung auslesen kann:
Ausgabe:
Gruß Tom
hier mal ein kleines Beispiel wie man mit Reflection unter Java 8 Annotations bei der Typ Verwendung auslesen kann:
Java:
package de.tutorials.training;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Thomas Darimont
*/
public class TypeUseAnnotationReflectionExample {
public static void main(String[] args) {
Bar bar = new Foo();
Bubu bubu = bar.getClass().getSuperclass().getAnnotation(Bubu.class); //doesn't work since Bubu is not annotated on the type but rather at the type usage.
System.out.println(bubu);
bubu = bar.getClass().getAnnotatedSuperclass().getAnnotation(Bubu.class);
System.out.println(bubu);
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.TYPE})
static @interface Bubu {
String value() default "";
}
static class Bar {
}
static class Foo extends @Bubu("test") Bar {
}
}
Ausgabe:
Code:
null
@de.tutorials.training.TypeUseAnnotationReflectionExample$Bubu(value=test)
Gruß Tom