增加大量的复杂结构,用于定义对类的拦截,以及实例级上下文存储。#36
This commit is contained in:
parent
47fde7ebb8
commit
d635a4aff6
|
|
@ -36,7 +36,10 @@ public class EnhanceClazz4Interceptor {
|
|||
}
|
||||
}
|
||||
|
||||
private void enhance0(String enhanceOriginClassName) {
|
||||
private void enhance0(String interceptorDefineClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
InterceptorDefine define = (InterceptorDefine)Class.forName(interceptorDefineClassName).newInstance();
|
||||
|
||||
String enhanceOriginClassName = define.getBeInterceptedClassName();
|
||||
/**
|
||||
* add '$$Origin' at the end of be enhanced classname <br/>
|
||||
* such as: class com.ai.cloud.TestClass to class com.ai.cloud.TestClass$$Origin
|
||||
|
|
@ -48,5 +51,13 @@ public class EnhanceClazz4Interceptor {
|
|||
.make()
|
||||
.load(ClassLoader.getSystemClassLoader(),
|
||||
ClassLoadingStrategy.Default.INJECTION).getLoaded();
|
||||
|
||||
/**
|
||||
* define class as origin class name. and inject to classloader. <br/>
|
||||
* new class need:<br/>
|
||||
* 1.implement com.ai.cloud.skywalking.plugin.interceptor.IEnhancedClassInstanceContext(); <br/>
|
||||
* 2.add field '_$EnhancedClassInstanceContext' of type EnhancedClassInstanceContext
|
||||
* 3.intercept constructor and method if required by interceptorDefineClass
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 被增强的类实例,需扩展的context属性,用于在不同的方法,或者构造函数间保存实例
|
||||
*
|
||||
* @author wusheng
|
||||
*
|
||||
*/
|
||||
public class EnhancedClassInstanceContext {
|
||||
public static final String FIELD_NAME = "_$EnhancedClassInstanceContext";
|
||||
|
||||
private Map<Object, Object> context = new HashMap<Object, Object>();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
public interface IAroundInterceptor {
|
||||
public void onConstruct(EnhancedClassInstanceContext context);
|
||||
|
||||
public void beforeMethod(EnhancedClassInstanceContext context);
|
||||
|
||||
public void afterMethod(EnhancedClassInstanceContext context);
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
/**
|
||||
* 被增强的类,会实现此接口,用于快速获取实例级属性上下文扩展<br/>
|
||||
* @see com.ai.cloud.skywalking.plugin.interceptor.EnhanceClazz4Interceptor.enhance0()
|
||||
*
|
||||
* @author wusheng
|
||||
*
|
||||
*/
|
||||
public interface IEnhancedClassInstanceContext {
|
||||
public EnhancedClassInstanceContext getEnhancedClassInstanceContext();
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
public interface InterceptorDefine {
|
||||
public String getBeInterceptedClassName();
|
||||
|
||||
public String[] getBeInterceptedMethods();
|
||||
|
||||
public IAroundInterceptor instance();
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ import net.bytebuddy.pool.TypePool;
|
|||
|
||||
public class SimulateMain {
|
||||
public static void main(String[] args) throws NoSuchFieldException,
|
||||
SecurityException, InstantiationException, IllegalAccessException {
|
||||
SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
TypePool typePool = TypePool.Default.ofClassPath();
|
||||
|
||||
Class<?> newClazz = new ByteBuddy()
|
||||
|
|
@ -39,6 +39,9 @@ public class SimulateMain {
|
|||
// System.out.println(t22.testA("1"));
|
||||
TestClass t = new TestClass("abc");
|
||||
System.out.println(t.testA("1"));
|
||||
|
||||
t = new TestClass("abc");
|
||||
System.out.println(t.testA("1"));
|
||||
|
||||
// TestClass t2 = null;
|
||||
// try {
|
||||
|
|
|
|||
Loading…
Reference in New Issue