remove unnecessary ClassLoader.forName

pull/1182/head
hengyunabc 5 years ago
parent 9660ad1ea0
commit ee0042cfc6

@ -31,7 +31,7 @@ public interface AdviceListener {
* @throws Throwable
*/
void before(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args) throws Throwable;
/**
@ -49,7 +49,7 @@ public interface AdviceListener {
* @throws Throwable
*/
void afterReturning(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args,
Object returnObject) throws Throwable;
@ -67,7 +67,7 @@ public interface AdviceListener {
* @throws Throwable
*/
void afterThrowing(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args,
Throwable throwable) throws Throwable;

@ -58,196 +58,6 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
};
/**
* <br/>
* ,
*
* @param loader
* @param adviceId ID
* @param className
* @param methodName
* @param methodDesc
* @param target
* (void),null
* @param args
*/
public static void methodOnBegin(
int adviceId,
ClassLoader loader, String className, String methodName, String methodDesc,
Object target, Object[] args) {
if (isSelfCallRef.get()) {
return;
} else {
isSelfCallRef.set(true);
}
try {
// 构建执行帧栈,保护当前的执行现场
final GaStack<Object> frameStack = new ThreadUnsafeFixGaStack<Object>(FRAME_STACK_SIZE);
frameStack.push(loader);
frameStack.push(className);
frameStack.push(methodName);
frameStack.push(methodDesc);
frameStack.push(target);
frameStack.push(args);
final AdviceListener listener = getListener(adviceId);
frameStack.push(listener);
// 获取通知器并做前置通知
before(listener, loader, className, methodName, methodDesc, target, args);
// 保护当前执行帧栈,压入线程帧栈
threadFrameStackPush(frameStack);
} finally {
isSelfCallRef.set(false);
}
}
/**
* <br/>
* ,
*
* @param returnObject
* ,null
*/
public static void methodOnReturnEnd(Object returnObject) {
methodOnEnd(false, returnObject);
}
/**
* <br/>
* ,
*
* @param throwable
*/
public static void methodOnThrowingEnd(Throwable throwable) {
methodOnEnd(true, throwable);
}
/**
*
*
* @param isThrowing
* @param returnOrThrowable
*/
private static void methodOnEnd(boolean isThrowing, Object returnOrThrowable) {
if (isSelfCallRef.get()) {
return;
} else {
isSelfCallRef.set(true);
}
try {
// 弹射线程帧栈,恢复Begin所保护的执行帧栈
final GaStack<Object> frameStack = threadFrameStackPop();
// 弹射执行帧栈,恢复Begin所保护的现场
final AdviceListener listener = (AdviceListener) frameStack.pop();
final Object[] args = (Object[]) frameStack.pop();
final Object target = frameStack.pop();
final String methodDesc = (String) frameStack.pop();
final String methodName = (String) frameStack.pop();
final String className = (String) frameStack.pop();
final ClassLoader loader = (ClassLoader) frameStack.pop();
// 异常通知
if (isThrowing) {
afterThrowing(listener, loader, className, methodName, methodDesc, target, args, (Throwable) returnOrThrowable);
}
// 返回通知
else {
afterReturning(listener, loader, className, methodName, methodDesc, target, args, returnOrThrowable);
}
} finally {
isSelfCallRef.set(false);
}
}
/**
*
*
* @param adviceId ID
* @param owner
* @param name
* @param desc
*/
public static void methodOnInvokeBeforeTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeBeforeTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice before tracing failed.", t);
}
}
}
/**
* ()
*
* @param adviceId ID
* @param owner
* @param name
* @param desc
*/
public static void methodOnInvokeAfterTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeAfterTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice after tracing failed.", t);
}
}
}
/**
* ()
*
* @param adviceId ID
* @param owner
* @param name
* @param desc
*/
public static void methodOnInvokeThrowTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeThrowTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice throw tracing failed.", t);
}
}
}
/*
* 线<br/>
* 线
*/
private static void threadFrameStackPush(GaStack<Object> frameStack) {
GaStack<GaStack<Object>> threadFrameStack = threadBoundContext.get();
if (null == threadFrameStack) {
threadBoundContext.set(threadFrameStack = new ThreadUnsafeGaStack<GaStack<Object>>());
}
threadFrameStack.push(frameStack);
}
private static GaStack<Object> threadFrameStackPop() {
return threadBoundContext.get().pop();
}
private static AdviceListener getListener(int adviceId) {
return advices.get(adviceId);
}
/**
*
*
@ -302,45 +112,6 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
return advices.remove(adviceId);
}
private static void before(AdviceListener listener,
ClassLoader loader, String className, String methodName, String methodDesc,
Object target, Object[] args) {
if (null != listener) {
try {
listener.before(loader, className, methodName, methodDesc, target, args);
} catch (Throwable t) {
logger.warn("advice before failed.", t);
}
}
}
private static void afterReturning(AdviceListener listener,
ClassLoader loader, String className, String methodName, String methodDesc,
Object target, Object[] args, Object returnObject) {
if (null != listener) {
try {
listener.afterReturning(loader, className, methodName, methodDesc, target, args, returnObject);
} catch (Throwable t) {
logger.warn("advice returning failed.", t);
}
}
}
private static void afterThrowing(AdviceListener listener,
ClassLoader loader, String className, String methodName, String methodDesc,
Object target, Object[] args, Throwable throwable) {
if (null != listener) {
try {
listener.afterThrowing(loader, className, methodName, methodDesc, target, args, throwable);
} catch (Throwable t) {
logger.warn("advice throwing failed.", t);
}
}
}
private final int adviceId;
private final boolean isTracing;
private final boolean skipJDKTrace;

@ -130,26 +130,23 @@ public abstract class ReflectAdviceListenerAdapter implements AdviceListener , P
@Override
final public void before(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args) throws Throwable {
final Class<?> clazz = toClass(loader, className);
before(loader, clazz, toMethod(loader, clazz, methodName, methodDesc), target, args);
before(clazz.getClassLoader(), clazz, toMethod(clazz.getClassLoader(), clazz, methodName, methodDesc), target, args);
}
@Override
final public void afterReturning(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args, Object returnObject) throws Throwable {
final Class<?> clazz = toClass(loader, className);
afterReturning(loader, clazz, toMethod(loader, clazz, methodName, methodDesc), target, args, returnObject);
afterReturning(clazz.getClassLoader(), clazz, toMethod(clazz.getClassLoader(), clazz, methodName, methodDesc), target, args, returnObject);
}
@Override
final public void afterThrowing(
ClassLoader loader, String className, String methodName, String methodDesc,
Class<?> clazz, String methodName, String methodDesc,
Object target, Object[] args, Throwable throwable) throws Throwable {
final Class<?> clazz = toClass(loader, className);
afterThrowing(loader, clazz, toMethod(loader, clazz, methodName, methodDesc), target, args, throwable);
afterThrowing(clazz.getClassLoader(), clazz, toMethod(clazz.getClassLoader(), clazz, methodName, methodDesc), target, args, throwable);
}

@ -39,7 +39,7 @@ public class SpyImpl extends AbstractSpy {
if (skipAdviceListener(adviceListener)) {
continue;
}
adviceListener.before(classLoader, clazz.getName(), methodName, methodDesc, target, args);
adviceListener.before(clazz, methodName, methodDesc, target, args);
} catch (Throwable e) {
if (logger.isDebugEnabled()) {
logger.error("class: {}, methodInfo: {}", clazz.getName(), methodInfo, e);
@ -66,8 +66,7 @@ public class SpyImpl extends AbstractSpy {
if (skipAdviceListener(adviceListener)) {
continue;
}
adviceListener.afterReturning(classLoader, clazz.getName(), methodName, methodDesc, target, args,
returnObject);
adviceListener.afterReturning(clazz, methodName, methodDesc, target, args, returnObject);
} catch (Throwable e) {
if (logger.isDebugEnabled()) {
logger.error("class: {}, methodInfo: {}", clazz.getName(), methodInfo, e);
@ -93,8 +92,7 @@ public class SpyImpl extends AbstractSpy {
if (skipAdviceListener(adviceListener)) {
continue;
}
adviceListener.afterThrowing(classLoader, clazz.getName(), methodName, methodDesc, target, args,
throwable);
adviceListener.afterThrowing(clazz, methodName, methodDesc, target, args, throwable);
} catch (Throwable e) {
if (logger.isDebugEnabled()) {
logger.error("class: {}, methodInfo: {}", clazz.getName(), methodInfo, e);

Loading…
Cancel
Save