Programatically calling all methods in a Java class

Scott

I'm trying to go through a large amout of classes with variable methods and have each method run with some generated input. It doesn't really matter what the result is, it only matters that there aren't any errors.

I have this code to find all methods and invoke them:

MyClassType theClass = new MyClassType();  // Fake class for example
Method[] methods = theClass.getClass().getMethods();

for (Method m : methods) {
    try {
        m.invoke(theClass, new Object[]{});
    } catch (Exception ex) {
        //Log error
    }
}

This works fine for all methods that do not take any arguments, but fails for methods that do.

Is there any way I can programatically detect the type of arguments needed for each method and give it a test value? For example, all booleans are true, all ints are 1, all floats are 1.5f, etc.

Hovercraft Full Of Eels

Look at the Method API, .... which to be honest really should have been the first place you should look before coming here actually. The getParametertypes() method in the Method class will help. There are other useful methods present there including those that help with generic methods.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling methods on string literals (Java)

From Java

Why calling methods from container class instead of instances of it in Java

From Java

Why are the 'Arrays' class' methods all static in Java?

From Java

Automatically delegating all methods of a java class

From Java

Java prevent calling private or protected methods outside of class

From Java

Calling multiple methods in Java

From Java

Calling the methods in a class in Java

From Dev

calling different methods of same class using multi threading in java

From Dev

a class with all static methods

From Dev

Calling methods of a virtual member class

From Dev

Class inheritance - properly calling methods

From Dev

std::thread calling class methods

From Dev

Calling Java Methods in XSLT

From Dev

If both interface and abstract class having same methods then which one is better to override all methods into my class in java?

From Dev

Java - How to allow all methods of a class to access an array initialized in constructor?

From Dev

XPages - Calling Java methods

From Dev

python: generating methods for a convenience class programatically

From Dev

Undefined method for class / calling methods outside of a class

From Dev

Calling methods from module in class

From Dev

Calling methods for my java class

From Dev

Issuing with calling methods in Inherited class

From Dev

Calling the methods in java

From Dev

Java / Android: Collect all Button methods in Helper class

From Dev

(Java) Pass a parameter from a constructor to all the methods of a class

From Dev

Calling methods on objects Java

From Dev

ClassCastException in calling methods added class

From Dev

Profiling all class methods

From Dev

Calling class methods inside javascript class

From Dev

Trouble calling methods from a class/

Related Related

  1. 1

    Calling methods on string literals (Java)

  2. 2

    Why calling methods from container class instead of instances of it in Java

  3. 3

    Why are the 'Arrays' class' methods all static in Java?

  4. 4

    Automatically delegating all methods of a java class

  5. 5

    Java prevent calling private or protected methods outside of class

  6. 6

    Calling multiple methods in Java

  7. 7

    Calling the methods in a class in Java

  8. 8

    calling different methods of same class using multi threading in java

  9. 9

    a class with all static methods

  10. 10

    Calling methods of a virtual member class

  11. 11

    Class inheritance - properly calling methods

  12. 12

    std::thread calling class methods

  13. 13

    Calling Java Methods in XSLT

  14. 14

    If both interface and abstract class having same methods then which one is better to override all methods into my class in java?

  15. 15

    Java - How to allow all methods of a class to access an array initialized in constructor?

  16. 16

    XPages - Calling Java methods

  17. 17

    python: generating methods for a convenience class programatically

  18. 18

    Undefined method for class / calling methods outside of a class

  19. 19

    Calling methods from module in class

  20. 20

    Calling methods for my java class

  21. 21

    Issuing with calling methods in Inherited class

  22. 22

    Calling the methods in java

  23. 23

    Java / Android: Collect all Button methods in Helper class

  24. 24

    (Java) Pass a parameter from a constructor to all the methods of a class

  25. 25

    Calling methods on objects Java

  26. 26

    ClassCastException in calling methods added class

  27. 27

    Profiling all class methods

  28. 28

    Calling class methods inside javascript class

  29. 29

    Trouble calling methods from a class/

HotTag

Archive