blob: 184397fd39c595b200daeec937501d3ff3381b02 (
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package sample.evolve;
import java.util.Hashtable;
import java.lang.reflect.*;
/**
* Runtime system for class evolution
*/
public class VersionManager {
private static Hashtable versionNo = new Hashtable();
public final static String latestVersionField = "_version";
/**
* For updating the definition of class my.X, say:
*
* VersionManager.update("my.X");
*/
public static void update(String qualifiedClassname)
throws CannotUpdateException {
try {
Class c = getUpdatedClass(qualifiedClassname);
Field f = c.getField(latestVersionField);
f.set(null, c);
}
catch (ClassNotFoundException e) {
throw new CannotUpdateException("cannot update class: "
+ qualifiedClassname);
}
catch (Exception e) {
throw new CannotUpdateException(e);
}
}
private static Class getUpdatedClass(String qualifiedClassname)
throws ClassNotFoundException {
int version;
Object found = versionNo.get(qualifiedClassname);
if (found == null)
version = 0;
else
version = ((Integer)found).intValue() + 1;
Class c = Class.forName(qualifiedClassname + '$' + version);
versionNo.put(qualifiedClassname, new Integer(version));
return c;
}
/*
* initiaVersion() is used to initialize the _version field of the updatable
* classes.
*/
public static Class initialVersion(String[] params) {
try {
return getUpdatedClass(params[0]);
}
catch (ClassNotFoundException e) {
throw new RuntimeException("cannot initialize " + params[0]);
}
}
/**
* make() performs the object creation of the updatable classes. The
* expression "new <updatable class>" is replaced with a call to this
* method.
*/
public static Object make(Class clazz, Object[] args) {
Constructor[] constructors = clazz.getConstructors();
int n = constructors.length;
for (int i = 0; i < n; ++i) {
try {
return constructors[i].newInstance(args);
}
catch (IllegalArgumentException e) {
// try again
}
catch (InstantiationException e) {
throw new CannotCreateException(e);
}
catch (IllegalAccessException e) {
throw new CannotCreateException(e);
}
catch (InvocationTargetException e) {
throw new CannotCreateException(e);
}
}
throw new CannotCreateException("no constructor matches");
}
}
|