use relative path for bytecode dumping

This commit is contained in:
金戟 2021-04-05 14:50:46 +08:00
parent a46e73a421
commit d4a69528a7
4 changed files with 20 additions and 10 deletions

View File

@ -9,6 +9,8 @@ public class ConstPool {
public static final String FIELD_TARGET_CLASS = "targetClass";
public static final String FIELD_SCOPE = "scope";
public static final String PROPERTY_USER_DIR = "user.dir";
public static final String MOCK_WITH = "com.alibaba.testable.core.annotation.MockWith";
public static final String DUMP_TO = "com.alibaba.testable.core.annotation.DumpTo";
public static final String MOCK_DIAGNOSE = "com.alibaba.testable.core.annotation.MockDiagnose";

View File

@ -20,11 +20,11 @@ import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.List;
import static com.alibaba.testable.agent.constant.ConstPool.CGLIB_CLASS_PATTERN;
import static com.alibaba.testable.agent.constant.ConstPool.KOTLIN_POSTFIX_COMPANION;
import static com.alibaba.testable.agent.constant.ConstPool.*;
import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
import static com.alibaba.testable.core.constant.ConstPool.DOLLAR;
import static com.alibaba.testable.core.constant.ConstPool.TEST_POSTFIX;
import static com.alibaba.testable.core.util.PathUtil.createFolder;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
/**
@ -34,7 +34,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
private static final String FIELD_VALUE = "value";
private static final String FIELD_TREAT_AS = "treatAs";
private static final String FIELD_PATH = "path";
private static final String COMMA = ",";
private static final String CLASS_NAME_MOCK = "Mock";
@ -241,7 +240,11 @@ public class TestableClassTransformer implements ClassFileTransformer {
if (cn.visibleAnnotations != null) {
for (AnnotationNode an : cn.visibleAnnotations) {
if (toJavaStyleClassName(an.desc).equals(ConstPool.DUMP_TO)) {
return AnnotationUtil.getAnnotationParameter(an, FIELD_PATH, null, String.class);
String path = AnnotationUtil.getAnnotationParameter(an, FIELD_VALUE, null, String.class);
String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path);
if (createFolder(fullPath)) {
return fullPath;
}
}
}
}

View File

@ -6,6 +6,9 @@ import com.alibaba.testable.core.util.LogUtil;
import java.io.File;
import static com.alibaba.testable.agent.constant.ConstPool.PROPERTY_USER_DIR;
import static com.alibaba.testable.core.util.PathUtil.createFolder;
/**
* @author flin
*/
@ -14,7 +17,6 @@ public class GlobalConfig {
private static final String MUTE = "mute";
private static final String DEBUG = "debug";
private static final String VERBOSE = "verbose";
private static final String USER_DIR = "user.dir";
private static final String DISABLE_LOG_FILE = "null";
private static final String TESTABLE_AGENT_LOG = "testable-agent.log";
@ -43,7 +45,10 @@ public class GlobalConfig {
}
public static void setDumpPath(String path) {
dumpPath = path;
String fullPath = PathUtil.join(System.getProperty(PROPERTY_USER_DIR), path);
if (createFolder(fullPath)) {
dumpPath = fullPath;
}
}
public static String getPkgPrefix() {
@ -64,13 +69,13 @@ public class GlobalConfig {
public static void setupLogRootPath() {
if (logFile == null) {
String baseFolder = PathUtil.getFirstLevelFolder(System.getProperty(USER_DIR),
String baseFolder = PathUtil.getFirstLevelFolder(System.getProperty(PROPERTY_USER_DIR),
Object.class.getResource("/").getPath());
if (!baseFolder.isEmpty()) {
LogUtil.setGlobalLogPath(PathUtil.join(baseFolder, TESTABLE_AGENT_LOG));
}
} else if (!DISABLE_LOG_FILE.equals(logFile)) {
LogUtil.setGlobalLogPath(PathUtil.join(System.getProperty(USER_DIR), logFile));
LogUtil.setGlobalLogPath(PathUtil.join(System.getProperty(PROPERTY_USER_DIR), logFile));
}
}
}

View File

@ -14,8 +14,8 @@ public @interface DumpTo {
/**
* dump class byte code to specified folder
* @return an exist folder
* @return folder path relate to the project root path
*/
String path() default "";
String value();
}