我有以下問題:我想通過電線發送一個類型(java.lang.Class),並在另一側'定義'類。如何通過電線發送類
我想這樣的:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(MyClass.class);
,並在接收端:
ByteArrayInputStream bis = new ByteArrayInputStream(request.getBytes());
ObjectInputStream ois = new ObjectInputStream(bis);
Class c = (Class) ois.readObject(); // ClassNotFoundException
所以很明顯,我需要發送的類別的原始字節碼,並做了
ClassLoader.defineClass(bytes, ..
,但不幸的是我看不到我如何檢索一個加載類的bytcode。 我在尋找類似:
byte[] byteCode = MyClass.class.toByteArray();
這甚至與標準的JDK可能或有任何小的lib在那裏,能做到嗎?
聽起來像你正在試圖重塑RMI。 – 2009-06-02 22:39:25