Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #16 #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Map<String, Class<?>> compileAll() throws Exception {

Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
for (String className : sourceCodes.keySet()) {
classes.put(className, classLoader.loadClass(className));
classes.put(className, classLoader.findClass(className));
}
return classes;
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/mdkt/compiler/HelloClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.mdkt.compiler;

public class HelloClass {

public String hello() {
return "hello";
}
}
26 changes: 26 additions & 0 deletions src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mdkt.compiler;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -30,6 +31,29 @@ public void compile_WhenTypical() throws Exception {
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
}

@Test
public void compile_WhenTypicalUpdateClass() throws Exception {
StringBuffer sourceCode = new StringBuffer();

sourceCode.append("package org.mdkt.compiler;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello1\"; }");
sourceCode.append("}");

Class<?> oldClass = HelloClass.class;
Class<?> newClass = InMemoryJavaCompiler.newInstance().compile("org.mdkt.compiler.HelloClass", sourceCode.toString());

Assert.assertNotEquals(oldClass.hashCode() , newClass.hashCode());
Assert.assertNotEquals(oldClass.getDeclaredMethod("hello").invoke(oldClass.newInstance()) ,
newClass.getDeclaredMethod("hello").invoke(newClass.newInstance()));





}


@Test
public void compileAll_WhenTypical() throws Exception {
String cls1 = "public class A{ public B b() { return new B(); }}";
Expand Down Expand Up @@ -114,4 +138,6 @@ public void compile_WhenWarningsAndErrors() throws Exception {
throw e;
}
}


}