Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java classloader tracing [duplicate]

I have two ClassLoaders which loads the same class. So, obviously these can't cast to one another. But I need to access an object created in the other ClassLoader.

I have access to both ClassLoaders. How can I use that object in the other class? I don't need to cast the object to match to the current ClassLoader.

But the issue is that the returned object's type is Object. So, I have to cast down that object to access some methods. How can I do that? Normal cast like the following causes ClassCastException, which I already know.

Mojo mojo = (Mojo) descriptor.getMojo();

descriptor#getMojo() returns an object of type Mojo but the method returns Object. How can do this?

Let me know if you need further info.

I've read all the theories about classloading, but none has specified a proper solution for this.

like image 383
ravana Avatar asked Aug 16 '11 06:08

ravana


3 Answers

AFAIK, no, you can't cast an object of a class loaded by one class-loader in another class loader.

  • One solution would be to create a "common" class-loader which loads the classes to be used by your custom classloaders. So in your case, you'd have a new classloader which would load the given class and your custom classloaders would extend this classloader.
  • Another solution would be to pass around the "serialized" state between the two classloaders. Serialize one instance to a byte array and reconstruct the object in the other classloader by de-serializing the object stream.
like image 90
Sanjay T. Sharma Avatar answered Nov 19 '22 11:11

Sanjay T. Sharma


Reflection isn't that bad, and is appropriate here.
Is this a Maven plugin, BTW?

You'll want something like:

Mojo mojo = (Mojo)descriptor.getClass().getMethod("getMojo").invoke(descriptor);

I'm leaving out a lot - particularly exception handling - but this should lead you to the Javadoc you need. It's quite good, but read carefully.

If you also have two Mojo classes, the cast will break, and you'll have to do more reflection to do whatever you need to do with the evil-twin Mojo.

like image 40
Ed Staub Avatar answered Nov 19 '22 13:11

Ed Staub


I think better option to just store byte array instead of object. While deserliazing, get byte array back and convert into object.

I had the same issue and byte array approach worked.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(cachedValue);
        byte b[] = bos.toByteArray();

        //Store in DB, file wherever here using b[]. I am not writing code to store it as it may vary in your requirement.

    } catch (IOException e) {
        e.printStackTrace();
    }

Read from byte array:

ByteArrayInputStream bis = new ByteArrayInputStream(<<read byte[] where you stored earlier>>);
    ObjectInput in = null;

    try {
        in = new ObjectInputStream(bis);
        <Your Class >cachedRes = ( Your Class) in.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
like image 38
user3155799 Avatar answered Nov 19 '22 13:11

user3155799