Получить метод callers (java.lang.reflect.Method)
Я хотел бы получить метод вызова 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)
...
Это только для целей тестирования!
Ответы
Ответ 1
Если это просто для тестирования, это может сработать. Предполагается, что файлы классов доступны через вызывающий класс 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");
}
Ответ 2
Мы можем почти добраться туда, здесь метод, который работает во многих случаях. Проблема в том, что она не будет работать надежно, если есть перегруженные методы (несколько методов с тем же именем). К сожалению, трассировка стека не дает аргументов.
private static Method getCallingMethod() throws ClassNotFoundException{
final Thread t = Thread.currentThread();
final StackTraceElement[] stackTrace = t.getStackTrace();
final StackTraceElement ste = stackTrace[2];
final String methodName = ste.getMethodName();
final String className = ste.getClassName();
Class<?> kls = Class.forName(className);
do{
for(final Method candidate : kls.getDeclaredMethods()){
if(candidate.getName().equals(methodName)){
return candidate;
}
}
kls = kls.getSuperclass();
} while(kls != null);
return null;
}
Тестовый код:
public static void main(final String[] args) throws Exception{
System.out.println(getCallingMethod());
}
Вывод:
public static void foo.bar.Phleem.main(java.lang.String []) throws java.lang.Exception
ОК, вот решение, использующее ASM. Он работает практически во всех случаях:
private static Method getCallingMethod() throws ClassNotFoundException,
IOException{
final Thread t = Thread.currentThread();
final StackTraceElement[] stackTrace = t.getStackTrace();
final StackTraceElement ste = stackTrace[2];
final String methodName = ste.getMethodName();
final int lineNumber = ste.getLineNumber();
final String className = ste.getClassName();
final Class<?> kls = Class.forName(className);
final ClassReader cr = new ClassReader(className);
final EmptyVisitor empty = new EmptyVisitor();
final AtomicReference<Method> holder = new AtomicReference<Method>();
cr.accept(new ClassAdapter(empty){
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String desc,
final String signature,
final String[] exceptions){
return name.equals(methodName) ? new MethodAdapter(empty){
@Override
public void visitLineNumber(final int line,
final Label start){
if(line >= lineNumber && holder.get() == null){
final Type[] argumentTypes =
Type.getArgumentTypes(desc);
final Class<?>[] argumentClasses =
new Class[argumentTypes.length];
try{
for(int i = 0; i < argumentTypes.length; i++){
final Type type = argumentTypes[i];
final String dd = type.getDescriptor();
argumentClasses[i] = getClassFromType(type);
}
holder.set(kls.getDeclaredMethod(methodName,
argumentClasses));
} catch(final ClassNotFoundException e){
throw new IllegalStateException(e);
} catch(final SecurityException e){
throw new IllegalStateException(e);
} catch(final NoSuchMethodException e){
throw new IllegalStateException(e);
}
}
super.visitLineNumber(line, start);
}
private Class<?> getClassFromType(final Type type) throws ClassNotFoundException{
Class<?> javaType;
final String descriptor = type.getDescriptor();
if(type.equals(Type.INT_TYPE)){
javaType = Integer.TYPE;
} else if(type.equals(Type.LONG_TYPE)){
javaType = Long.TYPE;
} else if(type.equals(Type.DOUBLE_TYPE)){
javaType = Double.TYPE;
} else if(type.equals(Type.FLOAT_TYPE)){
javaType = Float.TYPE;
} else if(type.equals(Type.BOOLEAN_TYPE)){
javaType = Boolean.TYPE;
} else if(type.equals(Type.BYTE_TYPE)){
javaType = Byte.TYPE;
} else if(type.equals(Type.CHAR_TYPE)){
javaType = Character.TYPE;
} else if(type.equals(Type.SHORT_TYPE)){
javaType = Short.TYPE;
} else if(descriptor.startsWith("[")){
final Class<?> elementType =
getClassFromType(type.getElementType());
javaType =
Array.newInstance(elementType, 0).getClass();
} else{
javaType = Class.forName(type.getClassName());
}
return javaType;
}
}
: null;
}
},
0);
return holder.get();
}
Я оставлю это вам, чтобы реорганизовать это во что-то читаемое. И это не сработает, если подпись вызывающего метода содержит примитивные массивы или многомерные массивы. Очевидно, что он работает только в том случае, если файл класса содержит номера строк.
Аргх, я работаю целую вечность, а потом вижу, что кто-то придумал почти идентичное решение!!! Во всяком случае, я оставлю свое, потому что я разработал его самостоятельно.
Ответ 3
довольно просто: сначала сначала получите соответствующий объект класса, а затем используйте Class.getMethod(String name,params...)
проверьте здесь для javadoc
public class GetMethod {
public static void main(String[] args){
new GetMethod().checkMethod();
}
public void checkMethod(){
Thread t=Thread.currentThread();
StackTraceElement element=t.getStackTrace()[1];
System.out.println(element.getClassName());
System.out.println(element.getMethodName());
try{
Method m=Class.forName(element.getClassName()).getMethod(element.getMethodName(),null);
System.out.println("Method: " + m.getName());
}catch (Exception e) {
e.printStackTrace();
}
}
}
надеюсь, что это помогло....
Ответ 4
Вот модифицированная версия Sean Patrick Floyd, опубликованная методом получения класса Java из типа ASM. Он исправляет проблему с многомерными массивами и еще одну проблему с классами, загруженными другими загрузчиками классов.
public static Class<?> getClassFromType(Class<?> clazz, final Type type) throws ClassNotFoundException{
Class<?> javaType = null;
switch( type.getSort() ) {
case Type.VOID : javaType = Void.TYPE; break;
case Type.BOOLEAN : javaType = Boolean.TYPE; break;
case Type.CHAR : javaType = Character.TYPE; break;
case Type.BYTE : javaType = Byte.TYPE; break;
case Type.SHORT : javaType = Short.TYPE; break;
case Type.INT : javaType = Integer.TYPE; break;
case Type.FLOAT : javaType = Float.TYPE; break;
case Type.LONG : javaType = Long.TYPE; break;
case Type.DOUBLE : javaType = Double.TYPE; break;
case Type.ARRAY : javaType = Array.newInstance( getClassFromType( clazz, type.getElementType()), new int[type.getDimensions()] ).getClass(); break;
case Type.OBJECT : javaType = Class.forName( type.getClassName(), false, clazz.getClassLoader() ); break;
}
if ( javaType != null ) return javaType;
throw new ClassNotFoundException( "Couldn't find class for type " + type );
}