summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100277.html
blob: 7140acc006eea124136d9631940598e7e0b594e7 (plain)
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">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100271.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100276.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100278.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100279.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</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&uuml;ssel-Wert-Paare in Annotationen</a>
<ul>
<li><a href="k100277.html#sectlevel4id043006004001">Unterst&uuml;tzte Datentypen</a>
</ul>
<li><a href="k100277.html#sectlevel3id043006005">43.6.5 Standardwerte f&uuml;r Attribute</a>
<li><a href="k100277.html#sectlevel3id043006006">43.6.6 Einschr&auml;nken von Annotationen</a>
<ul>
<li><a href="k100277.html#sectlevel4id043006006001">Einschr&auml;nken der Verwendbarkeit</a>
<li><a href="k100277.html#sectlevel4id043006006002">Einschr&auml;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&uuml;r sind sie gut?
Nun, Metainformationen sind einfach Informationen &uuml;ber Informationen
und k&ouml;nnen daf&uuml;r verwendet werden, zus&auml;tzliches Wissen
abzulegen. In unserem Fall handelt es sich dabei um zus&auml;tzliches
Wissen &uuml;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&uuml;hrung gekommen. So sind beispielsweise die zus&auml;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&uuml;r das JavaDoc-Werkzeug, (steht in <a href="k100322.html#sectiondoku">Abschnitt 51.5.3</a>)
nichts anderes als zus&auml;tzliche Informationen, die dazu verwendet
werden k&ouml;nnen, die Schnittstellen eines Programms besser zu dokumentieren.

<p>
Im Kapitel &uuml;ber Persistenz werden wir au&szlig;erdem sehen, dass
Annotationen h&auml;ufig dazu verwendet werden, Konfigurationsinformationen
f&uuml;r zus&auml;tzlichen Frameworks zusammen mit dem betreffenden
Code abzulegen und uns so weitestgehend von der Last zus&auml;tzlicher
Konfigurationsdateien zu befreien. Es ist jedoch ebenso leicht eigene
Annotationen zu entwickeln und ihre Informationen anschlie&szlig;end
&uuml;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&uuml;ber , wann
und von wem eine Klasse oder eine Methode zuletzt ver&auml;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 &uuml;ber die Reflection API auswerten k&ouml;nnen, m&uuml;ssen
wir sie zun&auml;chst definieren. 

<!-- Section -->

<a name="sectlevel4id043006002001"></a>
<h4>Die Annotationsklasse </h4>

<p>
Mit Annotationen verh&auml;lt es sich &auml;hnlich wie mit einem Interface,
das in gewisser Weise auch zus&auml;tzliche Informationen &uuml;ber
eine Klasse zur Verf&uuml;gung stellt. So ist es kaum verwunderlich,
dass eine Annotation ganz &auml;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&uuml;r Klassen verwendet werden.

<!-- Section -->

<a name="sectlevel4id043006002002"></a>
<h4>Verwendung von Annotationen </h4>

<p>
Annotationen dienen dazu, zus&auml;tzliche Informationen im Quellcode
zu hinterlegen. Dabei k&ouml;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&auml;ter sehen, wie wir diese Liste einschr&auml;nken k&ouml;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&ouml;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&uuml;nschten Zusatzinformationen angereichert.

<p>
Der eine oder andere Leser k&ouml;nnte jetzt anmerken, dass die Klasse
zwar nun annotiert ist, jedoch noch keine wirklichen Zusatzinformationen
enth&auml;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&auml;nzen. 

<!-- Section -->

<a name="sectlevel3id043006003"></a>
<h3>43.6.3 Annotationen mit einem Wert </h3>

<p>
Um tats&auml;chlich zus&auml;tzliche Informationen ablegen zu k&ouml;nnen,
m&uuml;ssen wir die Annotation nun gewisserma&szlig;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&auml;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&szlig;end ge&auml;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&uuml;ckt,
aber f&uuml;r den Compiler steht sie immer noch in der Liest der Modifier.
Wir k&ouml;nnen Javaklassen prinzipiell auch als Einzeiler formulieren.
Um die &Uuml;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&auml;ter auch auswerten k&ouml;nnen. 

<!-- Section -->

<a name="sectlevel3id043006004"></a>
<h3>43.6.4 Beliebige Schl&uuml;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&ouml;nnen, m&uuml;ssten
wir nun weitere Annotationen definieren, was in einem Wust von zus&auml;tzlichen
&#187;Modifizierern&#171; enden w&uuml;rde. Um dies zu vermeiden und
zusammengeh&ouml;rende Informationen in einer Annotation zusammenzufassen,
k&ouml;nnen diese beliebig viele Informationen aufnehmen. 

<p>
Der Schl&uuml;ssel <font color="#000077"><tt>value</tt></font> stellt
dabei nur einen Spezialfall f&uuml;r ein Standard-Attribut dar. Wenn
wir den Namen des Attributs nicht angeben wei&szlig;t Java den Wert
der Annotation dem Attribut <font color="#000077"><tt>value</tt></font>
zu. Alternativ w&auml;re auch folgende Schreibweise m&ouml;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&auml;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&szlig;end ge&auml;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&ouml;nnen, verfeinern wir diese folgenderma&szlig;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&uuml;rlich auch gef&uuml;llt werden m&ouml;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&uuml;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&uuml;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&uuml;rfen. 

<!-- Section -->

<a name="sectlevel4id043006004001"></a>
<h4>Unterst&uuml;tzte Datentypen </h4>

<p>
Die Attribute einer Annotation d&uuml;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&auml;hlungstypen
<li><font color="#000077"><tt>Class</tt></font>
<li>Andere Annotationen
<li>Eindimensionale Felder der hier aufgez&auml;hlten Typen
</ul>

<p>
Die Liste ist auf diese Typen beschr&auml;nkt, weil der Compiler mit
ihnen die n&ouml;tigen Typ&uuml;berpr&uuml;fungen vornehmen kann.


<!-- Section -->
<a name="sectlevel3id043006005"></a>
<h3>43.6.5 Standardwerte f&uuml;r Attribute </h3>

<p>
Die Spezifikation f&uuml;r Annotation schreibt vor, dass alle definierten
Attribute einer Annotation auch verwendet werden m&uuml;ssen. Um dennoch
optionale Attribute definieren zu k&ouml;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&uuml;rlich
mit dem jeweils Angegebenen &uuml;berschrieben. 

<!-- Section -->

<a name="sectlevel3id043006006"></a>
<h3>43.6.6 Einschr&auml;nken von Annotationen </h3>

<p>
Bisher haben wir noch keinerlei Einschr&auml;nkungen f&uuml;r unsere
Annotation definiert und k&ouml;nnen diese deshalb sowohl f&uuml;r
Pakete wie auch f&uuml;r lokale Variablen verwenden. Da jedoch nicht
jede Annotation f&uuml;r jedes Element sinnvoll ist, k&ouml;nnen wir
ihre Verwendung bei Bedarf einschr&auml;nken. Und warum sollten wir
hierf&uuml;r etwas anderes verwenden als wiederum eine Annotation.


<!-- Section -->
<a name="sectlevel4id043006006001"></a>
<h4>Einschr&auml;nken der Verwendbarkeit </h4>

<p>
&Uuml;ber die Annotation <a href="index_0.html#ixb102392"><font color=#000080><tt>@Target</tt></font></a>
k&ouml;nnen wir die Verwendbarkeit einer Annotationen auf bestimmte
Elemente einschr&auml;nken. M&ouml;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&auml;nken, erg&auml;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&auml;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&auml;nken der Verwendbarkeit</i></p>

<p>
An diesem Beispiel k&ouml;nnen Sie au&szlig;erdem die Verwendung von
Arrays und Aufz&auml;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&auml;lt ein Array aus Aufz&auml;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&auml;nken der Sichtbarkeit </h4>

<p>
Die Verwendungsm&ouml;glichkeiten von Annotationen reichen von Zusatzinformationen
f&uuml;r Dokumentationstools bis hin zu Konfigurationsdaten, die das
Laufzeitverhalten der Java Virtual Machine beeinflussen k&ouml;nnen.
Um nicht alle - m&ouml;glicherweise - unn&ouml;tigen Metainformationen
bereitstellen zu m&uuml;ssen, lassen sich diese &uuml;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&uuml;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&ouml;nnen zur Laufzeit &uuml;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&auml;hlt.
Da unsere Annotation jedoch auch zur Laufzeit zur Verf&uuml;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&auml;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&uuml;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&auml;ndige Annotation</i></p>


<!-- Section -->
<a name="sectlevel4id043006006003"></a>
<h4>Dokumentieren der Annotation und Vererbung </h4>

<p>
&Uuml;ber die zus&auml;tzliche Annotation <a href="index_0.html#ixb102400"><font color=#000080><tt>@Documented</tt></font></a>
kann schlie&szlig;lich gesteuert werden, ob die Verwendung der Annotation
auch in der JavaDoc-Dokumentation angezeigt werden soll. Und &uuml;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&auml;r ein Kapitel &uuml;ber Reflection, als &uuml;ber
Annotationen ist, beenden wir diesen kleinen Ausflug mit dem Wissen,
wie die zuvor hinterlegten Meta-Informationen auch zur Laufzeit ausgelesen
werden k&ouml;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&uuml;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&lt;? extends Annotation&gt; annotationClass);

&lt;T extends Annotation&gt; T getAnnotation(Class&lt;T&gt; 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>
&Uuml;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 &uuml;bergebenen Annotation
erweitert wurde und &uuml;ber die Methode <a name="ixa103258"><a href="index_g.html#ixb102404"><font color=#000080><tt>getAnnotation</tt></font></a></a>
ist es m&ouml;glich das Annotations-Objekt auszulesen. 

<p>
Die beiden letzten Methoden geben schlie&szlig;lich alle zu einer
Element geh&ouml;renden Annotationen in Form eines Arrays zur&uuml;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&auml;chlich an das Element angehangenen Metainformationen
zur&uuml;ckgibt, w&auml;hrend <a name="ixa103260"><a href="index_g.html#ixb102406"><font color=#000080><tt>getAnnotations</tt></font></a></a>
auch die geerbten Annotationen einschlie&szlig;t. Besitzt das Element
keinerlei Annotationen, ist das Resultat der Methoden ein Array der
L&auml;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">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</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">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100276.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100278.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100279.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">&copy; 1998, 2007 Guido Kr&uuml;ger &amp; Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>