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 ;
5860import static net .bytebuddy .jar .asm .Opcodes .ACC_PRIVATE ;
5961import static net .bytebuddy .jar .asm .Opcodes .ACC_VOLATILE ;
6062import static org .apache .skywalking .apm .agent .core .plugin .AbstractClassEnhancePluginDefine .CONTEXT_ATTR_NAME ;
63+ import static org .apache .skywalking .apm .agent .core .plugin .AbstractClassEnhancePluginDefine .CONTEXT_GETTER_NAME ;
64+ import static org .apache .skywalking .apm .agent .core .plugin .AbstractClassEnhancePluginDefine .CONTEXT_SETTER_NAME ;
6165
6266public class AbstractInterceptTest {
6367 public static final String BIZ_FOO_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo" ;
6468 public static final String PROJECT_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ProjectService" ;
6569 public static final String DOC_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.DocService" ;
70+ public static final String PARENT_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar" ;
71+ public static final String CHILD_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar" ;
6672 public static final String SAY_HELLO_METHOD = "sayHello" ;
6773 public static final int BASE_INT_VALUE = 100 ;
6874 public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass" ;
@@ -85,6 +91,20 @@ protected void failed(Throwable e, Description description) {
8591 }
8692 };
8793
94+ protected static void callBar (int round ) {
95+ Log .info ("-------------" );
96+ Log .info ("callChildBar: " + round );
97+ // load target class
98+ String strResultChild = new ChildBar ().sayHelloChild ();
99+ Log .info ("result: " + strResultChild );
100+
101+ String strResultParent = new ChildBar ().sayHelloParent ();
102+ Log .info ("result: " + strResultParent );
103+
104+ Assert .assertEquals ("String value is unexpected" , "John" , strResultChild );
105+ Assert .assertEquals ("String value is unexpected" , "John" , strResultParent );
106+ }
107+
88108 protected static void callBizFoo (int round ) {
89109 Log .info ("-------------" );
90110 Log .info ("callBizFoo: " + round );
@@ -115,7 +135,7 @@ protected static void checkConstructorInterceptor(String className, int round) {
115135
116136 protected static void checkInterface (Class testClass , Class interfaceCls ) {
117137 Assert .assertTrue ("Check interface failure, the test class: " + testClass + " does not implement the expected interface: " + interfaceCls ,
118- EnhancedInstance .class .isAssignableFrom (BizFoo . class ));
138+ EnhancedInstance .class .isAssignableFrom (testClass ));
119139 }
120140
121141 protected static void checkErrors () {
@@ -195,6 +215,9 @@ protected void installInterface(String className) {
195215 builder = builder .defineField (
196216 CONTEXT_ATTR_NAME , Object .class , ACC_PRIVATE | ACC_VOLATILE )
197217 .implement (EnhancedInstance .class )
218+ .defineMethod (CONTEXT_GETTER_NAME , Object .class , Visibility .PUBLIC )
219+ .intercept (FieldAccessor .ofField (CONTEXT_ATTR_NAME ))
220+ .defineMethod (CONTEXT_SETTER_NAME , void .class , Visibility .PUBLIC ).withParameters (Object .class )
198221 .intercept (FieldAccessor .ofField (CONTEXT_ATTR_NAME ));
199222 }
200223 return builder ;
@@ -223,7 +246,7 @@ protected void installTraceClassTransformer(String msg) {
223246 ClassFileTransformer classFileTransformer = new ClassFileTransformer () {
224247 @ Override
225248 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" )) {
249+ if (className .endsWith ("BizFoo" ) || className .endsWith ("ProjectService" ) || className .endsWith ("DocService" ) || className . endsWith ( "ChildBar" ) || className . endsWith ( "ParentBar" ) ) {
227250 Log .error (msg + className );
228251 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
229252 ClassReader cr = new ClassReader (classfileBuffer );
0 commit comments