jOOQ / jOOR

jOOR - Fluent Reflection in Java jOOR is a very simple fluent API that gives access to your Java Class structures in a more intuitive way. The JDK's reflection APIs are hard and verbose to use. Other languages have much simpler constructs to access type meta information at runtime. Let us make Java reflection better.
http://www.jooq.org/products
Apache License 2.0
2.8k stars 377 forks source link

Reuse classes from different compilation units #100

Open lburgazzoli opened 4 years ago

lburgazzoli commented 4 years ago

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:

lukaseder commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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: https://github.com/jOOQ/jOOR/issues/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 commented 4 years ago

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 :)