Skip to content

Commit

Permalink
fix: 修复问题#426
Browse files Browse the repository at this point in the history
  • Loading branch information
dukun committed Jun 16, 2023
1 parent bf4bd9a commit 5f1bb1b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.jvm.sandbox.api.listener.ext;

import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.api.event.InvokeEvent;
import com.alibaba.jvm.sandbox.api.util.LazyGet;
Expand All @@ -21,6 +22,9 @@ public class Advice implements Attachment {
private final int invokeId;

private final ClassLoader loader;
private final String javaClassName;
private final String javaMethodName;
private final String javaMethodDesc;
private final LazyGet<Behavior> behaviorLazyGet;
private final Object[] parameterArray;
private final Object target;
Expand All @@ -44,7 +48,9 @@ public class Advice implements Attachment {
* @param loader 触发事件的行为所在ClassLoader
* @param parameterArray 触发事件的行为入参
* @param target 触发事件所归属的对象实例
* @deprecated 建议使用 {@link Advice#Advice(BeforeEvent, LazyGet)}
*/
@Deprecated
Advice(final int processId,
final int invokeId,
final LazyGet<Behavior> behaviorLazyGet,
Expand All @@ -57,6 +63,29 @@ public class Advice implements Attachment {
this.loader = loader;
this.parameterArray = parameterArray;
this.target = target;
this.javaClassName = null;
this.javaMethodName = null;
this.javaMethodDesc = null;
}

/**
* 构造通知
*
* @param bEvent Before事件
* @param behaviorLazyGet 触发事件的行为(懒加载)
* @since {@code sandbox-api:1.4.1}
*/
Advice(final BeforeEvent bEvent,
final LazyGet<Behavior> behaviorLazyGet) {
this.processId = bEvent.processId;
this.invokeId = bEvent.invokeId;
this.loader = bEvent.javaClassLoader;
this.parameterArray = bEvent.argumentArray;
this.target = bEvent.target;
this.javaClassName = bEvent.javaClassName;
this.javaMethodName = bEvent.javaMethodName;
this.javaMethodDesc = bEvent.javaMethodDesc;
this.behaviorLazyGet = behaviorLazyGet;
}

/**
Expand Down Expand Up @@ -195,6 +224,36 @@ public Throwable getThrowable() {
return throwable;
}

/**
* 获取触发调用事件的类名称
*
* @return 触发调用事件的类名称
* @since {@code sandbox-api:1.4.1}
*/
public String getJavaClassName() {
return javaClassName;
}

/**
* 获取触发调用事件的方法名称
*
* @return 触发调用事件的方法名称
* @since {@code sandbox-api:1.4.1}
*/
public String getJavaMethodName() {
return javaMethodName;
}

/**
* 获取触发调用事件的方法签名
*
* @return 触发调用事件的方法签名
* @since {@code sandbox-api:1.4.1}
*/
public String getJavaMethodDesc() {
return javaMethodDesc;
}

@Override
public void attach(final Object attachment) {
this.attachment = attachment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ private void switchEvent(final OpStack opStack,
final BeforeEvent bEvent = (BeforeEvent) event;
final ClassLoader loader = toClassLoader(bEvent.javaClassLoader);
final Advice advice = new Advice(
bEvent.processId,
bEvent.invokeId,
bEvent,
new LazyGet<Behavior>() {

private final ClassLoader _loader = loader;
Expand All @@ -68,10 +67,7 @@ protected Behavior initialValue() throws Throwable {
_javaMethodDesc
);
}
},
loader,
bEvent.argumentArray,
bEvent.target
}
);

final Advice top;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ public class AdviceListenerTestCase {
.onWatch(new AdviceListener(){
@Override
protected void before(Advice advice) throws Throwable {
Assert.assertEquals("java.lang.String", advice.getJavaClassName());
Assert.assertEquals("toString", advice.getJavaMethodName());
Assert.assertEquals("()Ljava/lang/String;", advice.getJavaMethodDesc());
traceSB.append("before;");
}

@Override
protected void afterReturning(Advice advice) throws Throwable {
Assert.assertEquals("java.lang.String", advice.getJavaClassName());
Assert.assertEquals("toString", advice.getJavaMethodName());
Assert.assertEquals("()Ljava/lang/String;", advice.getJavaMethodDesc());
traceSB.append("afterReturning;");
}

@Override
protected void afterThrowing(Advice advice) throws Throwable {
Assert.assertEquals("java.lang.String", advice.getJavaClassName());
Assert.assertEquals("toString", advice.getJavaMethodName());
Assert.assertEquals("()Ljava/lang/String;", advice.getJavaMethodDesc());
traceSB.append("afterThrowing;");
}
});
Expand Down

0 comments on commit 5f1bb1b

Please sign in to comment.