/Users/lyon/j4p/src/javassist/sample/reflect/Person.java
|
1 /*
2 A base-level class controlled by VerboseMetaobj.
3 */
4
5 package javassist.sample.reflect;
6
7 import javassist.reflect.Metalevel;
8 import javassist.reflect.Metaobject;
9
10 public class Person {
11 public String name;
12 public static int birth = 3;
13 public static final String defaultName = "John";
14
15 public Person(String name, int birthYear) {
16 if (name == null)
17 this.name = defaultName;
18 else
19 this.name = name;
20
21 this.birth = birthYear;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public int getAge(int year) {
29 return year - birth;
30 }
31
32 public static void main(String[] args) {
33 String name;
34 if (args.length > 0)
35 name = args[0];
36 else
37 name = "Bill";
38
39 Person p = new Person(name, 1960);
40 System.out.println("name: " + p.getName());
41 System.out.println("object: " + p.toString());
42
43 // change the metaobject of p.
44 if (p instanceof Metalevel) {
45 ((Metalevel) p)._setMetaobject(new Metaobject(p, null));
46 System.out.println("<< the metaobject was changed.>>");
47 }
48
49 System.out.println("age: " + p.getAge(1999));
50 }
51 }
52