Java 使用反射从动态方法获取参数类型
我有一些代码可以从动态类中获得setter方法。但是参数可以是Java 使用反射从动态方法获取参数类型,java,reflection,Java,Reflection,我有一些代码可以从动态类中获得setter方法。但是参数可以是java.lang.String或java.lang.Long。如何动态获取参数类型 public Method getDynMethod(Class aClass, String methodName) { Method m = null; Class[] paramTypes = new Class[1]; paramTypes[0] = String.class; try { m
java.lang.String
或java.lang.Long
。如何动态获取参数类型
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;
}
这是调用它的代码
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();
}
}
我可以将另一个参数传递给该方法,但我仍然不知道参数类型是什么您可以从
method.getParameterTypes()
获取该方法的参数类型;即:
public Class[] methodsParamsTypes(Method method) {
return method.getParameterTypes();
}
有关完整示例,请参见
编辑
现在再次阅读你的问题,我不确定上面的答案是否就是你所看到的
您的意思是,您的代码中有来自映射的参数,并且希望通过反射调用适当的方法吗?带长
或字符串
的那一个?如果是这样,下面是一个例子:
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;
}
如果要获取方法的参数类型,应调用调用的方法 getParameterTypes() 返回预期参数的类对象数组 有关更多信息,请查看此处:
编辑:我得到了忍者版:-(你可以得到所有的方法,并按名称过滤,如下所示:
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;
}
public方法getDynMethod(类aClass,字符串methodName){
对于(方法m:aClass.getMethods()){
if(methodName.equals(m.getName())){
类[]params=m.getParameterTypes();
如果(参数长度==1
&&(参数[0]==Long.class | |参数[0]==String.class)){
返回m;
}
}
}
返回null;
}
你能把它作为另一个参数传递吗?getDynMethod(Class,String,Class[])
谢谢。这很有效。我现在确实有另一个问题。我在这一行得到一个空指针String result=(String)m.invoke(c.newInstance(),ap.getValue());那么我想是时候使用交互式调试器并设置断点了。没有任何工具像调试器那样可以帮助您了解正在发生的事情,尤其是对于空引用。
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;
}