1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved. 4 * 5 * The contents of this file are subject to the Mozilla Public License Version 6 * 1.1 (the "License"); you may not use this file except in compliance with 7 * the License. Alternatively, the contents of this file may be used under 8 * the terms of the GNU Lesser General Public License Version 2.1 or later. 9 * 10 * Software distributed under the License is distributed on an "AS IS" basis, 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12 * for the specific language governing rights and limitations under the 13 * License. 14 */ 15 16 package javassist.util.proxy; 17 18 import java.io.Serializable; 19 import java.io.ObjectStreamException; 20 import java.security.AccessController; 21 import java.security.PrivilegedActionException; 22 import java.security.PrivilegedExceptionAction; 23 import java.security.ProtectionDomain; 24 25 /** 26 * A proxy object is converted into an instance of this class 27 * when it is written to an output stream. 28 * 29 * @see RuntimeSupport#makeSerializedProxy(Object) 30 */ 31 class SerializedProxy implements Serializable { 32 private String superClass; 33 private String[] interfaces; 34 private byte[] filterSignature; 35 private MethodHandler handler; 36 37 SerializedProxy(Class proxy, byte[] sig, MethodHandler h) { 38 filterSignature = sig; 39 handler = h; 40 superClass = proxy.getSuperclass().getName(); 41 Class[] infs = proxy.getInterfaces(); 42 int n = infs.length; 43 interfaces = new String[n - 1]; 44 String setterInf = ProxyObject.class.getName(); 45 for (int i = 0; i < n; i++) { 46 String name = infs[i].getName(); 47 if (!name.equals(setterInf)) 48 interfaces[i] = name; 49 } 50 } 51 52 /** 53 * Load class. 54 * 55 * @param className the class name 56 * @return loaded class 57 * @throws ClassNotFoundException for any error 58 */ 59 protected Class loadClass(final String className) throws ClassNotFoundException { 60 try { 61 return (Class)AccessController.doPrivileged(new PrivilegedExceptionAction(){ 62 public Object run() throws Exception{ 63 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 64 return Class.forName(className, true, cl); 65 } 66 }); 67 } 68 catch (PrivilegedActionException pae) { 69 throw new RuntimeException("cannot load the class: " + className, pae.getException()); 70 } 71 } 72 73 Object readResolve() throws ObjectStreamException { 74 try { 75 int n = interfaces.length; 76 Class[] infs = new Class[n]; 77 for (int i = 0; i < n; i++) 78 infs[i] = loadClass(interfaces[i]); 79 80 ProxyFactory f = new ProxyFactory(); 81 f.setSuperclass(loadClass(superClass)); 82 f.setInterfaces(infs); 83 ProxyObject proxy = (ProxyObject)f.createClass(filterSignature).newInstance(); 84 proxy.setHandler(handler); 85 return proxy; 86 } 87 catch (ClassNotFoundException e) { 88 throw new java.io.InvalidClassException(e.getMessage()); 89 } 90 catch (InstantiationException e2) { 91 throw new java.io.InvalidObjectException(e2.getMessage()); 92 } 93 catch (IllegalAccessException e3) { 94 throw new java.io.InvalidClassException(e3.getMessage()); 95 } 96 } 97 } 98