KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > evolve > VersionManager


1 package sample.evolve;
2
3 import java.util.Hashtable JavaDoc;
4 import java.lang.reflect.*;
5 import javassist.CtClass;
6
7 /**
8  * Runtime system for class evolution
9  */

10 public class VersionManager {
11     private static Hashtable JavaDoc versionNo = new Hashtable JavaDoc();
12     public final static String JavaDoc latestVersionField = "_version";
13
14     /**
15      * For updating the definition of class my.X, say:
16      *
17      * VersionManager.update("my.X");
18      */

19     public static void update(String JavaDoc qualifiedClassname)
20     throws CannotUpdateException
21     {
22     try {
23         Class JavaDoc c = getUpdatedClass(qualifiedClassname);
24         Field f = c.getField(latestVersionField);
25         f.set(null, c);
26     }
27     catch (ClassNotFoundException JavaDoc e) {
28         throw new CannotUpdateException("cannot update class: "
29                         + qualifiedClassname);
30     }
31     catch (Exception JavaDoc e) {
32         throw new CannotUpdateException(e);
33     }
34     }
35
36     private static Class JavaDoc getUpdatedClass(String JavaDoc qualifiedClassname)
37     throws ClassNotFoundException JavaDoc
38     {
39     int version;
40     Object JavaDoc found = versionNo.get(qualifiedClassname);
41     if (found == null)
42         version = 0;
43     else
44         version = ((Integer JavaDoc)found).intValue() + 1;
45
46     Class JavaDoc c = Class.forName(qualifiedClassname + '$' + version);
47     versionNo.put(qualifiedClassname, new Integer JavaDoc(version));
48     return c;
49     }
50
51     /* initiaVersion() is used to initialize the _version field of
52      * the updatable classes.
53      */

54     public static Class JavaDoc initialVersion(String JavaDoc[] params) {
55     try {
56         return getUpdatedClass(params[0]);
57     }
58     catch (ClassNotFoundException JavaDoc e) {
59         throw new RuntimeException JavaDoc("cannot initialize " + params[0]);
60     }
61     }
62
63     /** make() performs the object creation of the updatable classes.
64      * The expression "new <updatable class>" is replaced with a call
65      * to this method.
66      */

67     public static Object JavaDoc make(Class JavaDoc clazz, Object JavaDoc[] args) {
68     Constructor[] constructors = clazz.getConstructors();
69     int n = constructors.length;
70     for (int i = 0; i < n; ++i) {
71         try {
72         return constructors[i].newInstance(args);
73         }
74         catch (IllegalArgumentException JavaDoc e) {
75         // try again
76
}
77         catch (InstantiationException JavaDoc e) {
78         throw new CannotCreateException(e);
79         }
80         catch (IllegalAccessException JavaDoc e) {
81         throw new CannotCreateException(e);
82         }
83         catch (InvocationTargetException e) {
84         throw new CannotCreateException(e);
85         }
86     }
87
88     throw new CannotCreateException("no constructor matches");
89     }
90 }
91
Popular Tags