Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fixed issue with inner classes
  • Loading branch information
turpid-monkey committed Nov 7, 2015
commit b3af4e740ba0bc267066ce6986b3a179ce62038d
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;

/**
* Created by trung on 5/3/15.
* Edited by turpid-monkey on 9/25/15, completed support for multiple compile units.
* Created by trung on 5/3/15. Edited by turpid-monkey on 9/25/15, completed
* support for multiple compile units.
*/
public class ExtendedStandardJavaFileManager extends
ForwardingJavaFileManager<JavaFileManager> {

private CompiledCode[] compiledCode;
private List<CompiledCode> compiledCode = new ArrayList<CompiledCode>();
private DynamicClassLoader cl;

/**
Expand All @@ -26,23 +29,27 @@ public class ExtendedStandardJavaFileManager extends
* @param cl
*/
protected ExtendedStandardJavaFileManager(JavaFileManager fileManager,
DynamicClassLoader cl, CompiledCode... compiledCode) {
DynamicClassLoader cl) {
super(fileManager);
this.compiledCode = compiledCode;
this.cl = cl;
for (CompiledCode code : compiledCode) {
this.cl.addCode(code);
}
}

@Override
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
for (CompiledCode code : compiledCode)
{
if (code.getClassName().equals(className)) return code;
}
throw new FileNotFoundException("Missing source code for class " + className );
}
public JavaFileObject getJavaFileForOutput(
JavaFileManager.Location location, String className,
JavaFileObject.Kind kind, FileObject sibling) throws IOException {

try {
CompiledCode innerClass = new CompiledCode(className);
compiledCode.add(innerClass);
cl.addCode(innerClass);
return innerClass;
} catch (Exception e) {
throw new RuntimeException(
"Error while creating in-memory output file for "
+ className, e);
}
}

@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.mdkt.compiler;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;

/**
Expand Down Expand Up @@ -49,7 +53,7 @@ public Map<String, Class<?>> compileAll() throws Exception {
}

ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(
javac.getStandardFileManager(null, null, null), classLoader, code);
javac.getStandardFileManager(null, null, null), classLoader);
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager,
null, null, null, compilationUnits);
boolean result = task.call();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public void compile_severalFiles() throws Exception {
Object a = aClass.newInstance();
Assert.assertEquals("B!", aClass.getMethod("b").invoke(a).toString());
}

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

sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" private static class InnerHelloWorld { int inner; }\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");

Class<?> helloClass = InMemoryJavaCompiler.compile("org.mdkt.HelloClass", sourceCode.toString());
Assert.assertNotNull(helloClass);
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
}
}