I have the following problem: I want to send the type (java.lang.Class) by wire and "define" the class on the other side.
I tried like this:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(MyClass.class);
and on the receiving side:
ByteArrayInputStream bis = new ByteArrayInputStream(request.getBytes());
ObjectInputStream ois = new ObjectInputStream(bis);
Class c = (Class) ois.readObject();
so obviously i need to send the class source bytecode and do
ClassLoader.defineClass(bytes, ..
but unfortunately I donโt see how I can get the byte code of the loaded class. I am looking for something like:
byte[] byteCode = MyClass.class.toByteArray();
Is this even possible using the standard JDK, or is there a small lib that can do this?
source
share