trung / InMemoryJavaCompiler

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

className/Class<?> pairings not necessarily same as className/sourceCode #17

Open kaya3 opened 6 years ago

kaya3 commented 6 years ago

Calling addSource("A", "class B {}"); addSource("B", "class A {}"); compileAll(); returns a Map of "A" to the class A and "B" to the class B. This is unexpected, and means that compileAll().get(className) won't necessarily return the class compiled from the source code for className.

This could cause problems if, for example, client code ensures that the source code for "A" declares a class with some property, and then expects compileAll().get("A") to return a class with that property.

A suggested fix is for the compileAll() method to check that the source code associated with each class name does declare a class of that name, and throw a CompilationException otherwise. This mirrors the behaviour of javac which requires a file named A.java to declare a class named A.