我採取誰檢查的「ID的」類和項目的方法獨特的IdChecker程序。 IdChecker只在編譯我的項目時運行......在我推送發行版之前。因爲我只需要映射我所有的類,然後調用MyClass.getField(「CLASS_ID」)。getShort(MyClass.getField(「CLASS_ID」));然後我創建一個包含className和classIdentifier的ClassWrapper。如何在我知道該類時正確調用方法?
對於方法,它更復雜一點,因爲我不能使用反射來訪問一個變量誰是本地的一個方法......但在我的項目,我用debuglogger記錄每個「進入方法」和「離開方法」。爲了得到局部變量我想調用一個方法,然後在我的日誌緩衝區讀取的最後一個條目(誰是「留下最法」日誌條目)從該日誌條目,我得到的METHOD_ID。
我的問題:
我不能調用類上的方法,它生成IllegalArgumentException ... 我不需要參數傳遞給方法,我只是想調用它們,我可以生成一個日誌條目。
private void getMethodsList() throws IllegalArgumentException,
IllegalAccessException, NoSuchFieldException, SecurityException {
final short METHOD_ID = 0x03;
/* Log-entering the method */
mLogger.logDebug((byte) 1, METHOD_ID);
/* Create the MethodWrapper list corresponding
to each element of the ClassWrapper list */
for(Class<?> clazz : mClasses)
{
/* Get the declared methods from each class */
for(Method method : clazz.getDeclaredMethods())
{
/* Get the name of the method */
String newName = method.getName();
short newIdentifier = 0;
try
{
method.invoke(clazz, new Object[]{null});
/* Get the identifier of the class */
newIdentifier = AbstractDebugLogger.mLastMethodID;
}
.
.
.
請閱讀'Method#invoke(..)'的javadoc。它非常清楚它期望什麼作爲論據。 –
它可能是更正確使用自定義的註釋(例如'@MethodId(0×03)')這種情況。 –