Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic runtime compilation with custom classes #73

Open
joanbonilla opened this issue Nov 27, 2018 · 13 comments
Open

Dynamic runtime compilation with custom classes #73

joanbonilla opened this issue Nov 27, 2018 · 13 comments

Comments

@joanbonilla
Copy link

Hi,

I have a spring boot application with some custom code and I've tried to compile that sample with a custom class inside.

Can I compile dynamic classes with my custom code?

Expected behavior and actual behavior:

Expected: show the "Hello World!" message

Actual:

I get the next error (classloader):

Compilation error: /com/sample/soc/RuntimeCompilerTest.java:1: error: package com.sample.spring does not exist\r\npackage com.sample.soc; import com.sample.spring.WebsitesBO; class RuntimeCompilerTest implements java.util.function.Supplier { public String get() { return "Hello World!"; } }\r\n ^\r\n1 error\r\n

Steps to reproduce the problem:

Create package com.sample.spring and create Java class in this package named WebsitesBO

Copy the next code and run it:

Supplier<String> supplier = Reflect.compile(
            "com.sample.soc.RuntimeCompilerTest",
            "package com.sample.soc; " +
                    "import com.sample.spring.WebsitesBO; " +
                    "class RuntimeCompilerTest implements java.util.function.Supplier<String> { " +
                    "public String get() { " +
                    "return \"Hello World!\"; } " +
                    "}"
    ).create().get();

Versions:

  • jOOR: 0.9.8 and 0.9.9
  • Java: 8

Thank you

@lukaseder
Copy link
Member

Thanks for your report and for creating the issue :)

It should definitely work (see also some tests here: https://github.com/jOOQ/jOOR/blob/master/jOOR/src/test/java/org/joor/test/CompileTest.java), but is the com.sample.spring package visible to the code that invokes the compile method?

@lukaseder
Copy link
Member

Can you create a test case to help reproduce this?

@joanbonilla
Copy link
Author

You can reproduce it with this repo: https://github.com/joanbonilla/dynamic-compiler

Run it with mvn spring-boot:run

@lukaseder
Copy link
Member

I see, thanks for the test case. Very interesting. When I run the class in Eclipse, it works. It doesn't work from whatever the spring-boot:run goal is doing. This seems to be some sort of a class loader issue.

@lukaseder
Copy link
Member

Does this issue fix it? #65

@lukaseder
Copy link
Member

Tried it myself. #65 doesn't seem to fix it

@joanbonilla
Copy link
Author

I solved the issue with some tips in your library about the classloader, I needed to use the current Classloader

@joanbonilla
Copy link
Author

Do you want the code I just implemented in a new branch?

joanbonilla pushed a commit to joanbonilla/jOOR that referenced this issue Nov 28, 2018
@lukaseder
Copy link
Member

Do you want the code I just implemented in a new branch?

Is that it?
joanbonilla@34f2379

The solution would be something similar, but the ClassLoader reference would go into the CompileOptions argument type. That's why it was added - because I don't want to overload new methods every time a new compile option is being considered.

@joanbonilla
Copy link
Author

Are you using CompileOptions as context pattern object?

@lukaseder
Copy link
Member

I don't know what a "context pattern object" is

@joanbonilla
Copy link
Author

I did another branch:

joanbonilla@2d97d14

@lukaseder
Copy link
Member

Thanks. I've had a look at this when taking an attempt at implementing #72. Your code will affect the JDK 8 distribution, but what about JDK 9+? How can we define a class on an existing class loader when we're no longer allowed to "open up" the ClassLoader.defineClass() method through reflection? Lookup can be used, but it is much more difficult to get right...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants