java - Get parameter Type from dynamic method using reflection -


i've got bit of code setter method dynamic class. parameters can java.lang.string or java.lang.long. how can dynamically parameter type?

public method getdynmethod(class aclass, string methodname) {     method m = null;     class[] paramtypes = new class[1];     paramtypes[0] = string.class;     try {         m = aclass.getmethod(methodname, paramtypes);     } catch (nosuchmethodexception nsme) {         nsme.printstacktrace();     }     return m; } 

this code calls it

class c = getdynclass(a.getassettype().getdbtablename());         (long l : map.keyset()) {             assetproperties ap = new assetproperties();             ap.setassettypeproperties(em.find(assettypeproperty.class, l));             ap.setassets(a);             ap.setvalue(map.get(l));             a.getassetproperties().add(ap);             string methodname = "set" + ap.getassettypeproperties().getdbcolumn();             method m = getdynmethod(c, methodname);             try {                 string result = (string) m.invoke(c.newinstance(), ap.getvalue());                 system.out.println(result);             } catch (illegalaccessexception iae) {                 iae.printstacktrace();             } catch (invocationtargetexception ite) {                 ite.printstacktrace();             } catch (instantiationexception ie) {                 ie.printstacktrace();             }          } 

i pass parameter method still not know parameter type be

you methods, , filter name so:

public method getdynmethod(class aclass, string methodname) {    (method m : aclass.getmethods()) {        if (methodname.equals(m.getname())) {            class<?>[] params = m.getparametertypes();            if (params.length == 1                 && (params[0] == long.class || params[0] == string.class)) {                return m;            }        }     }      return null; } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -