我想得到调用方法java.lang.reflect.Method
。 不是 方法的名称。
这是一个如何获取调用方类的示例。
// find the callers class
Thread t = Thread.getCurrentThread();
Class<?> klass = Class.forName(t.getStackTrace()[2].getClassName());
// do something with the class (like processing its annotations)
...
仅用于测试目的!
如果仅用于测试,则可能有效。它假定可以通过调用类的类访问类文件ClassLoader
,并且类文件已使用调试符号进行编译(我希望它们用于测试!)。此代码依赖于ASM字节码库。
public static Method getMethod(final StackTraceElement stackTraceElement) throws Exception {
final String stackTraceClassName = stackTraceElement.getClassName();
final String stackTraceMethodName = stackTraceElement.getMethodName();
final int stackTraceLineNumber = stackTraceElement.getLineNumber();
Class<?> stackTraceClass = Class.forName(stackTraceClassName);
// I am only using AtomicReference as a container to dump a String into, feel free to ignore it for now
final AtomicReference<String> methodDescriptorReference = new AtomicReference<String>();
String classFileResourceName = "/" + stackTraceClassName.replaceAll("\\.", "/") + ".class";
InputStream classFileStream = stackTraceClass.getResourceAsStream(classFileResourceName);
if (classFileStream == null) {
throw new RuntimeException("Could not acquire the class file containing for the calling class");
}
try {
ClassReader classReader = new ClassReader(classFileStream);
classReader.accept(
new EmptyVisitor() {
@Override
public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) {
if (!name.equals(stackTraceMethodName)) {
return null;
}
return new EmptyVisitor() {
@Override
public void visitLineNumber(int line, Label start) {
if (line == stackTraceLineNumber) {
methodDescriptorReference.set(desc);
}
}
};
}
},
0
);
} finally {
classFileStream.close();
}
String methodDescriptor = methodDescriptorReference.get();
if (methodDescriptor == null) {
throw new RuntimeException("Could not find line " + stackTraceLineNumber);
}
for (Method method : stackTraceClass.getMethods()) {
if (stackTraceMethodName.equals(method.getName()) && methodDescriptor.equals(Type.getMethodDescriptor(method))) {
return method;
}
}
throw new RuntimeException("Could not find the calling method");
}
我是的新手,并编写了以下代码以从组中获取参数。 跳过:testMethodA java.lang.NullPoInterException(位于org.testng.internal.methodInvocationHelper.InvokedDataProvider(MethodInvocationHelper.java:151)(位于org.testng.internal.parameters
问题内容: Python:如何在被调用方法中获取调用者的方法名称? 假设我有2种方法: 如果我不想对method1进行任何更改,如何在method2中获取调用者的名称(在本示例中,名称为method1)? 问题答案: inspect.getframeinfo和其他相关功能可以帮助: 该自省旨在帮助调试和开发;建议不要将其用于生产功能。
本文向大家介绍ASP.NET中实现获取调用方法名,包括了ASP.NET中实现获取调用方法名的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了ASP.NET中实现获取调用方法名的技巧。分享给大家供大家参考。具体实现方法如下: 在写记录日志功能时,需要记录日志调用方所在的模块名、命名空间名、类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下:
问题内容: Python:如何在被调用方法中获取调用者的方法名称? 假设我有2种方法: 如果我不想对method1进行任何更改,如何在method2中获取调用方的名称(在本示例中,名称为method1)? 问题答案: 和其他相关功能可以帮助: 该自省旨在帮助调试和开发;建议不要出于生产功能目的而依赖它。
我有以下类(请注意,方法是静态的): 当我打电话的时候: 我想得到输出:
问题内容: 我想我在这里描述的可能有个名字,但我不知道。所以我的第一个问题是要知道这种技术的名称。 这是一个示例:假设您正在网页上实现实时搜索。每次用户在搜索框中键入内容时,您都会触发一个新的搜索查询,并且结果会尽可能频繁地更新。这是一件愚蠢的事情,因为您发送的查询会超出实际需要。每2-3个字母发送一次请求,或者每100 ms最多发送一次请求就足够了。 因此,一种技术是安排在键入键之后立即执行的查