Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A class at runtime is identified by both its fully qualified name and its ClassLoader.</p> <p>For example, when you test two <code>Class&lt;T&gt;</code> objects for equality, if they have the same canonical name but were loaded from different ClassLoaders, they won't be equal.</p> <p>For two classes to belong to the same package (and in turn being able to access package-private methods), they need to be loaded from the same ClassLoader too, which is not the case here. In fact <code>Test1</code> is loaded by the system classloader, while the Formula is loaded by the URLClassLoader created inside <code>loadClass()</code>. </p> <p>If you specify a parent loader for your URLClassLoader in order to make it load <code>Test1</code>, still two different loaders are used (you can check it by asserting loaders equality).</p> <p>I don't think you can make the <code>Formula</code> class loaded by the same <code>Test1</code> ClassLoader (you'd have to use a well-known path and put it on the CLASSPATH), but I found a way to do the opposite: loading another instance of <code>Test1</code> in the ClassLoader used for loading the formula. This is the layout in pseudocode:</p> <pre><code>class Test1 { public static void main(String... args) { loadClass(formula); } static void loadClass(location) { ClassLoader loader = new ClassLoader(); Class formula = loader.load(location); Class test1 = loader.load(Test1); // ... Method compute = test1.getMethod("compute"); compute.invoke(test1, formula); } static void compute(formula) { print formula; } } </code></pre> <p>Here is the <a href="http://pastebin.com/eN5DyBrC" rel="noreferrer">pastebin</a>. A couple of notes: I specifed a <code>null</code> parent for the URLClassLoader to avoid the issue listed above, and I manipulated strings to achieve the purpose - but don't know how robust this approach can be in other deployment scenarios. Also, the URLCLassLoader I used only searches in two directories to find class definitions, not all the entries listed in the CLASSPATH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload