blob: 5e0e2ad838324177dca275590c13c10c946d69fc (
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
|
/*
A base-level class controlled by VerboseMetaobj.
*/
package sample.reflect;
import javassist.reflect.Metalevel;
import javassist.reflect.Metaobject;
public class Person {
public String name;
public static int birth = 3;
public static final String defaultName = "John";
public Person(String name, int birthYear) {
if (name == null)
this.name = defaultName;
else
this.name = name;
this.birth = birthYear;
}
public String getName() {
return name;
}
public int getAge(int year) {
return year - birth;
}
public static void main(String[] args) {
String name;
if (args.length > 0)
name = args[0];
else
name = "Bill";
Person p = new Person(name, 1960);
System.out.println("name: " + p.getName());
System.out.println("object: " + p.toString());
// change the metaobject of p.
if (p instanceof Metalevel) {
((Metalevel)p)._setMetaobject(new Metaobject(p, null));
System.out.println("<< the metaobject was changed.>>");
}
System.out.println("age: " + p.getAge(1999));
}
}
|