Created
May 29, 2015 09:59
-
-
Save jumperchen/ec63e1b0c41992a57a6c to your computer and use it in GitHub Desktop.
Javassist with JDK 8 serialization issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package issue; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.lang.reflect.Method; | |
import javassist.util.proxy.MethodFilter; | |
import javassist.util.proxy.MethodHandler; | |
import javassist.util.proxy.Proxy; | |
import javassist.util.proxy.ProxyFactory; | |
public class SerializationIssue implements Serializable { | |
protected static MethodFilter METHOD_FILTER = new MethodFilter() { | |
public boolean isHandled(Method m) { | |
if (m.getName().startsWith("get")) | |
return true; | |
return false; | |
} | |
}; | |
public class FormProxyHandler implements MethodHandler, Serializable { | |
FormStatusImpl _status; | |
public FormProxyHandler() { | |
_status = new FormStatusImpl(); | |
} | |
private class FormStatusImpl implements FormStatus, Serializable { | |
private FormProxyObject self; | |
public FormStatusImpl() { | |
} | |
public void setOwner(FormProxyObject self) { | |
this.self = self; | |
} | |
public Object getOrigin() { | |
return self; | |
} | |
} | |
public Object invoke(Object self, Method method, Method proceed, | |
Object[] args) throws Exception { | |
try { | |
final String mname = method.getName(); | |
if ("getFormStatus".equals(mname)) { | |
_status.setOwner((FormProxyObject)self); | |
return _status; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
public interface FormProxyObject { | |
public FormStatus getFormStatus(); | |
} | |
public interface FormStatus { | |
public Object getOrigin(); | |
} | |
private static class POJO implements Serializable { | |
public POJO(){} | |
} | |
public static <T extends Object> T createFormProxy(T origin, Class<?> type) { | |
ProxyFactory factory = new ProxyFactory(); | |
factory.setFilter(METHOD_FILTER); | |
factory.setSuperclass(type); | |
factory.setInterfaces(new Class[]{FormProxyObject.class}); | |
Class<?> proxyClass = factory.createClass(); | |
Object p1 = null; | |
try { | |
p1 = proxyClass.newInstance(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
((Proxy) p1).setHandler(new SerializationIssue().new FormProxyHandler()); | |
return (T) p1; | |
} | |
public static void main(String[] args) throws Exception { | |
POJO pojo = new POJO(); | |
POJO p1 = createFormProxy(pojo, pojo.getClass()); | |
// caused by invoking this method. Fails in Java 8 | |
FormStatus formStatus = ((FormProxyObject) p1).getFormStatus(); | |
ByteArrayOutputStream oaos = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(oaos); | |
oos.writeObject(p1); | |
oos.close(); | |
oaos.close(); | |
byte[] bytes = oaos.toByteArray(); | |
ByteArrayInputStream oaos1 = new ByteArrayInputStream(bytes); | |
ObjectInputStream oos1 = new ObjectInputStream(oaos1); | |
POJO newStatus = (POJO) oos1.readObject(); | |
System.out.println(newStatus); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment