Skip to content

Commit b3af4e7

Browse files
committed
Fixed issue with inner classes
1 parent 1346c6e commit b3af4e7

3 files changed

Lines changed: 42 additions & 16 deletions

File tree

src/main/java/org/mdkt/compiler/ExtendedStandardJavaFileManager.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
import java.io.FileNotFoundException;
44
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
58

69
import javax.tools.FileObject;
710
import javax.tools.ForwardingJavaFileManager;
811
import javax.tools.JavaFileManager;
912
import javax.tools.JavaFileObject;
1013

1114
/**
12-
* Created by trung on 5/3/15.
13-
* Edited by turpid-monkey on 9/25/15, completed support for multiple compile units.
15+
* Created by trung on 5/3/15. Edited by turpid-monkey on 9/25/15, completed
16+
* support for multiple compile units.
1417
*/
1518
public class ExtendedStandardJavaFileManager extends
1619
ForwardingJavaFileManager<JavaFileManager> {
1720

18-
private CompiledCode[] compiledCode;
21+
private List<CompiledCode> compiledCode = new ArrayList<CompiledCode>();
1922
private DynamicClassLoader cl;
2023

2124
/**
@@ -26,23 +29,27 @@ public class ExtendedStandardJavaFileManager extends
2629
* @param cl
2730
*/
2831
protected ExtendedStandardJavaFileManager(JavaFileManager fileManager,
29-
DynamicClassLoader cl, CompiledCode... compiledCode) {
32+
DynamicClassLoader cl) {
3033
super(fileManager);
31-
this.compiledCode = compiledCode;
3234
this.cl = cl;
33-
for (CompiledCode code : compiledCode) {
34-
this.cl.addCode(code);
35-
}
3635
}
3736

3837
@Override
39-
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
40-
for (CompiledCode code : compiledCode)
41-
{
42-
if (code.getClassName().equals(className)) return code;
43-
}
44-
throw new FileNotFoundException("Missing source code for class " + className );
45-
}
38+
public JavaFileObject getJavaFileForOutput(
39+
JavaFileManager.Location location, String className,
40+
JavaFileObject.Kind kind, FileObject sibling) throws IOException {
41+
42+
try {
43+
CompiledCode innerClass = new CompiledCode(className);
44+
compiledCode.add(innerClass);
45+
cl.addCode(innerClass);
46+
return innerClass;
47+
} catch (Exception e) {
48+
throw new RuntimeException(
49+
"Error while creating in-memory output file for "
50+
+ className, e);
51+
}
52+
}
4653

4754
@Override
4855
public ClassLoader getClassLoader(JavaFileManager.Location location) {

src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.mdkt.compiler;
22

3+
import java.io.IOException;
34
import java.util.Collection;
45
import java.util.HashMap;
56
import java.util.Iterator;
67
import java.util.Map;
78

9+
import javax.tools.Diagnostic;
10+
import javax.tools.DiagnosticListener;
811
import javax.tools.JavaCompiler;
12+
import javax.tools.JavaFileObject;
913
import javax.tools.ToolProvider;
1014

1115
/**
@@ -49,7 +53,7 @@ public Map<String, Class<?>> compileAll() throws Exception {
4953
}
5054

5155
ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(
52-
javac.getStandardFileManager(null, null, null), classLoader, code);
56+
javac.getStandardFileManager(null, null, null), classLoader);
5357
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager,
5458
null, null, null, compilationUnits);
5559
boolean result = task.call();

src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ public void compile_severalFiles() throws Exception {
4242
Object a = aClass.newInstance();
4343
Assert.assertEquals("B!", aClass.getMethod("b").invoke(a).toString());
4444
}
45+
46+
@Test
47+
public void compile_filesWithInnerClasses() throws Exception {
48+
StringBuffer sourceCode = new StringBuffer();
49+
50+
sourceCode.append("package org.mdkt;\n");
51+
sourceCode.append("public class HelloClass {\n");
52+
sourceCode.append(" private static class InnerHelloWorld { int inner; }\n");
53+
sourceCode.append(" public String hello() { return \"hello\"; }");
54+
sourceCode.append("}");
55+
56+
Class<?> helloClass = InMemoryJavaCompiler.compile("org.mdkt.HelloClass", sourceCode.toString());
57+
Assert.assertNotNull(helloClass);
58+
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
59+
}
4560
}

0 commit comments

Comments
 (0)