mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
feat: use same package path of parent to avoid access error
This commit is contained in:
@@ -69,9 +69,7 @@ public class InMemoryJavaCompiler {
|
||||
throw new CompilationException("No source code to compile");
|
||||
}
|
||||
Collection<SourceCode> compilationUnits = sourceCodes.values();
|
||||
CompiledCode[] code;
|
||||
|
||||
code = new CompiledCode[compilationUnits.size()];
|
||||
CompiledCode[] code = new CompiledCode[compilationUnits.size()];
|
||||
Iterator<SourceCode> 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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> T generateSubClassOf(Class<T> 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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user