01: /*
02: * User: mrettig
03: * Date: Aug 10, 2002
04: */
05: package net.sourceforge.jaxor;
06:
07: import java.lang.reflect.InvocationHandler;
08: import java.lang.reflect.InvocationTargetException;
09: import java.lang.reflect.Method;
10: import java.lang.reflect.Proxy;
11: import java.util.Arrays;
12: import java.util.HashSet;
13: import java.util.Set;
14:
15: public class LazyProxy implements InvocationHandler {
16:
17: public static Object create(LazyLoader loader, Class interfaceClass) {
18: Set allInterfaces = new HashSet();
19: ClassLoader classLoader = interfaceClass.getClassLoader();
20: do {
21: allInterfaces.addAll(Arrays.asList(interfaceClass
22: .getInterfaces()));
23: interfaceClass = interfaceClass.getSuperclass();
24: } while (interfaceClass != null);
25: Class[] interfaces = (Class[]) allInterfaces
26: .toArray(new Class[allInterfaces.size()]);
27: return Proxy.newProxyInstance(classLoader, interfaces,
28: new LazyProxy(loader));
29: }
30:
31: private final LazyLoader _loader;
32:
33: private LazyProxy(LazyLoader _loader) {
34: this ._loader = _loader;
35: }
36:
37: public Object invoke(Object proxy, Method method, Object[] args)
38: throws Throwable {
39: try {
40: return method.invoke(_loader.getValue(), args);
41: } catch (InvocationTargetException targetExc) {
42: throw targetExc.getTargetException();
43: }
44: }
45: }
|