1

I have a class which contains a couple different objects.

I can use getDeclaredFields to get the name list of all objects, but I want to call a method in these objects.
How can I do that?

ClassA a = new ClassA();
Class cls = c.getClass();
Field[] fields = cls.getDeclaredFields();
for(int i = 0; i< fields.length;i++) {
   System.out.println("Field = " + fields[i].toString());
   System.out.prontln(fields[i].method()) // how can I call the method from object fields[i]             
}

more info: the reason I use reflection is I want write a test class which can be used to test all other classes' objects are properly existing.

testclass(class a), get all objects name in the class a, and use object.exists() method to verify the existing of this object.

here is my code: I have some dialog classes, each dialog class has some menuitem class, checkbox class,textfield class, I want write a class which can be used to verify all checboxes,textfields are exsting (use checkbox.exist(), textfield.exist() ...) in the given dialog.

ToolsMenu c = new ToolsMenu();
        Class cls = c.getClass();
        Field[] fields = cls.getDeclaredFields();
        for(int i = 0; i< fields.length;i++) {
            System.out.println("Field = " + fields[i].toString());
            println( fields[i].getDeclaringClass().exists()

I can use getdeclaringclass to get the field[i] class, but how can i call method exists() which is defined in checkboxes,textfields class.

4
  • Well what's the type of the field you're interested in, and what's the method? Commented Jun 25, 2013 at 19:07
  • I suggest that you google "java reflection api". There are plenty of tutorials available about how to do this. But then my question is: why are you using reflection in the first place? Commented Jun 25, 2013 at 19:08
  • 3
    Are you trying to invoke just any method or are you aware of the method signature? Commented Jun 25, 2013 at 19:09
  • the reason I use reflection is I want write a test class which can be used to test all other classes' objects are properly existing. testclass(class a), get all objects name in the class a, and use object.exists() method to verify the existing of this object. Commented Jun 25, 2013 at 19:36

2 Answers 2

3

You can call it with something like this:

...
Class clazz= fields[i].get(c).getClass();
clazz.getMethod("methodName").invoke(fields[i].get(c));
...

Where "methodName" is name of the method which should be called. You can also pass some parameters to the method.

Sign up to request clarification or add additional context in comments.

3 Comments

You should use c instead of cls
fields[i] will return the name of the object, I dont think we can use a string to call the object's method.
fields[i] will return Field object.
2

I'm not sure why you are using reflection at all. You can simply do

a.field.method()

If field and its method() declare the correct access modifiers.

4 Comments

I think method() was an example and the OP doesn't know the name of the method he's calling, which is why he's using reflection. However, that doesn't make a lot of sense in itself...
field is a string in field[], how Can i transfer it to a class?.
before use declaredfield, I dont know the name of the field, after declaredfield, can we use a.field[i].method() ?
@bitma My answer assumes that you know the names of both the field and the method. If you don't know these, then you certainly need to use reflection. I suggest you check out the official Oracle tutorial for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.