wu-sheng commented on code in PR #561: URL: https://github.com/apache/skywalking-java/pull/561#discussion_r1239645842
########## apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/case1/AbstractInterceptTest.java: ########## @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.agent.bytebuddy.case1; + +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.agent.ByteBuddyAgent; +import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.agent.builder.SWAgentBuilderDefault; +import org.apache.skywalking.apm.agent.bytebuddy.SWAsmVisitorWrapper; +import net.bytebuddy.agent.builder.SWNativeMethodStrategy; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.implementation.MethodDelegation; +import org.apache.skywalking.apm.agent.bytebuddy.SWAuxiliaryTypeNamingStrategy; +import net.bytebuddy.implementation.SWImplementationContextFactory; +import net.bytebuddy.implementation.SuperMethodCall; +import net.bytebuddy.matcher.ElementMatchers; +import net.bytebuddy.utility.JavaModule; +import org.apache.skywalking.apm.agent.bytebuddy.ConstructorAdvice; +import org.apache.skywalking.apm.agent.bytebuddy.ConstructorInter; +import org.apache.skywalking.apm.agent.bytebuddy.EnhanceHelper; +import org.apache.skywalking.apm.agent.bytebuddy.InstMethodAdvice; +import org.apache.skywalking.apm.agent.bytebuddy.InstMethodsInter; +import org.apache.skywalking.apm.agent.bytebuddy.Log; +import org.apache.skywalking.apm.agent.bytebuddy.SWClassFileLocator; +import org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo; +import org.junit.Assert; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.util.TraceClassVisitor; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.security.ProtectionDomain; +import java.util.Arrays; +import java.util.List; + +public class AbstractInterceptTest { + public static final String BIZ_FOO_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo"; + public static final String PROJECT_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ProjectService"; + public static final String DOC_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.DocService"; + public static final String SAY_HELLO_METHOD = "sayHello"; + public static final int BASE_INT_VALUE = 100; + public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass"; + public static final String METHOD_INTERCEPTOR_CLASS = "methodInterceptorClass"; + protected List<String> nameTraits = Arrays.asList("sw2023", "sw2024"); + protected boolean deleteDuplicatedFields = false; + + protected static void callBizFoo(int round) { + Log.info("-------------"); + Log.info("callBizFoo: " + round); + // load target class + int intResult = new BizFoo().sayHello(BASE_INT_VALUE); + Log.info(intResult); + + String result = new BizFoo("Smith").sayHello("Joe"); + Log.info(result); + + Assert.assertEquals("Int value is unexpected", BASE_INT_VALUE + round, intResult); + Assert.assertEquals("String value is unexpected", "Hello to John from Smith", result); + } + + protected static void checkMethodInterceptor(String method, int round) { + List<String> interceptors = EnhanceHelper.getInterceptors(); + String interceptorName = METHOD_INTERCEPTOR_CLASS + "$" + method + "$" + round; + Assert.assertTrue("Not found interceptor: " + interceptorName, interceptors.contains(interceptorName)); + Log.info("Found interceptor: " + interceptorName); + } + + protected static void checkConstructorInterceptor(int round) { + List<String> interceptors = EnhanceHelper.getInterceptors(); + String interceptorName = CONSTRUCTOR_INTERCEPTOR_CLASS + "$" + round; + Assert.assertTrue("Not found interceptor: " + interceptorName, interceptors.contains(interceptorName)); + Log.info("Found interceptor: " + interceptorName); + } + + protected void installMethodInterceptor(String className, String methodName, int round) { + this.installMethodInterceptor1(className, methodName, round); +// this.installMethodInterceptor2(className, methodName, round); + } + + protected void installMethodInterceptor1(String className, String methodName, int round) { + String interceptorClassName = METHOD_INTERCEPTOR_CLASS + "$" + methodName + "$" + round; + String nameTrait = getNameTrait(round); + String fieldName = nameTrait + "_delegate$" + methodName + round; + + newAgentBuilder(nameTrait).type(ElementMatchers.named(className)) + .transform((builder, typeDescription, classLoader, module, protectionDomain) -> { + if (deleteDuplicatedFields) { + builder = builder.visit(new SWAsmVisitorWrapper()); + } + return builder + .method(ElementMatchers.nameContainsIgnoreCase(methodName)) + .intercept(MethodDelegation.withDefaultConfiguration() + .to(new InstMethodsInter(interceptorClassName, classLoader), fieldName)) + ; + } + ) + .with(new AgentBuilder.Listener.Adapter() { + @Override + public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) { + System.err.println(String.format("Transform Error: interceptorClassName: %s, typeName: %s, classLoader: %s, module: %s, loaded: %s", interceptorClassName, typeName, classLoader, module, loaded)); + throwable.printStackTrace(); + } + }) + .installOn(ByteBuddyAgent.install()); + } + + protected void installMethodInterceptor2(String className, String methodName, int round) { + String interceptorClassName = METHOD_INTERCEPTOR_CLASS + "$" + methodName + "$" + round; + String nameTrait = getNameTrait(round); + String fieldName = nameTrait + "_delegate$" + methodName + round; + + AgentBuilder agentBuilder = newAgentBuilder(nameTrait); + agentBuilder.type(ElementMatchers.named(className)) + .transform((builder, typeDescription, classLoader, module, protectionDomain) -> { + if (deleteDuplicatedFields) { + builder = builder.visit(new SWAsmVisitorWrapper()); + } + return builder + .method(ElementMatchers.nameContainsIgnoreCase(methodName)) + .intercept(Advice.to(InstMethodAdvice.class)); + } + ) + .with(new AgentBuilder.Listener.Adapter() { + @Override + public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) { + System.err.println(String.format("Transform Error: interceptorClassName: %s, typeName: %s, classLoader: %s, module: %s, loaded: %s", interceptorClassName, typeName, classLoader, module, loaded)); + throwable.printStackTrace(); + } + }) + .installOn(ByteBuddyAgent.install()); + } + + protected void installConstructorInterceptor(String className, int round) { + installConstructorInterceptor1(className, round); +// installConstructorInterceptor2(className, round); Review Comment: Forget to remove? What is intercetopro1 and interceptor2? Could you give a more proper name? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
