diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index 58ddd15..e1bdbe7 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -14,6 +14,7 @@ public class PreMain { private static final String AND = "&"; private static final String LOG_LEVEL = "logLevel"; private static final String DUMP_PATH = "dumpPath"; + private static final String PKG_PREFIX = "pkgPrefix"; private static final String EQUAL = "="; public static void premain(String agentArgs, Instrumentation inst) { @@ -34,6 +35,8 @@ public class PreMain { GlobalConfig.setLogLevel(v); } else if (k.equals(DUMP_PATH)) { GlobalConfig.setDumpPath(v); + } else if (k.equals(PKG_PREFIX)) { + GlobalConfig.setPkgPrefix(v); } } else { GlobalConfig.setLogLevel(a); diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index e36ab19..8042d65 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -36,6 +36,7 @@ import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassNa public class TestableClassTransformer implements ClassFileTransformer { private static final String FIELD_DIAGNOSE = "diagnose"; + private static final String COMMA = ","; /** * Just avoid spend time to scan those surely non-user classes @@ -105,14 +106,25 @@ public class TestableClassTransformer implements ClassFileTransformer { if (null == className) { return true; } - for (String prefix : WHITELIST_PREFIXES) { - if (className.startsWith(prefix)) { - return false; + String whitePrefix = GlobalConfig.getPkgPrefix(); + if (whitePrefix != null) { + for (String prefix : whitePrefix.split(COMMA)) { + if (className.startsWith(prefix)) { + // Only consider package in provided list as non-system class + return false; + } } - } - for (String prefix : BLACKLIST_PREFIXES) { - if (className.startsWith(prefix)) { - return true; + return true; + } else { + for (String prefix : WHITELIST_PREFIXES) { + if (className.startsWith(prefix)) { + return false; + } + } + for (String prefix : BLACKLIST_PREFIXES) { + if (className.startsWith(prefix)) { + return true; + } } } return false; diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java index 178941a..3bb6ef6 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/util/GlobalConfig.java @@ -12,6 +12,7 @@ public class GlobalConfig { private static final String VERBOSE = "verbose"; private static String dumpPath = null; + private static String pkgPrefix = null; public static boolean setLogLevel(String level) { if (level.equals(MUTE)) { @@ -35,4 +36,11 @@ public class GlobalConfig { return dumpPath; } + public static String getPkgPrefix() { + return pkgPrefix; + } + + public static void setPkgPrefix(String pkgPrefix) { + GlobalConfig.pkgPrefix = pkgPrefix; + } } diff --git a/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java b/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java index 9e6b4da..d3e4c33 100644 --- a/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java +++ b/testable-maven-plugin/src/main/java/com/alibaba/testable/TestableMojo.java @@ -1,5 +1,6 @@ package com.alibaba.testable; +import com.google.common.base.Strings; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -45,6 +46,12 @@ public class TestableMojo extends AbstractMojo @Parameter private String dumpPath; + /** + * Package prefixes of class need to be mocked (comma split) + */ + @Parameter + private String pkgPrefix; + /** * Name of the Testable Agent artifact. */ @@ -71,13 +78,17 @@ public class TestableMojo extends AbstractMojo getLog().error("failed to fetch project properties"); return; } + String extraArgs = ""; - if (logLevel != null && !logLevel.isEmpty()) { + if (!Strings.isNullOrEmpty(logLevel)) { extraArgs += "&logLevel=" + logLevel; } - if (dumpPath != null && !dumpPath.isEmpty()) { + if (!Strings.isNullOrEmpty(dumpPath)) { extraArgs += "&dumpPath=" + dumpPath; } + if (!Strings.isNullOrEmpty(pkgPrefix)) { + extraArgs += "&pkgPrefix=" + pkgPrefix; + } final String oldArgs = projectProperties.getProperty(testArgsPropertyKey); String newArgs = (oldArgs == null) ? getAgentJarArgs().trim() : (oldArgs + getAgentJarArgs()); if (!extraArgs.isEmpty()) { @@ -91,6 +102,7 @@ public class TestableMojo extends AbstractMojo final Artifact testableAgentArtifact = pluginArtifactMap.get(AGENT_ARTIFACT_NAME); if (testableAgentArtifact == null) { getLog().error("failed to find testable agent jar"); + return ""; } return " -javaagent:" + testableAgentArtifact.getFile().getAbsolutePath(); }