Cristian Boariu
Cristian Boariu

Reputation: 9621

java reflection issue

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

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...

Upvotes: 4

Views: 173

Answers (5)

Mat
Mat

Reputation: 206669

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.

Upvotes: 1

axtavt
axtavt

Reputation: 242686

m.invoke(currVo);

See also:

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

Upvotes: 3

Nik
Nik

Reputation: 695

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.

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

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"

Upvotes: 1

Howard
Howard

Reputation: 39177

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

m.invoke(currVo);

Upvotes: 1

Related Questions