This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch remove-javassist in repository https://gitbox.apache.org/repos/asf/skywalking.git
commit b9dcf3104504a395769503d5af7e1739452d543a Author: Wu Sheng <[email protected]> AuthorDate: Sun Jul 28 09:44:22 2019 +0800 Remove javassist dependency --- apm-sniffer/apm-agent-core/pom.xml | 15 ---- .../core/plugin}/InstrumentDebuggingClass.java | 6 +- .../plugin/bootstrap/BootstrapInstrumentBoost.java | 79 +++++++++++++++------- apm-sniffer/apm-agent/pom.xml | 1 - .../skywalking/apm/agent/SkyWalkingAgent.java | 3 +- 5 files changed, 61 insertions(+), 43 deletions(-) diff --git a/apm-sniffer/apm-agent-core/pom.xml b/apm-sniffer/apm-agent-core/pom.xml index 4cedde0..575d7fa 100644 --- a/apm-sniffer/apm-agent-core/pom.xml +++ b/apm-sniffer/apm-agent-core/pom.xml @@ -38,10 +38,6 @@ <grpc.version>1.14.0</grpc.version> <guava.version>20.0</guava.version> <bytebuddy.version>1.9.2</bytebuddy.version> - <!-- 3.21.0-GA is the last version w/ JDK 6 supported --> - <!-- We can't update this, unless JDK 6 agent is not supported. --> - <!-- ref discussion, https://github.com/jboss-javassist/javassist/issues/268 --> - <javaassist.version>3.21.0-GA</javaassist.version> <disruptor.version>3.3.6</disruptor.version> <wiremock.version>2.6.0</wiremock.version> <netty-tcnative-boringssl-static.version>2.0.7.Final</netty-tcnative-boringssl-static.version> @@ -58,8 +54,6 @@ <shade.io.netty.target>${shade.package}.${shade.io.netty.source}</shade.io.netty.target> <shade.io.opencensus.source>io.opencensus</shade.io.opencensus.source> <shade.io.opencensus.target>${shade.package}.${shade.io.opencensus.source}</shade.io.opencensus.target> - <shade.javaassist.source>javassist</shade.javaassist.source> - <shade.javaassist.target>${shade.package}.${shade.javaassist.source}</shade.javaassist.target> <ststem-rules.version>1.18.0</ststem-rules.version> </properties> @@ -105,11 +99,6 @@ <version>${netty-tcnative-boringssl-static.version}</version> </dependency> <dependency> - <groupId>org.javassist</groupId> - <artifactId>javassist</artifactId> - <version>${javaassist.version}</version> - </dependency> - <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy-agent</artifactId> <version>${bytebuddy.version}</version> @@ -193,10 +182,6 @@ </artifactSet> <relocations> <relocation> - <pattern>${shade.javaassist.source}</pattern> - <shadedPattern>${shade.javaassist.target}</shadedPattern> - </relocation> - <relocation> <pattern>${shade.com.lmax.disruptor.source}</pattern> <shadedPattern>${shade.com.lmax.disruptor.target}</shadedPattern> </relocation> diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java similarity index 94% rename from apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java rename to apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java index acb2711..1b14be7 100644 --- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java @@ -17,7 +17,7 @@ */ -package org.apache.skywalking.apm.agent; +package org.apache.skywalking.apm.agent.core.plugin; import java.io.File; import java.io.IOException; @@ -38,7 +38,7 @@ public enum InstrumentDebuggingClass { private static final ILog logger = LogManager.getLogger(InstrumentDebuggingClass.class); private File debuggingClassesRootPath; - public void log(TypeDescription typeDescription, DynamicType dynamicType) { + public void log(DynamicType dynamicType) { if (!Config.Agent.IS_OPEN_DEBUGGING_CLASS) { return; } @@ -62,7 +62,7 @@ public enum InstrumentDebuggingClass { try { dynamicType.saveIn(debuggingClassesRootPath); } catch (IOException e) { - logger.error(e, "Can't save class {} to file." + typeDescription.getActualName()); + logger.error(e, "Can't save class {} to file." + dynamicType.getTypeDescription().getActualName()); } } catch (Throwable t) { logger.error(t, "Save debugging classes fail."); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java index e3b5af1..e9f4d7b 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java @@ -26,16 +26,19 @@ import java.lang.instrument.Instrumentation; import java.util.HashMap; import java.util.List; import java.util.Map; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtField; +import net.bytebuddy.ByteBuddy; import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.dynamic.ClassFileLocator; +import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.loading.ClassInjector; +import net.bytebuddy.pool.TypePool; import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath; import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.InstrumentDebuggingClass; import org.apache.skywalking.apm.agent.core.plugin.PluginException; import org.apache.skywalking.apm.agent.core.plugin.PluginFinder; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; @@ -43,6 +46,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsIn import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader; +import static net.bytebuddy.matcher.ElementMatchers.named; + /** * If there is Bootstrap instrumentation plugin declared in plugin list, BootstrapInstrumentBoost inject the necessary * classes into bootstrap class loader, including generated dynamic delegate classes. @@ -51,6 +56,7 @@ import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader; */ public class BootstrapInstrumentBoost { private static final ILog logger = LogManager.getLogger(BootstrapInstrumentBoost.class); + private static final String SHADE_PACKAGE = "org.apache.skywalking.apm.dependencies."; private static final String[] HIGH_PRIORITY_CLASSES = { "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.BootstrapInterRuntimeAssist", @@ -58,7 +64,14 @@ public class BootstrapInstrumentBoost { "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.OverrideCallable", - "org.apache.skywalking.apm.agent.core.plugin.bootstrap.IBootstrapLog" + "org.apache.skywalking.apm.agent.core.plugin.bootstrap.IBootstrapLog", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.RuntimeType", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.This", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.AllArguments", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.AllArguments$Assignment", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.SuperCall", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.Origin", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.Morph" }; private static String INSTANCE_METHOD_DELEGATE_TEMPLATE = "org.apache.skywalking.apm.agent.core.plugin.bootstrap.template.InstanceMethodInterTemplate"; private static String INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE = "org.apache.skywalking.apm.agent.core.plugin.bootstrap.template.InstanceMethodInterWithOverrideArgsTemplate"; @@ -127,26 +140,26 @@ public class BootstrapInstrumentBoost { */ private static boolean prepareJREInstrumentation(PluginFinder pluginFinder, Map<String, byte[]> classesTypeMap) throws PluginException { - ClassPool classPool = ClassPool.getDefault(); + TypePool typePool = TypePool.Default.of(BootstrapInstrumentBoost.class.getClassLoader()); List<AbstractClassEnhancePluginDefine> bootstrapClassMatchDefines = pluginFinder.getBootstrapClassMatchDefine(); for (AbstractClassEnhancePluginDefine define : bootstrapClassMatchDefines) { for (InstanceMethodsInterceptPoint point : define.getInstanceMethodsInterceptPoints()) { if (point.isOverrideArgs()) { - generateDelegator(classesTypeMap, classPool, INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } else { - generateDelegator(classesTypeMap, classPool, INSTANCE_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, INSTANCE_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } } for (ConstructorInterceptPoint point : define.getConstructorsInterceptPoints()) { - generateDelegator(classesTypeMap, classPool, CONSTRUCTOR_DELEGATE_TEMPLATE, point.getConstructorInterceptor()); + generateDelegator(classesTypeMap, typePool, CONSTRUCTOR_DELEGATE_TEMPLATE, point.getConstructorInterceptor()); } for (StaticMethodsInterceptPoint point : define.getStaticMethodsInterceptPoints()) { if (point.isOverrideArgs()) { - generateDelegator(classesTypeMap, classPool, STATIC_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, STATIC_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } else { - generateDelegator(classesTypeMap, classPool, STATIC_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, STATIC_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } } } @@ -159,29 +172,49 @@ public class BootstrapInstrumentBoost { * One key step to avoid class confliction between AppClassLoader and BootstrapClassLoader * * @param classesTypeMap hosts injected binary of generated class - * @param classPool to generate new class + * @param typePool to generate new class * @param templateClassName represents the class as template in this generation process. The templates are * pre-defined in SkyWalking agent core. * @param methodsInterceptor */ - private static void generateDelegator(Map<String, byte[]> classesTypeMap, ClassPool classPool, + private static void generateDelegator(Map<String, byte[]> classesTypeMap, TypePool typePool, String templateClassName, String methodsInterceptor) { String internalInterceptorName = internalDelegate(methodsInterceptor); try { - CtClass interClass = classPool.get(templateClassName); - interClass.setName(internalInterceptorName); - - CtField interceptorDefine = interClass.getField("TARGET_INTERCEPTOR"); - interClass.removeField(interceptorDefine); - - interceptorDefine = CtField.make("private static String TARGET_INTERCEPTOR = \"" + methodsInterceptor + "\";", interClass); - interClass.addField(interceptorDefine); + TypeDescription templateTypeDescription = typePool.describe(templateClassName) + .resolve(); + + DynamicType.Unloaded interceptorType = new ByteBuddy() + .redefine(templateTypeDescription, ClassFileLocator.ForClassLoader.of(BootstrapInstrumentBoost.class.getClassLoader())) + .name(internalInterceptorName) + .field(named("TARGET_INTERCEPTOR")).value(methodsInterceptor) + .make(); + + classesTypeMap.put(internalInterceptorName, interceptorType.getBytes()); + + try { + File debuggingClassesRootPath = null; + try { + debuggingClassesRootPath = new File(AgentPackagePath.getPath(), "/debugging"); + if (!debuggingClassesRootPath.exists()) { + debuggingClassesRootPath.mkdir(); + } + } catch (AgentPackageNotFoundException e) { + logger.error(e, "Can't find the root path for creating /debugging folder."); + } - byte[] bytes = interClass.toBytecode(); + try { + interceptorType.saveIn(debuggingClassesRootPath); + } catch (IOException e) { + logger.error(e, "Can't save class {} to file."); + } + } catch (Throwable t) { + logger.error(t, "Save debugging classes fail."); + } - interClass.toClass(); + classesTypeMap.put(internalInterceptorName, interceptorType.getBytes()); - classesTypeMap.put(internalInterceptorName, bytes); + InstrumentDebuggingClass.INSTANCE.log(interceptorType); } catch (Exception e) { throw new PluginException("Generate Dynamic plugin failure", e); } diff --git a/apm-sniffer/apm-agent/pom.xml b/apm-sniffer/apm-agent/pom.xml index 6f95267..64d51bd 100644 --- a/apm-sniffer/apm-agent/pom.xml +++ b/apm-sniffer/apm-agent/pom.xml @@ -81,7 +81,6 @@ <exclude>io.opencensus:*</exclude> <exclude>com.google.*:*</exclude> <exclude>com.google.guava:guava</exclude> - <exclude>org.javassist:*</exclude> </excludes> </artifactSet> <relocations> diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java index 98b3b53..8b02e86 100644 --- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java +++ b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java @@ -39,6 +39,7 @@ import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext; +import org.apache.skywalking.apm.agent.core.plugin.InstrumentDebuggingClass; import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap; import org.apache.skywalking.apm.agent.core.plugin.PluginException; import org.apache.skywalking.apm.agent.core.plugin.PluginFinder; @@ -171,7 +172,7 @@ public class SkyWalkingAgent { logger.debug("On Transformation class {}.", typeDescription.getName()); } - InstrumentDebuggingClass.INSTANCE.log(typeDescription, dynamicType); + InstrumentDebuggingClass.INSTANCE.log(dynamicType); } @Override
