简体   繁体   中英

Get parameter Type from dynamic method using reflection

I've got this bit of code to get a setter method from a dynamic class. But the parameters can be a java.lang.String or java.lang.Long . How can I dynamically get the 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 is the code that calls it

Class c = getDynClass(a.getAssetType().getDBTableName());
        for (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 could pass another parameter to the method but I still would not know what the parameter type would be

You can get method's parameters types from method.getParameterTypes() ; ie:

public Class[] methodsParamsTypes(Method method) {
    return method.getParameterTypes();
}

see here for a complete example.

Edit reading again your question now I'm unsure that the answer above is what you was looking.

Do you mean you have the parameter coming from a map in your code and you want to invoke the proper method by reflection? Either the one with Long or String ? if so here is an example:

public Method getDynMethod(Class aClass, String methodName, Object...params) {
    Method m = null;
    Class[] paramTypes = new Class[params.length];
    for (int i = 0; i < paramTypes.length; i++) {
        //note: if params[i] == null is not possible to retrieve the class type... 
        paramTypes[i] = params[i].getClass();
    }
    try {
        m = aClass.getMethod(methodName, paramTypes);
    } catch (NoSuchMethodException nsme) {
        nsme.printStackTrace();
    }
    return m;
}

and in your code you can invoke it like that:

Method m = getDynMethod(c, methodName, ap.getValue());

If you want to get the parameter type(s) of a method you should invoke the method called

getParameterTypes() that returns an array of Class objects that are the parameter(s) expected.

Have a look here for more information: Method class Documentation

EDIT: I got ninja'ed :- (

You could get all the methods, and filter by name like so:

public Method getDynMethod(Class aClass, String methodName) {
   for (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;
}

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