2424import net .bytebuddy .agent .builder .SWAgentBuilderDefault ;
2525import net .bytebuddy .agent .builder .SWDescriptionStrategy ;
2626import net .bytebuddy .agent .builder .SWNativeMethodStrategy ;
27+ import net .bytebuddy .description .modifier .Visibility ;
2728import net .bytebuddy .implementation .FieldAccessor ;
2829import net .bytebuddy .implementation .MethodDelegation ;
2930import net .bytebuddy .implementation .SWImplementationContextFactory ;
3839import org .apache .skywalking .apm .agent .bytebuddy .SWAuxiliaryTypeNamingStrategy ;
3940import org .apache .skywalking .apm .agent .bytebuddy .SWClassFileLocator ;
4041import org .apache .skywalking .apm .agent .bytebuddy .biz .BizFoo ;
42+ import org .apache .skywalking .apm .agent .bytebuddy .biz .ChildBar ;
4143import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .EnhancedInstance ;
4244import org .junit .Assert ;
4345import org .junit .BeforeClass ;
@@ -63,6 +65,8 @@ public class AbstractInterceptTest {
6365 public static final String BIZ_FOO_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo" ;
6466 public static final String PROJECT_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ProjectService" ;
6567 public static final String DOC_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.DocService" ;
68+ public static final String PARENT_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar" ;
69+ public static final String CHILD_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar" ;
6670 public static final String SAY_HELLO_METHOD = "sayHello" ;
6771 public static final int BASE_INT_VALUE = 100 ;
6872 public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass" ;
@@ -85,6 +89,20 @@ protected void failed(Throwable e, Description description) {
8589 }
8690 };
8791
92+ protected static void callBar (int round ) {
93+ Log .info ("-------------" );
94+ Log .info ("callChildBar: " + round );
95+ // load target class
96+ String strResultChild = new ChildBar ().sayHelloChild ();
97+ Log .info ("result: " + strResultChild );
98+
99+ String strResultParent = new ChildBar ().sayHelloParent ();
100+ Log .info ("result: " + strResultParent );
101+
102+ Assert .assertEquals ("String value is unexpected" , "John" , strResultChild );
103+ Assert .assertEquals ("String value is unexpected" , "John" , strResultParent );
104+ }
105+
88106 protected static void callBizFoo (int round ) {
89107 Log .info ("-------------" );
90108 Log .info ("callBizFoo: " + round );
@@ -115,7 +133,7 @@ protected static void checkConstructorInterceptor(String className, int round) {
115133
116134 protected static void checkInterface (Class testClass , Class interfaceCls ) {
117135 Assert .assertTrue ("Check interface failure, the test class: " + testClass + " does not implement the expected interface: " + interfaceCls ,
118- EnhancedInstance .class .isAssignableFrom (BizFoo . class ));
136+ EnhancedInstance .class .isAssignableFrom (testClass ));
119137 }
120138
121139 protected static void checkErrors () {
@@ -195,6 +213,9 @@ protected void installInterface(String className) {
195213 builder = builder .defineField (
196214 CONTEXT_ATTR_NAME , Object .class , ACC_PRIVATE | ACC_VOLATILE )
197215 .implement (EnhancedInstance .class )
216+ .defineMethod ("getSkyWalkingDynamicField" , Object .class , Visibility .PUBLIC )
217+ .intercept (FieldAccessor .ofField (CONTEXT_ATTR_NAME ))
218+ .defineMethod ("setSkyWalkingDynamicField" , void .class , Visibility .PUBLIC ).withParameters (Object .class )
198219 .intercept (FieldAccessor .ofField (CONTEXT_ATTR_NAME ));
199220 }
200221 return builder ;
@@ -223,7 +244,7 @@ protected void installTraceClassTransformer(String msg) {
223244 ClassFileTransformer classFileTransformer = new ClassFileTransformer () {
224245 @ Override
225246 public byte [] transform (ClassLoader loader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classfileBuffer ) throws IllegalClassFormatException {
226- if (className .endsWith ("BizFoo" ) || className .endsWith ("ProjectService" ) || className .endsWith ("DocService" )) {
247+ if (className .endsWith ("BizFoo" ) || className .endsWith ("ProjectService" ) || className .endsWith ("DocService" ) || className . endsWith ( "ChildBar" ) || className . endsWith ( "ParentBar" ) ) {
227248 Log .error (msg + className );
228249 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
229250 ClassReader cr = new ClassReader (classfileBuffer );
0 commit comments