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.81k stars 376 forks source link

Error of code with JSON #58

Closed ghost closed 6 years ago

ghost commented 6 years ago

Expected behavior and actual behavior:

  1. To run the code from a json and compile it.

    Exception in thread "main" org.joor.ReflectException: Compilation error: /io/github/xavierdd1st/TimeViewer.java:1: error: class, interface, or enum expected
    nullpackage io.github.xavierdd1st.commands;import java.time.Clock;import java.time.ZoneId;public class TimeViewer extends Command {public void command() {Clock clock = Clock.system(ZoneId.systemDefault());CommandParser.setOutput( clock.toString() );}}
    ^
    1 error
    
    at org.joor.Compile.compile(Compile.java:65)
    at org.joor.Reflect.compile(Reflect.java:77)
    at io.github.xavierdd1st.commands.CommandParser.parseCode(CommandParser.java:22)
    at io.github.xavierdd1st.commands.CommandHandler.setup(CommandHandler.java:16)
    at io.github.xavierdd1st.Main.<init>(Main.java:21)
    at io.github.xavierdd1st.Main.main(Main.java:35)

    Steps to reproduce the problem:

    Run the project. https://github.com/XavierDD1st/TBUA

    Versions:

    • jOOR: 0.9.9
    • Java: 8
lukaseder commented 6 years ago

Thanks for reporting.

I'm looking at the relevant code in your class that calls Reflect.compile():

public class CommandParser {
    private HashMap<String, Command> compiledCode = new HashMap();
    private HashMap<String, Reflect> compiledClasses = new HashMap();
    private static String output;
    String code;

    public void parseCode(Command command) {
        if (!this.compiledCode.containsKey(command.getInstanceID())) {

            for (int i = 0; i < command.getCode().length; i++) {
                code = code + command.getCode()[i];
            }
            Supplier<String> supplier = Reflect.compile(
                    "io.github.xavierdd1st." + command.getName(),
                    code).create().get();
            this.compiledClasses.put(command.getInstanceID(), (Reflect) supplier);
            this.compiledCode.put(command.getInstanceID(), command);
        }
    }

Notice how you did not initialise your String code member to an empty string, thus when you concatenate some code to it, there's going to be "null" at the beginning of your code, as you can see in the first statement of the error message:

nullpackage io.github.xavierdd1st.commands;

Hope this helps

ghost commented 6 years ago

Thanks a lot I never experimented with things like that.