diff --git a/testable-core/src/main/java/com/alibaba/testable/core/compile/InMemoryJavaCompiler.java b/testable-core/src/main/java/com/alibaba/testable/core/compile/InMemoryJavaCompiler.java index fa259b3..0569107 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/compile/InMemoryJavaCompiler.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/compile/InMemoryJavaCompiler.java @@ -69,9 +69,7 @@ public class InMemoryJavaCompiler { throw new CompilationException("No source code to compile"); } Collection compilationUnits = sourceCodes.values(); - CompiledCode[] code; - - code = new CompiledCode[compilationUnits.size()]; + CompiledCode[] code = new CompiledCode[compilationUnits.size()]; Iterator iter = compilationUnits.iterator(); for (int i = 0; i < code.length; i++) { code[i] = new CompiledCode(iter.next().getClassName()); @@ -81,7 +79,7 @@ public class InMemoryJavaCompiler { JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, options, null, compilationUnits); boolean result = task.call(); if (!result || collector.getDiagnostics().size() > 0) { - StringBuffer exceptionMsg = new StringBuffer(); + StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append("Unable to compile the source"); boolean hasWarnings = false; boolean hasErrors = false; diff --git a/testable-core/src/main/java/com/alibaba/testable/core/compile/README.md b/testable-core/src/main/java/com/alibaba/testable/core/compile/README.md index a2f284d..907c795 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/compile/README.md +++ b/testable-core/src/main/java/com/alibaba/testable/core/compile/README.md @@ -1,4 +1,4 @@ -This part of code is under [Apache License Version 2.0](LICENSE), modified from [InMemoryJavaCompiler](https://github.com/trung/InMemoryJavaCompiler) with several PRs merged and other refactorings (e.g. dropping unnecessary dependence and avoid throwing raw Exception). +This part of code is under [Apache License Version 2.0](LICENSE), modified from [InMemoryJavaCompiler](https://github.com/trung/InMemoryJavaCompiler) with modification (e.g. dropping unnecessary dependence and avoid throwing raw Exception). Many thanks to the origin author and contributors of this lovely library: - [trung](https://github.com/trung) diff --git a/testable-core/src/main/java/com/alibaba/testable/core/compile/SourceCode.java b/testable-core/src/main/java/com/alibaba/testable/core/compile/SourceCode.java index e882e15..7688e3f 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/compile/SourceCode.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/compile/SourceCode.java @@ -1,17 +1,16 @@ package com.alibaba.testable.core.compile; import javax.tools.SimpleJavaFileObject; -import java.io.IOException; import java.net.URI; /** * Created by trung on 5/3/15. */ public class SourceCode extends SimpleJavaFileObject { - private String contents = null; - private String className; + private final String contents; + private final String className; - public SourceCode(String className, String contents) throws Exception { + public SourceCode(String className, String contents) { super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); this.contents = contents; @@ -22,8 +21,8 @@ public class SourceCode extends SimpleJavaFileObject { return className; } - public CharSequence getCharContent(boolean ignoreEncodingErrors) - throws IOException { + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { return contents; } } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/ConstructionUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/ConstructionUtil.java index 78d3aa3..3d98b4d 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/ConstructionUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/ConstructionUtil.java @@ -4,25 +4,30 @@ import com.alibaba.testable.core.compile.InMemoryJavaCompiler; import java.lang.reflect.Method; +import static com.alibaba.testable.core.constant.ConstPool.DOLLAR; +import static com.alibaba.testable.core.constant.ConstPool.DOT; + public class ConstructionUtil { private static final String TESTABLE_IMPL = "$TestableImpl"; - private static final String TESTABLE_OMNI_PKG = "com.alibaba.testable.omni"; public static T generateSubClassOf(Class clazz) throws InstantiationException { StringBuilder sourceCode = new StringBuilder(); - sourceCode.append("package ").append(TESTABLE_OMNI_PKG).append(";\n") + sourceCode.append("package ").append(clazz.getPackage().getName()).append(";\n") .append("public class ").append(getSubclassName(clazz)) - .append(clazz.isInterface() ? " implements " : " extends ").append(clazz.getName()).append(" {\n"); + .append(clazz.isInterface() ? " implements " : " extends ") + .append(clazz.getName().replace(DOLLAR, DOT)) + .append(" {\n"); for (Method m : clazz.getMethods()) { } sourceCode.append("}"); try { - Class helloClass = InMemoryJavaCompiler.newInstance() - .compile(TESTABLE_OMNI_PKG + "." + getSubclassName(clazz), sourceCode.toString()); - return (T) helloClass.newInstance(); + return (T) InMemoryJavaCompiler.newInstance() + .useParentClassLoader(clazz.getClassLoader()) + .compile(clazz.getPackage().getName() + DOT + getSubclassName(clazz), sourceCode.toString()) + .newInstance(); } catch (Exception e) { throw new InstantiationException(e.toString()); }