diff --git a/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java b/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java index 0856d42..841934b 100644 --- a/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java +++ b/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java @@ -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 annotations, RoundEnvironment roundEnv) { Set 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); - } - } - } diff --git a/core/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java b/core/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java index a8b2ef9..d9c8ed2 100644 --- a/core/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java +++ b/core/src/main/java/com/alibaba/testable/processor/EnableTestableProcessor.java @@ -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 annotations, RoundEnvironment roundEnv) { + createTestableAgentJar(); Set 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); + } + } }