Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create an URLClassLoader to load your newly compiled classes, or you can have a look at a library I wrote which will compile in memory and load into the current class loader by default.</p> <p><a href="http://vanillajava.blogspot.co.uk/2010/11/more-uses-for-dynamic-code-in-java.html" rel="nofollow">http://vanillajava.blogspot.co.uk/2010/11/more-uses-for-dynamic-code-in-java.html</a></p> <p>If you have generated code, it will save the file to a source directory when debugging so you can step into the generated code (otherwise it does everything in memory)</p> <p>You can only load a class once this way so if you need to load many versions I suggest you implement an interface and change the name of the class each time.</p> <pre><code>// this writes the file to disk only when debugging is enabled. CachedCompiler cc = CompilerUtils.DEBUGGING ? new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) : CompilerUtils.CACHED_COMPILER; String text = "generated test " + new Date(); Class fooBarTeeClass = cc.loadFromJava("eg.FooBarTee", "package eg;\n" + '\n' + "import eg.components.BarImpl;\n" + "import eg.components.TeeImpl;\n" + "import eg.components.Foo;\n" + '\n' + "public class FooBarTee{\n" + " public final String name;\n" + " public final TeeImpl tee;\n" + " public final BarImpl bar;\n" + " public final BarImpl copy;\n" + " public final Foo foo;\n" + '\n' + " public FooBarTee(String name) {\n" + " // when viewing this file, ensure it is synchronised with the copy on disk.\n" + " System.out.println(\"" + text + "\");\n" + " this.name = name;\n" + '\n' + " tee = new TeeImpl(\"test\");\n" + '\n' + " bar = new BarImpl(tee, 55);\n" + '\n' + " copy = new BarImpl(tee, 555);\n" + '\n' + " // you should see the current date here after synchronisation.\n" + " foo = new Foo(bar, copy, \"" + text + "\", 5);\n" + " }\n" + '\n' + " public void start() {\n" + " }\n" + '\n' + " public void stop() {\n" + " }\n" + '\n' + " public void close() {\n" + " stop();\n" + '\n' + " }\n" + "}\n"); // add a debug break point here and step into this method. FooBarTee fooBarTee = new FooBarTee("test foo bar tee"); Foo foo = fooBarTee.foo; assertNotNull(foo); assertEquals(text, foo.s); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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