简体   繁体   中英

Passing the primitive type and calling methods via reflection

I am working on a kind of Framework. Here I will have to call a particular method on a particular class with particular parameters. I will receive parameter values in string form.

like inputs will be,


class name : com.classes.MyClass

method name : setPrimtiveType

Value : "true"

type of value : boolean


Here the problem is that the method I am trying to call via reflection accepts primitive type. this type is also an input to set the particular parameter.

I am not getting any in reflection so this can be achieved.

if this is your class

class MyClass {
    public static void setPrimitiveType(boolean v) {
    }
}

then this is how to set boolean value

MyClass.class.getMethod("setPrimitiveType", boolean.class).invoke(null, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM