我有这样的代码:Java反射问题
public static final <TypeVO extends BaseVo> List<SelectItem> populateSelectBoxForType(
final Class<TypeVO> voClass, final String fieldName) {
List<SelectItem> listSelectBox = null;
final List<TypeVO> vosList = GenericEjbProxyFactory
.getGenericTopValueObjectProxy(voClass)
.getAllValueObjects(null);
System.out.println("loaded vosList!!!!");
if (vosList != null) {
listSelectBox = new ArrayList<SelectItem>();
for (final TypeVO currVo : vosList) {
listSelectBox.add(new SelectItem(currVo.getInternalId(), currVo.getName()));
}
}
return listSelectBox;
}
正如你看到这里,我使用currVo.getName因为总是currVo有一个名称属性。
我希望能够还使用其他领域,从这个currVo其类型为voClass,但不是所有currVo类将包含这个字段,所以我不得不使用反射来识别这些getfield命令的方法,是这样的:
for (final TypeVO currVo : vosList) {
for (final Method m : voClass.getMethods()) {
if (m.getName().contains(fieldName)) {
listSelectBox.add(new SelectItem(
currVo.getInternalId(), currVo.m));
}
}
}
我不知道的是当我找到它时,如何使用该特定方法的值,完全像currVo.getName(因为当然,currVo.m错误)?
例如:如果字段名是 “时代” 我希望把名单:currVo.getAge()
...我只是在这里封锁...
'invoke'有两个参数。 – Mat 2011-06-02 11:33:26
@Mat:它需要vararg作为第二个参数,它可以是空的。 – axtavt 2011-06-02 11:34:06
啊,正确的JDK> = 1.5。 1.4.2有不同的签名。 – Mat 2011-06-02 11:39:31