I am implementing an IdChecker program who check the uniqueness of "id's" for classes and methods of a project. The IdChecker run only at compilation of my project...before I push a release.
For the classes it is easy to get the id's because I just need to map all my classes then invoke MyClass.getField("CLASS_ID").getShort(MyClass.getField("CLASS_ID")); then I create a ClassWrapper who contain the className and the classIdentifier.
For the methods, it's a little bit more complicated, because I can't use reflection to access a variable who is local to a method...but...in my project, I use a debuglogger to log every "entering-the-method" and "leaving-the-method". To get the local variable I want to invoke a method then read in my logBuffer the last entry (who is the "leaving-the-method" log entry) from this log entry, I get the METHOD_ID.
My problem:
I can't invoke the method on the class it generate an IllegalArgumentException... I don't need to pass parameters to the method, I just want to invoke them that I can generate a log entry.
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;
}
.
.
.