需求假設:假設在包com.zhoutao.controller下有方法getKey()方法,在JavaEE中,通過AOP獲得該方法的的對象method,現在通過該對象的getName方法,僅僅只能獲得getKey的結果,現在我需要的是com.zhoutao.controller.getKay 那么該如何獲取呢?
查找官方API並未發現此方法,現在我們來分析下將類名和方法名分開看:
結果 = 類名全稱 +"."+方法名
現在我們已經拿到方法名了,那么我們看看能不能找到這個方法的類,我們看一下Method的源碼:
public final class Method extends Executable { private Class<?> clazz; private int slot; // This is guaranteed to be interned by the VM in the 1.4 // reflection implementation private String name; private Class<?> returnType; private Class<?>[] parameterTypes; private Class<?>[] exceptionTypes; private int modifiers; // Generics and annotations support private transient String signature; // generic info repository; lazily initialized private transient MethodRepository genericInfo; private byte[] annotations; private byte[] parameterAnnotations; private byte[] annotationDefault; private volatile MethodAccessor methodAccessor; .....
通過源碼可以看到,內部提供了clazz,我們知道通過clazz可以獲得類名,但是這個clazz是私有的,我們怎么拿到它呢?
幸運的是,我們發現了下面的方法通過源碼可以看到,內部提供了clazz,我們知道通過clazz可以獲得類名,但是這個clazz是私有的,我們怎么拿到它呢?
幸運的是,我們發現了下面的方法:
/** * {@inheritDoc} */ @Override public Class<?> getDeclaringClass() { return clazz; }
結果 = 類名全稱 +"."+方法名 結果 = method.getDeclaringClass().getName()+"."+method.getName()