trung / InMemoryJavaCompiler

Utility class to compile java source code in memory
Apache License 2.0
267 stars 85 forks source link

InMemoryJavaCompiler.compile() returns reference to old class when compiling a new class with the same name #16

Open drdrwhite opened 6 years ago

drdrwhite commented 6 years ago

If I ask InMemoryJavaCompiler to compile a new class with the same name as an existing class, the compile() method returns a reference to the existing class.

Minimal example across two files, both in the default package, compiled and loaded on the classpath:

Example.java:

public class Example {
}

MinimalExample.java:

import org.mdkt.compiler.InMemoryJavaCompiler;

public class MinimalExample {

    public static void main(String args[]) throws Exception {

        Class<?> oldClass = Example.class;

        InMemoryJavaCompiler compiler = InMemoryJavaCompiler.newInstance();
        Class<?> newClass = compiler.compile("Example", "public class Example {\n }\n");

        if (oldClass.hashCode() == newClass.hashCode()) {
            System.out.println("I have two copies of the old class.");
        }

    }

}

Expected behaviour: the class returned by compile should be the result of compiling the string passed to it.

I think this is a bug.

Thanks very much for providing this library! I am very keen to use it in Gin :-)

drdrwhite commented 6 years ago

Please could we get this pushed to Maven?

umeshshende commented 3 years ago

Any update on this im facing same issue.