1 package dynaop; 2 3 import java.io.IOException ; 4 import java.io.ObjectInputStream ; 5 import java.io.ObjectOutputStream ; 6 import java.io.Serializable ; 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Method ; 9 import java.lang.reflect.Modifier ; 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import dynaop.util.Classes; 16 import dynaop.util.Closure; 17 import dynaop.util.NestedException; 18 19 import net.sf.cglib.Enhancer; 20 import net.sf.cglib.Factory; 21 import net.sf.cglib.MethodInterceptor; 22 import net.sf.cglib.MethodProxy; 23 24 30 class CglibProxyHandle implements Handle, Serializable { 31 32 static long serialVersionUID = 0; 33 34 static MethodInterceptor MOCK_INTERCEPTOR = new MethodInterceptor() { 35 public Object intercept(Object proxy, Method method, Object [] arguments, 36 MethodProxy methodProxy) throws Throwable { 37 throw new IllegalStateException ( 38 "Cannot invoke proxy during deserialization."); 39 } 40 }; 41 42 transient Proxy proxy; 43 44 CglibProxyHandle(Proxy proxy) { 45 this.proxy = proxy; 46 } 47 48 public Proxy getProxy() { 49 return this.proxy; 50 } 51 52 private void writeObject(ObjectOutputStream out) throws IOException { 53 List interfaces = 55 filterInterfaces(this.proxy.getClass().getInterfaces()); 56 out.writeInt(interfaces.size()); 57 for (Iterator i = interfaces.iterator(); i.hasNext();) 58 out.writeUTF(((Class ) i.next()).getName()); 59 60 out.writeUTF(proxy.getClass().getSuperclass().getName()); 61 62 out.writeObject(Enhancer.getMethodInterceptor(this.proxy)); 64 65 out.writeObject(unwrap()); 66 } 67 68 Object unwrap() { 69 Object unwrapped; 70 try { 71 unwrapped = proxy.getClass().getSuperclass().newInstance(); 72 } 73 catch (Exception e) { 74 throw NestedException.wrap(e); 75 } 76 execute(fieldCopier(this.proxy, unwrapped), unwrapped.getClass()); 77 78 return unwrapped; 79 } 80 81 private void readObject(ObjectInputStream in) throws IOException , 82 ClassNotFoundException { 83 int size = in.readInt(); 85 List interfaces = new ArrayList (); 86 for (int i = 0; i < size; i++) 87 interfaces.add(Classes.getClassLoader().loadClass(in.readUTF())); 88 89 Class unproxiedClass = Classes.getClassLoader().loadClass(in.readUTF()); 90 91 List allClasses = new ArrayList (interfaces); 93 allClasses.add(unproxiedClass); 94 this.proxy = (Proxy) Enhancer.enhance( 95 unproxiedClass, 96 (Class []) interfaces.toArray(new Class [interfaces.size()]), 97 MOCK_INTERCEPTOR, 98 Classes.commonLoader(allClasses), 99 null, 100 ClassProxyCreator.METHOD_FILTER 101 ); 102 103 MethodInterceptor interceptor = (MethodInterceptor) in.readObject(); 105 106 Object unproxied = in.readObject(); 108 109 execute(fieldCopier(unproxied, this.proxy), 110 unproxied.getClass()); 111 112 ((Factory) this.proxy).interceptor(interceptor); 113 } 114 115 118 void execute(Closure closure, Class clazz) { 119 while (!clazz.equals(Object .class)) { 120 Field [] fields = clazz.getDeclaredFields(); 121 for (int i = 0; i < fields.length; i++) { 122 fields[i].setAccessible(true); 123 124 if (Modifier.isFinal(fields[i].getModifiers())) 126 continue; 127 128 closure.execute(fields[i]); 129 } 130 clazz = clazz.getSuperclass(); 131 } 132 } 133 134 137 static List filterInterfaces(Class [] interfaces) { 138 List interfaceList = new ArrayList (Arrays.asList(interfaces)); 139 for (Iterator i = interfaceList.iterator(); i.hasNext();) 140 if (((Class ) i.next()).getName().startsWith("net.sf.cglib.")) 141 i.remove(); 142 return interfaceList; 143 } 144 145 Closure fieldCopier(final Object src, final Object dst) { 146 return new Closure() { 147 public void execute(Object o) { 148 Field field = (Field ) o; 149 try { 150 field.set(dst, field.get(src)); 151 } 152 catch (Exception e) { 153 throw NestedException.wrap(e); 154 } 155 } 156 }; 157 } 158 } 159
| Popular Tags
|