mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-25 11:51:15 +08:00
move testable agent generation to EnableTestableProcessor
This commit is contained in:
parent
0f007c6d96
commit
e51da09824
@ -3,7 +3,6 @@ package com.alibaba.testable.processor;
|
||||
import com.alibaba.testable.annotation.EnableTestableInject;
|
||||
import com.alibaba.testable.translator.EnableTestableInjectTranslator;
|
||||
import com.alibaba.testable.translator.MethodRecordTranslator;
|
||||
import com.alibaba.testable.util.ResourceUtil;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
@ -13,10 +12,6 @@ import javax.annotation.processing.SupportedSourceVersion;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -26,15 +21,9 @@ import java.util.Set;
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_7)
|
||||
public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
|
||||
private static final String AGENT_TARGET_FOLDER = "";
|
||||
private static final String AGENT_TARGET_FILE = "testable.jar";
|
||||
private static final String AGENT_SOURCE_FILE = "testable-agent.jar";
|
||||
private static boolean hasFirstClassCompiled = false;
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EnableTestableInject.class);
|
||||
createStaticNewClass();
|
||||
for (Element element : elements) {
|
||||
if (element.getKind().isClass()) {
|
||||
processClassElement((Symbol.ClassSymbol)element);
|
||||
@ -43,24 +32,6 @@ public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void createStaticNewClass() {
|
||||
if (!checkFirstClassCompiled()) {
|
||||
byte[] bytes = ResourceUtil.fetchBinary(AGENT_SOURCE_FILE);
|
||||
if (bytes.length == 0) {
|
||||
cx.logger.error("Failed to generate testable agent jar");
|
||||
}
|
||||
writeBinaryFile(AGENT_TARGET_FOLDER, AGENT_TARGET_FILE, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkFirstClassCompiled() {
|
||||
if (!hasFirstClassCompiled) {
|
||||
hasFirstClassCompiled = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void processClassElement(Symbol.ClassSymbol clazz) {
|
||||
JCTree tree = cx.trees.getTree(clazz);
|
||||
MethodRecordTranslator methodRecordTranslator = new MethodRecordTranslator();
|
||||
@ -68,17 +39,4 @@ public class EnableTestableInjectProcessor extends BaseProcessor {
|
||||
tree.accept(new EnableTestableInjectTranslator(cx, methodRecordTranslator.getMethods()));
|
||||
}
|
||||
|
||||
private void writeBinaryFile(String path, String fileName, byte[] content) {
|
||||
try {
|
||||
FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName);
|
||||
OutputStream out = resource.openOutputStream();
|
||||
out.write(content);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
cx.logger.error("Failed to write " + fileName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.alibaba.testable.processor;
|
||||
|
||||
import com.alibaba.testable.annotation.EnableTestable;
|
||||
import com.alibaba.testable.translator.EnableTestableTranslator;
|
||||
import com.alibaba.testable.util.ResourceUtil;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
@ -12,6 +13,10 @@ import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.Name;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -21,8 +26,12 @@ import java.util.Set;
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_7)
|
||||
public class EnableTestableProcessor extends BaseProcessor {
|
||||
|
||||
private static final String TESTABLE_AGENT_JAR = "testable-agent.jar";
|
||||
private static boolean hasFirstClassCompiled = false;
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
createTestableAgentJar();
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(EnableTestable.class);
|
||||
for (Element element : elements) {
|
||||
if (element.getKind().isClass() && isTestClass(element.getSimpleName())) {
|
||||
@ -50,4 +59,34 @@ public class EnableTestableProcessor extends BaseProcessor {
|
||||
return testClassName.substring(0, testClassName.length() - "Test".length());
|
||||
}
|
||||
|
||||
private void createTestableAgentJar() {
|
||||
if (!checkFirstClassCompiled()) {
|
||||
byte[] bytes = ResourceUtil.fetchBinary(TESTABLE_AGENT_JAR);
|
||||
if (bytes.length == 0) {
|
||||
cx.logger.error("Failed to generate testable agent jar");
|
||||
}
|
||||
writeBinaryFile("", TESTABLE_AGENT_JAR, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkFirstClassCompiled() {
|
||||
if (!hasFirstClassCompiled) {
|
||||
hasFirstClassCompiled = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void writeBinaryFile(String path, String fileName, byte[] content) {
|
||||
try {
|
||||
FileObject resource = cx.filter.createResource(StandardLocation.SOURCE_OUTPUT, path, fileName);
|
||||
OutputStream out = resource.openOutputStream();
|
||||
out.write(content);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
cx.logger.error("Failed to write " + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user