Search code examples
javaosgiconcept

How OSGi solves the task of casting bundle-defined classes to rt.jar-defined classes?


How OSGi solves the problem1 of casting classes which were loaded from bundles with OSGi custom classloader to classes loaded from rt.jar? Does rt.jar loaded with custom classloader too, instead of system classloader?

UPD1

It appears that i canot load most parts of rt.jar with custom classloader, because of the following code in the ClassLoader.java:

private ProtectionDomain preDefineClass(String name, ProtectionDomain protectionDomain) {
    ...
    if ((name != null) && name.startsWith("java.")) {
        throw new SecurityException("Prohibited package name: " +
        name.substring(0, name.lastIndexOf('.')));
    }
    ...
}

[1] the problem is in the following: classes loaded with different classloaders are treated by jvm as entirely different ones even if their byte-code is exactly the same, so when, for example, we have class rt.jar!/SomeClass and derived it inside of bundle.jar!/SomeClassChild, we cannot cast SomeClassChild to SomeClass if they were loaded by different classloaders.


Solution

  • There is no such problem in Java. Certainly not as stated in the question. It is true that classes with the same name loaded by different classloaders are never treated as the same class, but from this it does not follow that a superclass has to be loaded by the same classloader as a subclass.

    Actually since all java.* classes, including Object, come from the bootstrap classloader and no user class in an application is loaded by the bootstrap classloader, it would be impossible to even cast anything to Object, ever, way before OSGi came into the picture.