lundi 5 juin 2017

JavaCompiler inside JAR executable

I have quite a bit of problems with using JavaCompiler inside application that was started from JAR.
Basically, we we would like that application to have possibility of adding/removing/editing some logic remotely.
I have followed up few tutorials to implement in memory-compiler for Java (for example this) and it's working just fine when I'm starting application from IDE (Intelij IDEA), but it throws weird class-cast exception when I starting it from JAR executable.
Let's consider I have following interface:

interface A {
    boolean doStuff(B b, C c);
}

Then I'm generating custom class from remote code like:

imports
class Custom implements A {
     boolean doStuff(B b, C c) {
         return b.d > 100;
     }
}

And it's compiles just fine, but when I'm trying to cast compiled object to my interface A like this:

final ClassLoader inMemoryClassLoader = createClassLoader(byteObject);
Class<A> test = (Class<A>) inMemoryClassLoader.loadClass(name);
return test.newInstance();

It throws exception: "Class 'class_name' cannot be cast to A" (when I starts if from IDE it's working just fine, no exceptions). I already tried a lot of tutorials and reviewed few similar issues (like this), but with no luck so forth.
Obviously, I can call 'doStuff' with reflection and change parameters from my model classes to unwrapped primitive arguments, but I would rather avoid that.
Some additional information that might help:

  1. Framework - Spring Boot
  2. Java Version - Java 1.8.103
  3. Target system - Ubuntu 16.04 x64
  4. I have looked up thru reflection which interfaces implements compiled class. Interface A is in the list and this is confusing me a lot

I would highly appreciate any suggestions or ideas how it can be resolved!





Aucun commentaire:

Enregistrer un commentaire