简体   繁体   English

使用反射从动态方法中获取参数类型

[英]Get parameter Type from dynamic method using reflection

I've got this bit of code to get a setter method from a dynamic class.我有这段代码可以从动态类中获取 setter 方法。 But the parameters can be a java.lang.String or java.lang.Long .但参数可以是java.lang.Stringjava.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() ;您可以从method.getParameterTypes()获取方法的参数类型; ie: 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 ?Long还是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. getParameterTypes() 返回一个 Class 对象数组,这些对象是预期的参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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