groovy에서 직렬화 가능 인터페이스 구현을 동적으로 생성하려고하는데,이 직렬화 인터페이스는 직렬화가 가능하고 args로 직렬화 될 수 있습니다. 지도를 사용하여 익명 인터페이스 구현을 만들었지 만 직렬화에 실패합니다. 그루비 폐쇄가 불가능 직렬화됩니다 경우에도 직렬화 복원 할 수 있도록 할 임의의 클래스 이름에 컴파일 된 경우클래스 정의가없는 Groovy 직렬화
gcloader = new GroovyClassLoader()
script = "class X { public def x = [call: {y -> y+1}] as MyCallable }"
gclass = gcloader.parseClass(script)
x = gclass.newInstance().x
// serialzing x fails
나는 확실하지 않다. 이것을 할 수있는 방법이 있습니까?
import groovy.lang.GroovyClassLoader
def gcLoader = new GroovyClassLoader()
def script = """
class X implements Serializable {
public def x = [
call: { y -> y + 1 }
]
}"""
def cls = gcLoader.parseClass(script)
def inst = cls.newInstance().x
def baos = new ByteArrayOutputStream()
def oos = new ObjectOutputStream(baos)
oos.writeObject(inst)
def serialized = baos.toByteArray()
def bais = new ByteArrayInputStream(serialized)
def ois = new CustomObjectInputStream(bais, gcLoader)
inst = ois.readObject()
assert 2 == inst.call(1)
public class CustomObjectInputStream extends ObjectInputStream {
private ClassLoader classLoader
public CustomObjectInputStream(InputStream ins, ClassLoader classLoader) throws IOException {
super(ins)
this.classLoader = classLoader
}
protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException {
return Class.forName(desc.getName(), false, classLoader)
}
}
는 기본적으로, 당신은 사용자 정의 ClassLoader
으로 ObjectInputStream
의 인스턴스가 필요합니다
예외는 무엇입니까? –
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SerializationUtils.html을 사용하고 이상하게도 java.io.NotSerializableException이 발생합니다 : X ie 최상위 클래스 : ( – FUD
실제로 내가 "X 구현하는 경우 .."다음 직렬화 할 수 오전 deserialzing java.lang.ClassNotFoundException 얻을 : X $ _closure1 이해할 수 있습니다. 그래서 일부는 수행 할 수 있습니다. 다른 속임수? – FUD