将Java对象/类从一个类装入器复制到另一个类装入器 [英] Copy java object/class from one classloader to another classloader
问题描述
你有一种方法可以将一个类加载的上下文(atrributes等)从一个类加载器(例如'made'类Point)复制到另一个类加载器。
Hi is there a way to copy one class loaded context (atrributes etc) from one classloader (for instance a 'made' class Point) to another classloader?
清除
示例:
我有一个对象在CL 1上的点。
现在在另一个CL2上运行,我想在CL 3中创建这个对象。
Making clear, Example: I have an object Point on CL 1. Now running on another CL2, I want to creat this object in CL 3.
一些obj:
class Point {
int x;
int y;
public Point() {}
//getters and setters
风景:
...
class CL2 {
// Running on CL 2
...
// Point obj from CL 1
Object point = gotFromCL1();
// Want to create the object on Cl2
Object pointCL2 = point.conversion();
但我不能使用sun.reflection(不可用),并且序列化不工作,因为CL2自动重建对象与CL 1.
But I can't use sun.reflection (not available) and serialization doesn't work since CL2 automatically "reconstruct" the object with CL 1.
我所做的一个解决方案是做一个100%java反射rconstruct,基本上获得对象的字段从CL2和
One solution that I tought was do an "100%" java reflection rconstruct, basically getting the fields of object from CL2 and setting up on the new obj.
PS:它需要在Java 1.4.2上运行:'(
PS: It needs to run on Java 1.4.2 :'(
推荐答案
请参阅 Transloader 如何在ClassLoaders如果您需要执行以下操作之一:
See Transloader on how to copy classes between ClassLoaders if you need to do one of the following:
-
将类加载器移动到另一个
Clone almost any object graph from one ClassLoader to another
从外部ClassLoader获取任何对象并调用任何方法 li>
Take any object from a foreign ClassLoader and invoke any method on it without cloning it
这篇关于将Java对象/类从一个类装入器复制到另一个类装入器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!