You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by lu...@apache.org on 2025/05/21 11:02:51 UTC

(skywalking-java) branch feature/reproduce-parent-class-enhance-issue created (now 695460bdb3)

This is an automated email from the ASF dual-hosted git repository.

lujiajing pushed a change to branch feature/reproduce-parent-class-enhance-issue
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


      at 695460bdb3 add test cases

This branch includes the following new commits:

     new 695460bdb3 add test cases

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(skywalking-java) 01/01: add test cases

Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lujiajing pushed a commit to branch feature/reproduce-parent-class-enhance-issue
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git

commit 695460bdb307dfe6fcbfea5a982590cc3bbe1bb1
Author: Megrez Lu <lu...@gmail.com>
AuthorDate: Wed May 21 19:02:35 2025 +0800

    add test cases
---
 .../apm/agent/bytebuddy/biz/ChildBar.java          |  7 +++
 .../apm/agent/bytebuddy/biz/ParentBar.java         |  7 +++
 .../bytebuddy/cases/AbstractInterceptTest.java     | 24 +++++++--
 .../agent/bytebuddy/cases/ReTransform4Test.java    | 61 ++++++++++++++++++++++
 4 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ChildBar.java b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ChildBar.java
new file mode 100644
index 0000000000..91f3cd3ef1
--- /dev/null
+++ b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ChildBar.java
@@ -0,0 +1,7 @@
+package org.apache.skywalking.apm.agent.bytebuddy.biz;
+
+public class ChildBar extends ParentBar {
+    public String sayHelloChild() {
+        return "Joe";
+    }
+}
diff --git a/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ParentBar.java b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ParentBar.java
new file mode 100644
index 0000000000..d3bc44dfe1
--- /dev/null
+++ b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/biz/ParentBar.java
@@ -0,0 +1,7 @@
+package org.apache.skywalking.apm.agent.bytebuddy.biz;
+
+public class ParentBar {
+    public String sayHelloParent() {
+        return "Joe";
+    }
+}
diff --git a/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/AbstractInterceptTest.java b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/AbstractInterceptTest.java
index ba9cf4d372..9093d8c98a 100644
--- a/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/AbstractInterceptTest.java
+++ b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/AbstractInterceptTest.java
@@ -38,6 +38,7 @@ import org.apache.skywalking.apm.agent.bytebuddy.SWAsmVisitorWrapper;
 import org.apache.skywalking.apm.agent.bytebuddy.SWAuxiliaryTypeNamingStrategy;
 import org.apache.skywalking.apm.agent.bytebuddy.SWClassFileLocator;
 import org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo;
+import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
 import org.junit.Assert;
 import org.junit.BeforeClass;
@@ -63,6 +64,8 @@ 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 PARENT_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar";
+    public static final String CHILD_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar";
     public static final String SAY_HELLO_METHOD = "sayHello";
     public static final int BASE_INT_VALUE = 100;
     public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass";
@@ -85,6 +88,20 @@ public class AbstractInterceptTest {
         }
     };
 
+    protected static void callBar(int round) {
+        Log.info("-------------");
+        Log.info("callChildBar: " + round);
+        // load target class
+        String strResultChild = new ChildBar().sayHelloChild();
+        Log.info("result: " + strResultChild);
+
+        String strResultParent = new ChildBar().sayHelloParent();
+        Log.info("result: " + strResultParent);
+
+        Assert.assertEquals("String value is unexpected", "John", strResultChild);
+        Assert.assertEquals("String value is unexpected", "John", strResultParent);
+    }
+
     protected static void callBizFoo(int round) {
         Log.info("-------------");
         Log.info("callBizFoo: " + round);
@@ -115,7 +132,7 @@ public class AbstractInterceptTest {
 
     protected static void checkInterface(Class testClass, Class interfaceCls) {
         Assert.assertTrue("Check interface failure, the test class: " + testClass + " does not implement the expected interface: " + interfaceCls,
-                EnhancedInstance.class.isAssignableFrom(BizFoo.class));
+                EnhancedInstance.class.isAssignableFrom(testClass));
     }
 
     protected static void checkErrors() {
@@ -139,8 +156,7 @@ public class AbstractInterceptTest {
                             return builder
                                     .method(ElementMatchers.nameContainsIgnoreCase(methodName))
                                     .intercept(MethodDelegation.withDefaultConfiguration()
-                                            .to(new InstMethodsInter(interceptorClassName, classLoader), fieldName))
-                                    ;
+                                            .to(new InstMethodsInter(interceptorClassName, classLoader), fieldName));
                         }
                 )
                 .with(getListener(interceptorClassName))
@@ -223,7 +239,7 @@ public class AbstractInterceptTest {
         ClassFileTransformer classFileTransformer = new ClassFileTransformer() {
             @Override
             public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
-                if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService")) {
+                if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService") || className.endsWith("ChildBar") || className.endsWith("ParentBar")) {
                     Log.error(msg + className);
                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                     ClassReader cr = new ClassReader(classfileBuffer);
diff --git a/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/ReTransform4Test.java b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/ReTransform4Test.java
new file mode 100644
index 0000000000..7570e2c94e
--- /dev/null
+++ b/apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/ReTransform4Test.java
@@ -0,0 +1,61 @@
+/*
+ * 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.cases;
+
+import net.bytebuddy.agent.ByteBuddyAgent;
+import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
+import org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.junit.Test;
+
+import java.lang.instrument.Instrumentation;
+
+public class ReTransform4Test extends AbstractReTransformTest {
+
+    @Test
+    public void testEnhancedInstanceForMultiShotRetransform() throws Exception {
+        Instrumentation instrumentation = ByteBuddyAgent.install();
+
+        // install transformer
+        installMethodInterceptor(PARENT_BAR_CLASS_NAME, "sayHelloParent", 1);
+        installMethodInterceptor(CHILD_BAR_CLASS_NAME, "sayHelloChild", 1);
+        // implement EnhancedInstance
+        installInterface(PARENT_BAR_CLASS_NAME);
+        installInterface(CHILD_BAR_CLASS_NAME);
+
+        callBar(1);
+
+        // check interceptors
+        checkInterface(ParentBar.class, EnhancedInstance.class);
+        checkInterface(ChildBar.class, EnhancedInstance.class);
+        checkErrors();
+
+        installTraceClassTransformer("Trace class: ");
+
+        // do retransform
+        reTransform(instrumentation, ChildBar.class);
+
+        // check interceptors
+        checkInterface(ParentBar.class, EnhancedInstance.class);
+        checkInterface(ChildBar.class, EnhancedInstance.class);
+        checkErrors();
+    }
+
+}
+