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