1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
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>
|