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
Aucun commentaire:
Enregistrer un commentaire