Skip to content

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

Open
@drdrwhite

Description

@drdrwhite

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions