How do I correctly call the method when I know the class?


Mo Fubo

I'm implementing an IdChecker program that checks the uniqueness of the "id" of an item's classes and methods. IdChecker only runs when my project compiles...before I release a build.

For classes, it's easy to get the ID, because I just need to map all the classes and call MyClass.getField("CLASS_ID"). getShort(MyClass.getField("CLASS_ID")); then create a ClassWrapper containing className and classIdentifier.

For methods it's a bit more complicated because I can't use reflection to access method local variables...but...in my project I use the debugger to log every "enter--method" and "leave-method" . To get the local variables of the method I'm calling, and then read the last entry in my logBuffer from that log entry (it's the "leaving method" log entry), I get the METHOD_ID.

my question:

I can't call the method on the class that generates the IllegalArgumentException...I don't need to pass arguments to the method, I just want to call them to generate log entries.

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;
            }

        .
        .
        .
Vitruvius

method.invoke(...) expects an array of arguments that can actually be used to invoke the method. In this case, you give it {null} and it works for any function that accepts a single Object, but not any other function. To call an arbitrary function, you need to match the length of the argument list to the number of arguments the function requires:

method.invoke(..., new Object[method.getParameterTypes().length])

There are still some problems with this. First, the first argument to invoke is not the class you entered, but an instance of an object of that class (null for static methods). You may need to search for the class's constructor and construct an instance of the class before you can call the class's instance methods. The second problem is that this doesn't work for functions that require primitives (which cannot be null). You can inspect the class of the parameter and replace null with 0 or false as appropriate. The third and most important problem, and the reason it's so hard to do, is that you don't actually want to call the method. You want to retrieve information about the method, which in this case happens to be stored inside the method. The correct solution is to just store the information attached to the method, with annotations:

@Retention(RetentionPolicy.RUNTIME)
@interface ID {
    int value();
}
...
@ID(1337)
void exampleMethod() { ... }
...
newIdentifier = method.getAnnotation(ID.class).value(); //returns 1337 for exampleMethod

Related


How do I correctly call the method when I know the class?

Mo Fubo I'm implementing an IdChecker program that checks the uniqueness of the "id" of an item's classes and methods. IdChecker only runs when my project compiles...before I release a build. For classes, it's easy to get the ID, because I just need to map all

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the method starts

Hax0r Now I am trying to benchmark my application. So I wrote a simple code. Below is my code $startTime = microtime(); $endTime = microtime(); print $endTime - $startTime I don't think the way I work is efficient, so I want to improve. I have to test severa

How do I know when the Ajax call is done with getScript?

SBB I am using jQuery getScriptto load X number of js files into my page. In each JS page there is an AJAX call that fetches data from the database. I'm using that .donemethod getScriptto see when all files have been loaded, but I need to wait for all AJAX cal

Async call to WriteAsync, how do I know when it's done?

bob machine I write the file to IsolatedStorageFile via an asynchronous call async void Base64ToImage() { byte[] data; using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = i

How do I know when the Ajax call is done with getScript?

SBB I am using jQuery getScriptto load X number of js files into my page. In each JS page there is an AJAX call that fetches data from the database. I'm using that .donemethod getScriptto see when all files have been loaded, but I need to wait for all AJAX cal

How do I know if crontab is working correctly?

LinuxSecurityFreak Is there a crontab log that will let me know if the crontab is working fine? I'm on Linux Mint 17.3. what did I do: crontab -erun as root It didn't exist so I chose nano editor and continued into this line00 12 * * * /home/vlastimil/Developm

How do I know if crontab is working correctly?

LinuxSecurityFreak Is there a crontab log that will let me know if the crontab is working fine? I'm on Linux Mint 17.3. what did I do: crontab -erun as root It didn't exist so I chose nano editor and continued into this line00 12 * * * /home/vlastimil/Developm

How do I know when a JavaScript method ends

Pull 76 I just want to learn Ext Js. I have something like: handler: function (checkBox, checked) { setLoading(true); grid1.filterExpired(checked); grid2.filterExpired(checked); grid3.filterE

How do I know when to use a support class?

Alexey Andruskevich I'm new to Android platform development and trying to understand how to know or predict when a support class has to be used? This may be stupid question, but let me give a simple example. Let's say I want to add sharing functionality to my

How do I know when to use a support class?

Alexey Andrushkevich I'm new to Android platform development and trying to understand how to know or predict when a support class has to be used? This may be stupid question, but let me give a simple example. Let's say I want to add sharing functionality to my

How do I know when to use a support class?

Alexey Andruskevich I'm new to Android platform development and trying to understand how to know or predict when a support class has to be used? This may be stupid question, but let me give a simple example. Let's say I want to add sharing functionality to my

How do I know when to use a support class?

Alexey Andrushkevich I'm new to Android platform development and trying to understand how to know or predict when a support class has to be used? This may be stupid question, but let me give a simple example. Let's say I want to add sharing functionality to my

How do I know when to use a support class?

Alexey Andruskevich I'm new to Android platform development and trying to understand how to know or predict when a support class has to be used? This may be stupid question, but let me give a simple example. Let's say I want to add sharing functionality to my