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

ReflectException during runtime compilation within a fatjar #107

Closed SrinSS01 closed 3 years ago

SrinSS01 commented 4 years ago

Expected behavior and actual behavior:

Expected to compile a class mentioned in Reflect.compile(...) Actual behaviour: throwing ReflectException whenever the pice of code is executed

Steps to reproduce the problem:

I was using JOOR in my Minecraft plugin, This is all I did: Create a method which returns Reflect.compile(...).create(...).get(); Build the plugin and fatJar JOOR in it Run the plugin in Minecraft server and you'll get the exception

Versions:

lukaseder commented 3 years ago

I'm not acquainted with Minecraft plugin development, or how to create a "fatjar plugin" for Minecraft. I would need some help reproducing this issue, but here's what I'd like you to check beforehand:

Does your target environment really support compiling Java code on the fly? jOOQ doesn't do anything on its own here, but delegates to the java.compiler module, which is part of the JDK, but perhaps not of your runtime environment?

What's the exception you were getting?

lukaseder commented 3 years ago

This may be related: https://github.com/jOOQ/jOOR/issues/69#issuecomment-687265360

SrinSS01 commented 3 years ago

So apparently this happens for any jar files, Here's an example

import org.joor.Reflect;
public class Main {
    public static void main(String[] args){
        Reflect.compile("Name",
                "public class Name{" +
                        "   private String s;" +
                        "   public Name(String s){" +
                        "       this.s=s;" +
                        "   }" +
                        "   void printString(){" +
                        "       System.out.println(\"Hi! \"+this.s);" +
                        "   }" +
                        "}"
        ).create("Srinjoy").call("printString").get();
    }
}

and this is the exception

$ java -jar /c/Users/Srinj/Desktop/JarjOOR.jar
Exception in thread "main" org.joor.ReflectException: Error while compiling Name
        at org.joor.Compile.compile(Compile.java:158)
        at org.joor.Reflect.compile(Reflect.java:104)
        at Main.main(Main.java:12)
Caused by: java.lang.NullPointerException
        at org.joor.Compile.compile(Compile.java:66)
        ... 2 more
lukaseder commented 3 years ago

Thanks for your update. That line is this one: https://github.com/jOOQ/jOOR/blob/version-0.9.13/jOOR/src/main/java/org/joor/Compile.java#L66

The only thing that can be null is compiler, which is null, according to the ToolProvider.getSystemJavaCompiler() Javadoc if:

This implementation returns the compiler providedby the jdk.compiler module if that module is available,and null otherwise.

We could throw a better exception than a generic NPE of course (will fix right away: https://github.com/jOOQ/jOOR/issues/108), but you'd still have to make sure a compiler is available at runtime by pulling in the java.compiler module from the JDK (e.g. it doesn't ship with the JRE)