调整代码结构。完善context相关内容。#36
This commit is contained in:
parent
c64ce242f1
commit
904d0cb205
|
|
@ -24,21 +24,14 @@ public class ClassConstructorInterceptor {
|
|||
@FieldProxy(EnhanceClazz4Interceptor.contextAttrName) FieldSetter accessor,
|
||||
@AllArguments Object[] allArguments) {
|
||||
try {
|
||||
accessor.setValue(new EnhancedClassInstanceContext());
|
||||
EnhancedClassInstanceContext context = new EnhancedClassInstanceContext();
|
||||
accessor.setValue(context);
|
||||
ConstructorContext interceptorContext = new ConstructorContext(
|
||||
allArguments);
|
||||
interceptor.onConstruct(null, interceptorContext);
|
||||
interceptor.onConstruct(context, interceptorContext);
|
||||
} catch (Throwable t) {
|
||||
logger.error("ClassConstructorInterceptor failue.", t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface FieldGetter {
|
||||
Object getValue();
|
||||
}
|
||||
|
||||
public interface FieldSetter {
|
||||
void setValue(Object value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,8 +19,6 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.PluginCfg;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.ClassConstructorInterceptor.FieldGetter;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.ClassConstructorInterceptor.FieldSetter;
|
||||
|
||||
public class EnhanceClazz4Interceptor {
|
||||
private static Logger logger = LogManager
|
||||
|
|
@ -76,15 +74,14 @@ public class EnhanceClazz4Interceptor {
|
|||
ClassLoadingStrategy.Default.INJECTION).getLoaded();
|
||||
|
||||
/**
|
||||
* to classloader. <br/>
|
||||
* create a new class using origin classname.<br/>
|
||||
*
|
||||
* new class need:<br/>
|
||||
* 1.add field '_$EnhancedClassInstanceContext' of type
|
||||
* EnhancedClassInstanceContext <br/>
|
||||
*
|
||||
* 2.intercept constructor and method if required by
|
||||
* interceptorDefineClass. use '@FieldValue' get
|
||||
* '_$EnhancedClassInstanceContext' ref<br/>
|
||||
* 2.intercept constructor by default, and intercept method which it's required by
|
||||
* interceptorDefineClass. <br/>
|
||||
*/
|
||||
IAroundInterceptor interceptor = define.instance();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 被增强的类实例,需扩展的context属性,用于在不同的方法,或者构造函数间保存实例
|
||||
|
|
@ -12,7 +12,22 @@ import java.util.Map;
|
|||
public class EnhancedClassInstanceContext {
|
||||
public static final String FIELD_NAME = "_$EnhancedClassInstanceContext";
|
||||
|
||||
private Map<Object, Object> context = new HashMap<Object, Object>();
|
||||
private Map<Object, Object> context = new ConcurrentHashMap<Object, Object>();
|
||||
|
||||
public void set(Object key, Object value){
|
||||
context.put(key, value);
|
||||
}
|
||||
|
||||
public Object get(Object key){
|
||||
return context.get(key);
|
||||
}
|
||||
|
||||
public boolean isContain(Object key){
|
||||
return context.containsKey(key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(Object key, Class<T> type){
|
||||
return (T)this.get(key);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
public interface FieldGetter {
|
||||
Object getValue();
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
public interface FieldSetter {
|
||||
void setValue(Object value);
|
||||
}
|
||||
|
|
@ -16,6 +16,10 @@ public class InterceptorContext {
|
|||
return this.allArguments;
|
||||
}
|
||||
|
||||
public String methodName(){
|
||||
return methodName;
|
||||
}
|
||||
|
||||
public Object inst(){
|
||||
return objInst;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,25 @@
|
|||
package com.ai.cloud.skywalking.plugin.interceptor;
|
||||
|
||||
public interface InterceptorDefine {
|
||||
/**
|
||||
* 返回要被增强的类,应当返回类全名
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getBeInterceptedClassName();
|
||||
|
||||
/**
|
||||
* 返回需要被增强的方法列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String[] getBeInterceptedMethods();
|
||||
|
||||
/**
|
||||
* 返回增强拦截器的实现<br/>
|
||||
* 每个拦截器在同一个被增强类的内部,保持单例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public IAroundInterceptor instance();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,17 +9,18 @@ public class TestAroundInterceptor implements IAroundInterceptor {
|
|||
|
||||
@Override
|
||||
public void onConstruct(EnhancedClassInstanceContext context, ConstructorContext interceptorContext) {
|
||||
context.set("test.key", "123");
|
||||
System.out.println("onConstruct, args size=" + interceptorContext.allArguments().length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InterceptorContext interceptorContext) {
|
||||
System.out.println("beforeMethod : " + context);
|
||||
System.out.println("beforeMethod : " + context.get("test.key", String.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterMethod(EnhancedClassInstanceContext context, InterceptorContext interceptorContext) {
|
||||
System.out.println("afterMethod: " + context);
|
||||
System.out.println("afterMethod: " + context.get("test.key", String.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue