Monday, November 7, 2016

Class.getDeclaredMethod with primitive types (basic types)

Lets have to get method object of this function:
    private void setI(int i) {
        this.i = i;
    }

Sending Integer.class in argument list cause throwing an exception: java.lang.NoSuchMethodException:

The correct way is this:
    Method iMethod = progClass.getDeclaredMethod(
        "setI", new Class[]{ Integer.TYPE } );

Same pattern could be applied for byte, short, long, float, double, boolean, char.

No comments:

Post a Comment