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

Reuse classes from different compilation units #100

Open
lburgazzoli opened this issue Apr 15, 2020 · 4 comments
Open

Reuse classes from different compilation units #100

lburgazzoli opened this issue Apr 15, 2020 · 4 comments

Comments

@lburgazzoli
Copy link

Expected behavior and actual behavior:

Assuming I have some code like:

String data = Files.readString(Path.of("data/MyData.java"));
String route = Files.readString(Path.of("data/MyRoutes.java"));

Reflect.compile("my.example.MyData", data).create().get();
Reflect.compile("my.example.MyRoutes", route).create().get();

Where the class MyData is needed by MyRoutes in this case for unmarshalling from json, then the code above fails with:

org.joor.ReflectException: Compilation error: /my/example/MyRoutes.java:26: error: cannot find symbol
            .marshal().json(JsonLibrary.Jackson, MyData.class)
                                                 ^
  symbol:   class MyData
  location: class MyRoutes

I don't know if this is supported by jOOR.

Steps to reproduce the problem:

  1. clone https://github.com/lburgazzoli/camel-joor-test
  2. mvn compile exec:java

Versions:

  • jOOR: 0.9.12
  • Java: 11
@lukaseder
Copy link
Member

Thanks for your suggestion.

I'm not sure how we can easily add compilation products on the classpath for subsequent compilations. This seems to be a can of worms I'm not convinced we should open automatically.

However, perhaps there's a way to achieve this through CompileOptions, manually. We pass along the -classpath flag to the compiler. That flag is file based, listing some jar files. I'm not sure if it's possible to list individual classes and/or class loaders?

I'm happy to review specific suggestions, but cannot spend time on investigations of this topic, right now.

@lburgazzoli
Copy link
Author

I'm not sure how we can easily add compilation products on the classpath for subsequent compilations. This seems to be a can of worms I'm not convinced we should open automatically.

Yeah I have realized that while writing the issue and I do agree that it should not be done open by default.

I'm happy to review specific suggestions, but cannot spend time on investigations of this topic, right now.

Wonder if something like:

String data  = Files.readString(Path.of("data/MyData.java"));
String route = Files.readString(Path.of("data/MyRoutes.java"));

var unit   = Reflect.compilationUnit(data, route);
var result = unit.onClass("my.example.MyRoutes").create().get();

could be acceptable

@lukaseder
Copy link
Member

That's a different suggestion, which I can see more realistically as it is 1) indeed a missing feature, 2) not raising more questions than it answers :) I've created a new feature request for this: #101

I was not aware that this was an option for you from your original feature request, where it may be possible that the two classes are not compiled at the same time.

Of course, if you are in full control of your classes, and when you want to compile them, then you can also nest the MyData class in MyRoutes and get things to work already now.

@lburgazzoli
Copy link
Author

Yeah, the original requirement was based on the fact that the classes are compiled at different times, but after thinking a little bit more about my current set-up I can easily collect and compile them in a single step :)

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