A method for conditionally sharing dynamically compiled code between
different class loaders is provided. In this method, bytecodes of a first
class type are compiled into a compiled code if the bytecodes have not
been compiled. The locations of sequence of instructions in the compiled
code that are loader dependent are then recorded. Thereafter, the loader
dependencies in the compiled code are compared to determine whether the
compiled code can be used by a method of a second class type. If the
loader dependencies of the second class type are different from the
loader dependencies of the first class type, the compiled code for the
method of the first class type is cloned. Subsequently, portions of the
cloned code that are loader dependent are modified to enable the method
of the second class type to use the cloned code.