avoid "n" naming conflict

This commit is contained in:
金戟 2020-07-20 22:16:45 +08:00
parent c449649679
commit bc62af8bb1
3 changed files with 15 additions and 22 deletions

View File

@ -8,7 +8,6 @@ import com.alibaba.testable.util.ResourceUtil;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import javax.annotation.processing.FilerException;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
@ -23,8 +22,6 @@ import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.util.Set; import java.util.Set;
import static javax.tools.StandardLocation.SOURCE_OUTPUT;
/** /**
* @author flin * @author flin
*/ */
@ -33,7 +30,11 @@ import static javax.tools.StandardLocation.SOURCE_OUTPUT;
public class EnableTestableInjectProcessor extends BaseProcessor { public class EnableTestableInjectProcessor extends BaseProcessor {
private static final String JAVA_POSTFIX = ".java"; private static final String JAVA_POSTFIX = ".java";
private static final String GENERATED_TEST_SOURCES = "generated-test-sources"; private static final String AGENT_TARGET_FOLDER = "generated_testable";
private static final String AGENT_TARGET_FILE = "agent.jar";
private static final String AGENT_SOURCE_FILE = "testable-agent.jar";
private static final String NE_SOURCE_FILE = ConstPool.NE_CLS + JAVA_POSTFIX;
private static boolean hasFirstClassCompiled = false;
@Override @Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
@ -48,30 +49,22 @@ public class EnableTestableInjectProcessor extends BaseProcessor {
} }
private void createStaticNewClass() { private void createStaticNewClass() {
if (!isStaticNewClassExist()) { if (!checkFirstClassCompiled()) {
try { try {
writeSourceFile(ConstPool.NE_PKG_CLS, ResourceUtil.fetchText("e.java")); writeBinaryFile(AGENT_TARGET_FOLDER, AGENT_TARGET_FILE, ResourceUtil.fetchBinary(AGENT_SOURCE_FILE));
writeBinaryFile("testable", "agent.jar", ResourceUtil.fetchBinary("testable-agent.jar")); writeSourceFile(ConstPool.NE_PKG_CLS, ResourceUtil.fetchText(NE_SOURCE_FILE));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
private boolean isStaticNewClassExist() { private boolean checkFirstClassCompiled() {
try { if (!hasFirstClassCompiled) {
FileObject staticNewClassFile = cx.filter.getResource(SOURCE_OUTPUT, ConstPool.NE_PKG, hasFirstClassCompiled = true;
ConstPool.NE_CLS + JAVA_POSTFIX);
return isCompilingTestClass(staticNewClassFile) || staticNewClassFile.getLastModified() > 0;
} catch (FilerException e) {
return true;
} catch (IOException e) {
return false; return false;
} }
} return true;
private boolean isCompilingTestClass(FileObject staticNewClassFile) {
return staticNewClassFile.getName().contains(GENERATED_TEST_SOURCES);
} }
private void processClassElement(Symbol.ClassSymbol clazz) { private void processClassElement(Symbol.ClassSymbol clazz) {

View File

@ -5,11 +5,11 @@ package com.alibaba.testable.util;
*/ */
public final class ConstPool { public final class ConstPool {
public static final String NE_PKG = "n"; public static final String NE_PKG = "generated_testable.n";
public static final String NE_CLS = "e"; public static final String NE_CLS = "e";
public static final String NE_NEW = "w"; public static final String NE_NEW = "w";
public static final String NE_FUN = "f"; public static final String NE_FUN = "f";
public static final String NE_PKG_CLS = NE_PKG + ".e"; public static final String NE_PKG_CLS = NE_PKG + "." + NE_CLS;
public static final String NE_POOL = NE_PKG_CLS + ".p"; public static final String NE_POOL = NE_PKG_CLS + ".p";
public static final String NE_ADD_W = NE_PKG_CLS + ".aw"; public static final String NE_ADD_W = NE_PKG_CLS + ".aw";
public static final String NE_ADD_F = NE_PKG_CLS + ".af"; public static final String NE_ADD_F = NE_PKG_CLS + ".af";

View File

@ -1,4 +1,4 @@
package n; package generated_testable.n;
import com.sun.tools.javac.util.Pair; import com.sun.tools.javac.util.Pair;