JAVA-B3
Reflection API
Overview
Annotations
Basics
Marker-Annotationen
Annotationen mit Werten
Multiple Annotations
Type-Annotationen
Deklaration
Predefined Annotations
@Deprecated
@Override
@SuppressWarnings
@FunctionalInterface
Meta-Annotationen
@Retention
@Documented
@Target
@Inherited
@Repeatable
Pluggable-Type-System Annotations
Repeating-Annotations (Java 8)
@Repeatable
@Repeatable
@ExampleAuthor
Annotations FAQ
Annotation Example: AOP
AOP Aspect Oriented Programming mit Custom Annotations
AopRechnerInterface
AopRechnerImpl
AopRechnerInterface
AopCheckHatZugangInterceptor
AopCheckHatZugangAnnotation
AopZugangssystem
Predefined Interfaces
Iterable
RandomAccess
Serializable
Cloneable
INDEX
Pluggable-Type-System
@Repeatable| Overview
Repeating-Annotations sind Annotationen, welche wiederholt werden dürfen.
Typische Annotationen dieses Typs sind beispielsweise Annotationen, wo man drei Autoren eines Programms angeben möchte, die @Author Annotation aus Custom-Annotation aber als @Repeatable annotiert werden muss. Normalerweise wird jede Annotation 0- bis 1-mal eingesetzt.
Beispiel @Author
@Repeatable | Beispiel @Author
@interface ExampleAuthors ist hier ein Beispiel für eine
Annotation, welche mehrere Werte beinhalten kann und
deshalb eine ExampleAuthor[] value()
Methode haben muss.
Geliefert wird als Ergebnis wiederum eine @ExampleAuthor Annotation welche die Werte name und url hat. Default-Werte können auch angegeben werden.
package com.example.annotations;
import java.lang.annotation.*;
// Container für die Repeatable-Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ExampleAuthors {
ExampleAuthor[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(ExampleAuthors.class)
public @interface ExampleAuthor {
String name();
String url() default "";
}
package com.example.demo;
import com.example.annotations.ExampleAuthor;
@ExampleAuthor(name = "Manuel Mustermann", url = "https://manuel.example.com")
@ExampleAuthor(name = "Manuela Musterfrau", url = "https://manuela.example.com")
public class MyService {
// …
}
Das Auslesen erfolgt über Java-Reflection-API.
Class<MyService> cls = MyService.class;
ExampleAuthor[] authors = cls.getAnnotationsByType(ExampleAuthor.class);
for (ExampleAuthor a : authors) {
System.out.printf("Author: %s (%s)%n", a.name(), a.url());
}
Das Auslesen erfolgt über Java-Reflection-API.
Author: Manuel Mustermann (https://manuel.example.com)
Author: Manuela Musterfrau (https://manuela.example.com)
FootNotes
UIO3 Es ist einfacher als Du denkst.
Stelle noch heute Deine Anfrage.
