diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/html/k100277.html | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/html/k100277.html')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/html/k100277.html | 712 |
1 files changed, 712 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/html/k100277.html b/Master/Reference Architectures and Patterns/hjp5/html/k100277.html new file mode 100644 index 0000000..7140acc --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/html/k100277.html @@ -0,0 +1,712 @@ +<html>
+<head>
+<title>
+Handbuch der Java-Programmierung, 5. Auflage
+</title>
+</head>
+<body>
+<a name="startofbody"></a>
+<script language="JavaScript" src="hjp4lib.js">
+</script>
+<script language="JavaScript">
+installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100271.html;106,k100276.html;107,k100278.html;108,k100279.html");
+</script>
+<table border=0 cellpadding=0 cellspacing=1 width="100%">
+<tr bgcolor="#EEFFCC">
+<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
+<td align="right">Handbuch der Java-Programmierung, 5. Auflage
+<tr bgcolor="#EEFFCC">
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100271.html"> << </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100276.html"> < </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100278.html"> > </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100279.html"> >> </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
+<td align="right">Kapitel 43 - Reflection
+</table>
+<hr>
+
+
+<!-- Section -->
+<a name="sectionannotationen"></a>
+<h2>43.6 <a name="ixa103254">Annotationen</a> oder <a name="ixa103255">Metainformationen</a>
+im Javacode </h2>
+<hr>
+<ul>
+<li><a href="k100277.html#sectionannotationen">43.6 Annotationen oder Metainformationen
+im Javacode</a>
+<ul>
+<li><a href="k100277.html#sectlevel3id043006001">43.6.1 Metainformationen</a>
+<li><a href="k100277.html#sectlevel3id043006002">43.6.2 Eine einfache Annotation</a>
+<ul>
+<li><a href="k100277.html#sectlevel4id043006002001">Die Annotationsklasse</a>
+<li><a href="k100277.html#sectlevel4id043006002002">Verwendung von Annotationen</a>
+</ul>
+<li><a href="k100277.html#sectlevel3id043006003">43.6.3 Annotationen mit einem Wert</a>
+<li><a href="k100277.html#sectlevel3id043006004">43.6.4 Beliebige Schlüssel-Wert-Paare in Annotationen</a>
+<ul>
+<li><a href="k100277.html#sectlevel4id043006004001">Unterstützte Datentypen</a>
+</ul>
+<li><a href="k100277.html#sectlevel3id043006005">43.6.5 Standardwerte für Attribute</a>
+<li><a href="k100277.html#sectlevel3id043006006">43.6.6 Einschränken von Annotationen</a>
+<ul>
+<li><a href="k100277.html#sectlevel4id043006006001">Einschränken der Verwendbarkeit</a>
+<li><a href="k100277.html#sectlevel4id043006006002">Einschränken der Sichtbarkeit</a>
+<li><a href="k100277.html#sectlevel4id043006006003">Dokumentieren der Annotation und Vererbung</a>
+</ul>
+<li><a href="k100277.html#sectlevel3id043006007">43.6.7 Auslesen von Annotationen</a>
+</ul>
+</ul>
+<hr>
+
+
+<!-- Section -->
+<a name="sectlevel3id043006001"></a>
+<h3>43.6.1 Metainformationen </h3>
+
+<p>
+Was sind eigentlich Metainformationen und wofür sind sie gut?
+Nun, Metainformationen sind einfach Informationen über Informationen
+und können dafür verwendet werden, zusätzliches Wissen
+abzulegen. In unserem Fall handelt es sich dabei um zusätzliches
+Wissen über Javaprogramme, das zusammen mit dem Quellcode abgelegt
+werden kann, die Arbeitsweise eines Programms jedoch nicht beeinflusst.
+
+<p>
+An verschiedenen Stellen sind wir sogar schon mit Annotationen in
+Berührung gekommen. So sind beispielsweise die zusätzlichen
+Tags <a href="index_0.html#ixb100225"><font color=#000080><tt>@param</tt></font></a>
+oder <a href="index_0.html#ixb100226"><font color=#000080><tt>@return</tt></font></a>
+für das JavaDoc-Werkzeug, (steht in <a href="k100322.html#sectiondoku">Abschnitt 51.5.3</a>)
+nichts anderes als zusätzliche Informationen, die dazu verwendet
+werden können, die Schnittstellen eines Programms besser zu dokumentieren.
+
+<p>
+Im Kapitel über Persistenz werden wir außerdem sehen, dass
+Annotationen häufig dazu verwendet werden, Konfigurationsinformationen
+für zusätzlichen Frameworks zusammen mit dem betreffenden
+Code abzulegen und uns so weitestgehend von der Last zusätzlicher
+Konfigurationsdateien zu befreien. Es ist jedoch ebenso leicht eigene
+Annotationen zu entwickeln und ihre Informationen anschließend
+über das Reflection API auswerten. Wir werden dies anhand einer
+eigenen Annotation demonstrieren, die es gestattet, Revisionsinformationen
+im Quellcode zu hinterlegen. Diese geben Auskunft darüber , wann
+und von wem eine Klasse oder eine Methode zuletzt verändert worden
+sind.
+
+<!-- Section -->
+
+<a name="sectlevel3id043006002"></a>
+<h3>43.6.2 Eine einfache Annotation </h3>
+
+<p>
+Bevor wir die Revisionsinformationen in unsere Klassen integrieren
+und über die Reflection API auswerten können, müssen
+wir sie zunächst definieren.
+
+<!-- Section -->
+
+<a name="sectlevel4id043006002001"></a>
+<h4>Die Annotationsklasse </h4>
+
+<p>
+Mit Annotationen verhält es sich ähnlich wie mit einem Interface,
+das in gewisser Weise auch zusätzliche Informationen über
+eine Klasse zur Verfügung stellt. So ist es kaum verwunderlich,
+dass eine Annotation ganz ähnlich definiert wird:
+<a name="revision1"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#00AA00">/**
+<font color="#555555">002 </font> * Eine einfache Annotation
+<font color="#555555">003 </font> */</font>
+<font color="#555555">004 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.10: Eine einfache Annotation</i></p>
+
+<p>
+Bis auf das vorangestellte <font color="#000077"><tt>@</tt></font>-Zeichen
+unterscheidet sich die Definition einer Annotation also nicht von
+der eines normalen Interfaces, wie es in <a href="k100057.html#kapiteloop3">Kapitel 9</a>
+beschrieben wird. Im Gegensatz zu reinen Interfaces kann eine Annotation
+allerdings nicht nur für Klassen verwendet werden.
+
+<!-- Section -->
+
+<a name="sectlevel4id043006002002"></a>
+<h4>Verwendung von Annotationen </h4>
+
+<p>
+Annotationen dienen dazu, zusätzliche Informationen im Quellcode
+zu hinterlegen. Dabei können folgende Elemente annotiert werden:
+<ul>
+<li>Pakete
+<li>Klassen
+<li>Konstruktoren
+<li>Felder
+<li>Methoden
+<li>Lokale Variablen
+<li>Parameter
+<li>Annotationen
+</ul>
+
+<p>
+Da nicht alle Annotationen bei allen Elementen Sinn machen, werden
+wir später sehen, wie wir diese Liste einschränken können.
+Um nun beispielsweise die in <a href="k100275.html#sectionrefmember">Abschnitt 43.4</a>
+beschriebene Klasse <font color="#000077"><tt>PrintableObject</tt></font>
+und die von dieser definierte Methode <font color="#000077"><tt>toString</tt></font>
+mit Zusatzinformationen zu versehen schreiben wir ihren Namen dieser
+einfach vor die Liste der vorhandenen Modifier:
+<a name="annotation1"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.reflect.*;
+<font color="#555555">002 </font>
+<font color="#555555">003 </font>@Revision <font color="#0000AA">public</font> <font color="#0000AA">class</font> PrintableObject
+<font color="#555555">004 </font>{
+<font color="#555555">005 </font> @Revision <font color="#0000AA">public</font> String toString()
+<font color="#555555">006 </font> {
+<font color="#555555">007 </font> ...
+<font color="#555555">008 </font> }
+<font color="#555555">009 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.11: Verwendung der Annotation</i></p>
+
+<p>
+Geschafft! Sie können Ihre Annotationen nun genau wie die anderen
+Modifier verwenden und einfach vor das zu annotierende Element schreiben.
+Schon ist der Code mit den gewünschten Zusatzinformationen angereichert.
+
+<p>
+Der eine oder andere Leser könnte jetzt anmerken, dass die Klasse
+zwar nun annotiert ist, jedoch noch keine wirklichen Zusatzinformationen
+enthält. Zwar sind ist die Klasse <font color="#000077"><tt>PrintableObject</tt></font>
+und die Methode <font color="#000077"><tt>toString</tt></font> annotiert,
+allerdings wissen wir nicht vom wem und wann. Diese Informationen
+werden wir nun ergänzen.
+
+<!-- Section -->
+
+<a name="sectlevel3id043006003"></a>
+<h3>43.6.3 Annotationen mit einem Wert </h3>
+
+<p>
+Um tatsächlich zusätzliche Informationen ablegen zu können,
+müssen wir die Annotation nun gewissermaßen mit einer Variablen
+ausstatten. Dazu erweitern wir die Definition wie folgt:
+<a name="revision2"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#00AA00">/**
+<font color="#555555">002 </font> * Eine Annotation mit einer Variablen
+<font color="#555555">003 </font> */</font>
+<font color="#555555">004 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font> String value();
+<font color="#555555">007 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.12: Annotation mit einer Variablen</i></p>
+
+<p>
+Die Annotation besitzt nun die Variable <font color="#000077"><tt>value</tt></font>,
+die die eigentliche Information aufnehmen kann und im Gegenzug auch
+belegt werden muss. Der Wert der Variablen <font color="#000077"><tt>value</tt></font>
+wird bei der Verwendung der Annotation in der Klasse <font color="#000077"><tt>PrintableObject</tt></font>
+gesetzt.
+<a name="annotation2"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.reflect.*;
+<font color="#555555">002 </font>
+<font color="#555555">003 </font>@Revision(<font color="#0000FF">"Wurde zuerst geändert"</font>)
+<font color="#555555">004 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> PrintableObject
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font> @Revision(<font color="#0000FF">"Wurde anschließend geändert"</font>)
+<font color="#555555">007 </font> <font color="#0000AA">public</font> String toString()
+<font color="#555555">008 </font> {
+<font color="#555555">009 </font> ...
+<font color="#555555">010 </font> }
+<font color="#555555">011 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.13: Zuweisen von annotierten Werten </i></p>
+
+<p>
+Die Annotationen sind zwar nun jeweils eine Zeile nach oben gerückt,
+aber für den Compiler steht sie immer noch in der Liest der Modifier.
+Wir können Javaklassen prinzipiell auch als Einzeiler formulieren.
+Um die Übersicht zu wahren, ist es aber von Vorteil, lange Annotationen
+in eine separate Zeile zu stellen.
+
+<p>
+Wie Sie sehen, sind nun beide Annotationen mit unterschiedlichen Werten
+ausgestattet, die wir später auch auswerten können.
+
+<!-- Section -->
+
+<a name="sectlevel3id043006004"></a>
+<h3>43.6.4 Beliebige Schlüssel-Wert-Paare in Annotationen </h3>
+
+<p>
+Das Element <font color="#000077"><tt>value</tt></font>, welches in
+den vorangegangenen Listings den Wert aufnimmt ist zwar praktisch,
+aber um detailliertere Informationen ablegen zu können, müssten
+wir nun weitere Annotationen definieren, was in einem Wust von zusätzlichen
+»Modifizierern« enden würde. Um dies zu vermeiden und
+zusammengehörende Informationen in einer Annotation zusammenzufassen,
+können diese beliebig viele Informationen aufnehmen.
+
+<p>
+Der Schlüssel <font color="#000077"><tt>value</tt></font> stellt
+dabei nur einen Spezialfall für ein Standard-Attribut dar. Wenn
+wir den Namen des Attributs nicht angeben weißt Java den Wert
+der Annotation dem Attribut <font color="#000077"><tt>value</tt></font>
+zu. Alternativ wäre auch folgende Schreibweise möglich gewesen:
+<a name="annotation3"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.reflect.*;
+<font color="#555555">002 </font>
+<font color="#555555">003 </font>@Revision(value = <font color="#0000FF">"Wurde zuerst geändert"</font>)
+<font color="#555555">004 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> PrintableObject
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font> @Revision(value = <font color="#0000FF">"Wurde anschließend geändert"</font>)
+<font color="#555555">007 </font> <font color="#0000AA">public</font> String toString()
+<font color="#555555">008 </font> {
+<font color="#555555">009 </font> ...
+<font color="#555555">010 </font> }
+<font color="#555555">011 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.14: Zuweisen von annotierten Werten </i></p>
+
+<p>
+Nachdem wir die einzelnen Attribute einer Annotation nun genau adressieren
+können, verfeinern wir diese folgendermaßen:
+<a name="revision3"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#00AA00">/**
+<font color="#555555">002 </font> * Annotation mit mehreren Variablen
+<font color="#555555">003 </font> */</font>
+<font color="#555555">004 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font> <font color="#006699">int</font> id();
+<font color="#555555">007 </font> String name();
+<font color="#555555">008 </font> String vorname();
+<font color="#555555">009 </font> String notizen();
+<font color="#555555">010 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.15: Komplexe Annotation</i></p>
+
+<p>
+Das unspezifische Attribut <font color="#000077"><tt>value</tt></font>
+ist nun ein Reihe von detaillierten Einzelattributen gewichen, die
+natürlich auch gefüllt werden möchten:
+<a name="annotation4"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.reflect.*;
+<font color="#555555">002 </font>
+<font color="#555555">003 </font>@Revision( id = 1, name = <font color="#0000FF">"Krüger"</font>, vorname = <font color="#0000FF">"Guido"</font>,
+<font color="#555555">004 </font> notizen = <font color="#0000FF">"Klasse erstellt"</font>)
+<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> PrintableObject
+<font color="#555555">006 </font>{
+<font color="#555555">007 </font> @Revision( id = 2, name = <font color="#0000FF">"Stark"</font>, vorname = <font color="#0000FF">"Thomas"</font>,
+<font color="#555555">008 </font> notizen = <font color="#0000FF">"Methode hinzugefügt"</font>)
+<font color="#555555">009 </font> <font color="#0000AA">public</font> String toString()
+<font color="#555555">010 </font> {
+<font color="#555555">011 </font> ...
+<font color="#555555">012 </font> }
+<font color="#555555">013 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.16: Zuweisen von annotierten Werten </i></p>
+
+<p>
+Dies sieht doch schon nach brauchbaren Informationen aus und bringt
+uns zu der Frage, welche Datentypen Annotations-Attribute besitzen
+dürfen.
+
+<!-- Section -->
+
+<a name="sectlevel4id043006004001"></a>
+<h4>Unterstützte Datentypen </h4>
+
+<p>
+Die Attribute einer Annotation dürfen folgende Typen besitzen:
+<ul>
+<li>Primitive Datentypen wie <font color="#000077"><tt>byte</tt></font>,
+<font color="#000077"><tt>short</tt></font>, <font color="#000077"><tt>int</tt></font>,
+<font color="#000077"><tt>long</tt></font>, <font color="#000077"><tt>float</tt></font>,
+<font color="#000077"><tt>double</tt></font> und <font color="#000077"><tt>boolean</tt></font>,
+jedoch nicht deren Wrapperklassen
+<li><font color="#000077"><tt>String</tt></font>, als einzigen Referenztypen
+<li><font color="#000077"><tt>Enum</tt></font>-Aufzählungstypen
+<li><font color="#000077"><tt>Class</tt></font>
+<li>Andere Annotationen
+<li>Eindimensionale Felder der hier aufgezählten Typen
+</ul>
+
+<p>
+Die Liste ist auf diese Typen beschränkt, weil der Compiler mit
+ihnen die nötigen Typüberprüfungen vornehmen kann.
+
+
+<!-- Section -->
+<a name="sectlevel3id043006005"></a>
+<h3>43.6.5 Standardwerte für Attribute </h3>
+
+<p>
+Die Spezifikation für Annotation schreibt vor, dass alle definierten
+Attribute einer Annotation auch verwendet werden müssen. Um dennoch
+optionale Attribute definieren zu können, gestattet es die Spezifikation,
+Standardwerte festzulegen. Wir demonstrieren dies, indem wir die Notizen
+der Annotationen nun mit einem leeren <font color="#000077"><tt>String</tt></font>
+vorbelegen.
+<a name="revisio4"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#00AA00">/**
+<font color="#555555">002 </font> * Annotation mit mehreren Variablen
+<font color="#555555">003 </font> */</font>
+<font color="#555555">004 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">005 </font>{
+<font color="#555555">006 </font> <font color="#006699">int</font> id();
+<font color="#555555">007 </font> String name();
+<font color="#555555">008 </font> String vorname();
+<font color="#555555">009 </font> String notizen() <font color="#0000AA">default</font> <font color="#0000FF">""</font>;
+<font color="#555555">010 </font>}</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.17: Komplexe Annotation</i></p>
+
+<p>
+Nun ist die Angabe des Attributes <font color="#000077"><tt>notizen</tt></font>
+bei der Verwendung von Annotationen optional, da der Wert bereits
+vorinitialisiert ist. Gegebenenfalls wird der Default-Wert natürlich
+mit dem jeweils Angegebenen überschrieben.
+
+<!-- Section -->
+
+<a name="sectlevel3id043006006"></a>
+<h3>43.6.6 Einschränken von Annotationen </h3>
+
+<p>
+Bisher haben wir noch keinerlei Einschränkungen für unsere
+Annotation definiert und können diese deshalb sowohl für
+Pakete wie auch für lokale Variablen verwenden. Da jedoch nicht
+jede Annotation für jedes Element sinnvoll ist, können wir
+ihre Verwendung bei Bedarf einschränken. Und warum sollten wir
+hierfür etwas anderes verwenden als wiederum eine Annotation.
+
+
+<!-- Section -->
+<a name="sectlevel4id043006006001"></a>
+<h4>Einschränken der Verwendbarkeit </h4>
+
+<p>
+Über die Annotation <a href="index_0.html#ixb102392"><font color=#000080><tt>@Target</tt></font></a>
+können wir die Verwendbarkeit einer Annotationen auf bestimmte
+Elemente einschränken. Mögliche Werte sind:
+<ul>
+<li>PACKAGE
+<li>TYPE
+<li>CONSTRUCTOR
+<li>FIELD
+<li>METHOD
+<li>LOCAL_VARIABLE
+<li>PARAMETER
+<li>ANNOTATION_TYPE
+</ul>
+
+<p>
+Da wir uns am Anfang des Kapitels entschlossen haben die Annotation
+auf Klassen und Methoden zu beschränken, ergänzen wir das
+Listing der <font color="#000077"><tt>Revision</tt></font> ein weiteres
+Mal:
+<a name="revision4"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.annotation.Target;
+<font color="#555555">002 </font><font color="#0000AA">import</font> java.lang.annotation.ElementType;
+<font color="#555555">003 </font>
+<font color="#555555">004 </font><font color="#00AA00">// Diese Annotation ist auf Klassen und Methoden beschränkt</font>
+<font color="#555555">005 </font>@Target({ElementType.TYPE, ElementType.METHOD})
+<font color="#555555">006 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">007 </font>{
+<font color="#555555">008 </font> <font color="#006699">int</font> id();
+<font color="#555555">009 </font> String name();
+<font color="#555555">010 </font> String vorname();
+<font color="#555555">011 </font> String notizen() <font color="#0000AA">default</font> <font color="#0000FF">""</font>;
+<font color="#555555">012 </font>}
+</pre>
+</font>
+</td>
+</tr>
+</table>
+<i>
+Listing 43.18: Einschränken der Verwendbarkeit</i></p>
+
+<p>
+An diesem Beispiel können Sie außerdem die Verwendung von
+Arrays und Aufzählungen (Enum) als Attribut der Annotation <a href="index_t.html#ixb102393"><font color=#000080><tt>Target</tt></font></a>
+nachvollziehen. Die Variable <font color="#000077"><tt>value</tt></font>
+dieser Annotation enthält ein Array aus Aufzählungsobjekten,
+deren Werte in einer geschweifen Klammer und durch Kommata getrennt
+definiert werden. Wenn Sie das <font color="#000077"><tt>Target</tt></font>
+nicht spezifizieren, kann die Annotation universell verwendet werden.
+
+<!-- Section -->
+
+<a name="sectlevel4id043006006002"></a>
+<h4>Einschränken der Sichtbarkeit </h4>
+
+<p>
+Die Verwendungsmöglichkeiten von Annotationen reichen von Zusatzinformationen
+für Dokumentationstools bis hin zu Konfigurationsdaten, die das
+Laufzeitverhalten der Java Virtual Machine beeinflussen können.
+Um nicht alle - möglicherweise - unnötigen Metainformationen
+bereitstellen zu müssen, lassen sich diese über die Annotation
+<a href="index_0.html#ixb102394"><font color=#000080><tt>@Retention</tt></font></a>
+in 3 Kategorien einteilen: <a name="tableid043002"></a>
+
+<p>
+<table cols=2 border width=66%>
+
+<tr>
+<td valign=top align=left width=33%><b>Attribut </b></td>
+<td valign=top align=left width=66%><b>Verwendung</b></td></tr>
+<tr>
+<td valign=top align=left><a href="index_s.html#ixb102395"><font color=#000080><tt>SOURCE</tt></font></a>
+</td>
+<td valign=top align=left>Diese Informationen sind nur Bestandteil
+der Sourcedatei und werden nicht in die Klassen einkompiliert</td></tr>
+<tr>
+<td valign=top align=left><a href="index_c.html#ixb102396"><font color=#000080><tt>CLASS</tt></font></a>
+</td>
+<td valign=top align=left>Diese Informationen werden vom Compiler
+in die Classdatei integriert, stehen aber nicht zur Laufzeit zur Verfügung</td></tr>
+<tr>
+<td valign=top align=left><a href="index_r.html#ixb102397"><font color=#000080><tt>RUNTIME</tt></font></a>
+</td>
+<td valign=top align=left>Diese Informationen werden vom Compiler
+in die Classdatei integriert und können zur Laufzeit über
+das Reflection API ausgelesen werden</td></tr>
+</table>
+<p><i>
+Tabelle 43.2: Sicherbarkeitsattribute</i></p>
+
+<p>
+Wenn Sie keine Angabe zur <a name="ixa103256">Retention</a> machen,
+wird die <a href="index_r.html#ixb102399"><font color=#000080><tt>RetentionPolicy</tt></font></a>
+<font color="#000077"><tt>CLASS</tt></font> als Standardwert ausgewählt.
+Da unsere Annotation jedoch auch zur Laufzeit zur Verfügung stehen
+soll, erweitern wir das Listing ein letztes Mal:
+<a name="revision"></a>
+
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
+<tr>
+<td valign=top>
+<font color="#000055">
+<pre>
+<font color="#555555">001 </font><font color="#0000AA">import</font> java.lang.annotation.Target;
+<font color="#555555">002 </font><font color="#0000AA">import</font> java.lang.annotation.ElementType;
+<font color="#555555">003 </font><font color="#0000AA">import</font> java.lang.annotation.Retention;
+<font color="#555555">004 </font><font color="#0000AA">import</font> java.lang.annotation.RetentionPolicy;
+<font color="#555555">005 </font>
+<font color="#555555">006 </font><font color="#00AA00">// Diese Annotation ist auf Klassen und Methoden beschränkt</font>
+<font color="#555555">007 </font>@Target({ElementType.TYPE, ElementType.METHOD})
+<font color="#555555">008 </font>
+<font color="#555555">009 </font><font color="#00AA00">// Die Information soll auch zur Laufzeit zur Verfügung stehen</font>
+<font color="#555555">010 </font>@Retention(RetentionPolicy.RUNTIME)
+<font color="#555555">011 </font><font color="#0000AA">public</font> @<font color="#0000AA">interface</font> Revision
+<font color="#555555">012 </font>{
+<font color="#555555">013 </font> <font color="#006699">int</font> id();
+<font color="#555555">014 </font> String name();
+<font color="#555555">015 </font> String vorname();
+<font color="#555555">016 </font> String notizen() <font color="#0000AA">default</font> <font color="#0000FF">""</font>;
+<font color="#555555">017 </font>}</pre>
+</font>
+</td>
+<td valign=top align=right>
+<a href="../examples/Revision.java"><font color="#000055" size=-1>Revision.java</font></a></td>
+</tr>
+</table>
+<i>
+Listing 43.19: Vollständige Annotation</i></p>
+
+
+<!-- Section -->
+<a name="sectlevel4id043006006003"></a>
+<h4>Dokumentieren der Annotation und Vererbung </h4>
+
+<p>
+Über die zusätzliche Annotation <a href="index_0.html#ixb102400"><font color=#000080><tt>@Documented</tt></font></a>
+kann schließlich gesteuert werden, ob die Verwendung der Annotation
+auch in der JavaDoc-Dokumentation angezeigt werden soll. Und über
+die Annotation <a href="index_0.html#ixb102401"><font color=#000080><tt>@Inherited</tt></font></a>
+gibt man an, ob eine annotierte Klasse diese Zusatzinformationen auch
+an davon abgeleitete Klassen vererbt.
+
+<!-- Section -->
+
+<a name="sectlevel3id043006007"></a>
+<h3>43.6.7 Auslesen von Annotationen </h3>
+
+<p>
+Da dies primär ein Kapitel über Reflection, als über
+Annotationen ist, beenden wir diesen kleinen Ausflug mit dem Wissen,
+wie die zuvor hinterlegten Meta-Informationen auch zur Laufzeit ausgelesen
+werden können. Das ist nicht schwieriger als das Ermitteln der
+von einer Klasse implementierten Methoden oder ihrer Modifier, wie
+wir es in <a href="k100274.html#reflectionmethodenaufrufe">Abschnitt 43.3</a>
+kennengelernt haben.
+
+<p>
+Seit der Version 5 des JDK stellt das Reflection API das Interface
+<a href="index_a.html#ixb102402"><font color=#000080><tt>AnnotatedElement</tt></font></a>
+zur Verfügung. Es wir von nahezu allen Reflection-Typen wie <a href="index_c.html#ixb100641"><font color=#000080><tt>Class</tt></font></a>,
+<a href="index_m.html#ixb102337"><font color=#000080><tt>Method</tt></font></a>
+oder <a href="index_f.html#ixb102373"><font color=#000080><tt>Field</tt></font></a>
+implementiert und besitzt folgende Signatur:
+<p>
+<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
+<tr>
+<td valign=top width=100%>
+<font color="#660066">
+<pre>
+boolean isAnnotationPresent(
+ Class<? extends Annotation> annotationClass);
+
+<T extends Annotation> T getAnnotation(Class<T> annotationClass);
+
+Annotation[] getAnnotations();
+
+Annotation[] getDeclaredAnnotations();
+
+</pre>
+</font>
+</td>
+<td valign=top>
+<a href="../jdkdocs/api/java/lang/reflect/AnnotatedElement.html" onClick="this.href=getApiDoc('java.lang.reflect.AnnotatedElement')"><font color="#660066" size=-1>java.lang.reflect.AnnotatedElement</font></a></td>
+</tr>
+</table>
+
+<p>
+Über die Methode <a name="ixa103257"><a href="index_i.html#ixb102403"><font color=#000080><tt>isAnnotationPresent</tt></font></a></a>
+kann festgestellt werden, ob das Element um die übergebenen Annotation
+erweitert wurde und über die Methode <a name="ixa103258"><a href="index_g.html#ixb102404"><font color=#000080><tt>getAnnotation</tt></font></a></a>
+ist es möglich das Annotations-Objekt auszulesen.
+
+<p>
+Die beiden letzten Methoden geben schließlich alle zu einer
+Element gehörenden Annotationen in Form eines Arrays zurück.
+Der Unterschied zwischen beiden ist, dass <a name="ixa103259"><a href="index_g.html#ixb102405"><font color=#000080><tt>getDeclaredAnnotations</tt></font></a></a>
+nur die tatsächlich an das Element angehangenen Metainformationen
+zurückgibt, während <a name="ixa103260"><a href="index_g.html#ixb102406"><font color=#000080><tt>getAnnotations</tt></font></a></a>
+auch die geerbten Annotationen einschließt. Besitzt das Element
+keinerlei Annotationen, ist das Resultat der Methoden ein Array der
+Länge 0.
+<hr>
+<table border=0 cellpadding=0 cellspacing=1 width="100%">
+<tr bgcolor="#EEFFCC">
+<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
+<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
+Wesley, Version 5.0.1
+<tr bgcolor="#EEFFCC">
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100271.html"> << </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100276.html"> < </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100278.html"> > </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="k100279.html"> >> </a>
+<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
+<td align="right">© 1998, 2007 Guido Krüger & Thomas
+Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
+</table>
+<a name="endofbody"></a>
+</body>
+</html>
|
