•
!
Module 8 – Annotations
""#$%&'()*+(,-")(,
+./
•
)012
•
(3.232
•
*12
•
"32
•
"3"4
516
•
.7411provide additional
information about existing pieces of data$
•
1413344$*1891
83343.8.331139
3344$
•
.32343737$"443
3.848$
•
1133443323..8
.313.23431
3$
•
annotation4.81334431$
3317218$
•
Annotation typ17$*1.81334431
43$
•
*104.74:
// Definition of an Annotation type
public @interface TestAnnotation {
String value;
}
// Usage of Annotation
@TestAnnotation (value="name")
public void display() {}
Uses of Annotations
•
14.32:
–
Information for the compiler;..8143
333333$
–
Compiler-time and deployment-time processing;23
32343<=>7<231$
–
Runtime processing;43..04
34$
•
.334?32<
7<41<133344$
(+(+-@*
DO
DO
if the metadata changes the
design of the class.
if the metadata changes the
design of code dealing with
class.
if the metadata changes the
design of the class.
if the metadata changes the
design of code dealing with
class.
DON’T
DON’T
•
2883
.3..
33
.
•
51873
384.$
•
2883
.3..
33
.
•
51873
384.$
"32
•
3A3
–
"241
–
B3<1@TestAnno
•
–
"2<4343A3$
–
".33:C3
–
)04:@TestAnno(“Testing”)
•
D
–
"2444.3$
–
)144.333.8C3
–
@TestAnno(owner=“Stephen”, value=“class scope”)
E
•
F(3
•
F+33
•
F353
•
F*3
•
F
•
F13
GGuse a deprecated method and tell
GGcompiler not to generate a warning
@SuppressWarnings("deprecation")
(31HIJ ./+$31HIK
GG33suppressed
L
Annotations Used by the Compiler
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated static void deprecatedMethod() { }
}
GGmark method as a superclass method
GGthat has been overridden
@Override 331HIJL
F(3
•
518<3283113
3$
•
2<8133H32
1I431<131
$,33:
–
3<.8<3118M
–
8233
–
3.3
F353
•
*234143
3331
41
131.
3$
%N
F353
•
*114331.
1142.
4$
•
*123341
32132.
4$
•
04<283
32413133<
.13.31418$
F353)04
class Thu
{
@Deprecated
public void chao() {System.out.println("\n Hello");}
}
class ThuTest
{
@SuppressWarnings({"deprecation"})
public void test()
{ Thu t = new Thu();
t.chao();
}
}
public class SuppressWarning
{
public static void main(String[] args)
{
ThuTest h = new ThuTest();
h.test();
}
}
F(4
•
*1F(4711.
14<4.81/$
•
*121F(41.
A/318
234134$
F
•
*181F34
13113$
•
1N:
–
8$+'")
–
8$">
–
8$'-*)
)=>)
@Retention(RetentionPolicy.SOURCE)
@interface Test_Retention
{
String v1();
}
class RetentionTest
{
@Test_Retention(v1="Hello")
public void thu() {System.out.println("Test annotations");}
}
class RetentionAnno
{public static void main(String args[])
{ new RetentionTest().thu();
}
}
"423"
•
*3823<1OFO84.
2.81A83<3214
$
•
343.1(3$
–
3431
–
34312
–
34333441
"4231
•
5132341<1
31F$
•
*1332144.321
243$
•
*13281.14
A11
4438
Module 9
Re2ection API
""#$%&'()*+(,-")(,
+.
PN
3
•
1 .8 2 < ./ 32 7
234.2$
•
*13334<1.3<
.33&'.3
•
23123:
–
8Q
–
"3./
–
"31338
PR
8Q
•
34:
–
2 2 1 .< 1 .8 A getName() 41< 1
4.33$
•
37:
–
E8AgetField() 41./$
•
333:
–
E8AgetConstructors() 41./$
•
1234:
–
E8AgetMethods() 41./$
P