开发者

java reflection issue

开发者 https://www.devze.com 2023-03-10 03:18 出处:网络
I have this code: public static final <TypeVO extends BaseVo> List<SelectItem> populateSelectBoxForType(

I have this code:

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;
   开发者_JS百科 }

As you see here, I'm using currVo.getName because always, currVo has a name property.

I want to be able to use also other fields from this currVo which has type as voClass, but not all currVo classes will contain this field so I have to use reflection to identify these getField method, something like:

for (final TypeVO currVo : vosList) {
                for (final Method m : voClass.getMethods()) {
                    if (m.getName().contains(fieldName)) {
                        listSelectBox.add(new SelectItem(
                                currVo.getInternalId(), currVo.m));
                    }
                }
            }

What I do not know is HOW I can use that specific method's value when I find it, exactly like currVo.getName (because, of course, currVo.m is wrong)?

Eg: If fieldName is "Age" I want to put in the list: currVo.getAge()... I am simply blocked here...


m.invoke(currVo);

See also:

  • Method javadoc

Also note the correct way to look for a method as suggested by Nik and Bohemian.


Do I understand it correctly that you want to invoke the method m on your object currVo? Then it's simply

m.invoke(currVo);


Use reflection to get the getFieldName method and invoke it, as follows:

 Method method = voClass.getMethod("get" + fieldName); // the getter with no params in the signature
 Object value = method.invoke(currVo}); // invoke with no params
 listSelectBox.add(new SelectItem(currVo.getInternalId(), value));

Note: This assumes that fieldName is leading uppercase, eg "Value", not "value", so prepending it with "get" gives the exact method name, eg "getValue"


You should use the invoke method from the Method class.

m.invoke(currVo, (Object[]) null);

(Assuming the method takes no parameter.)

This will work for JDK versions 1.4 and later, since they state:

If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null

The one-parameter version of that call will not work on older JVMs.


I am not sure if i got ur question right, but what i feel u are asking wud be answered by following code:

// Class is whatever is the type u r using
Method mthd = Class.getMethod("get" + fieldName); //in case method don't have any parameters.
listSelectBox.add(mthd.invoke(currVo));

otherwise ignore.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号