0

I have a list of Method objects that I want to execute using user supplied parameters which I don't know the type or value until runtime.

I want to loop through the methods array and execute them using the invoke method until one of the methods executes successfully without an exception. Is this a good way to do it?

        Method[] methods = cls.getMethods();

        for (Method method : methods) {
            try {
                method.invoke(obj, param1, param2);
                break;
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }

I've been told that using try and catch for flow control is bad idea, but I'm not sure why. I'm also not sure what the alternative would be in a situation like this where I expect an exception to occur in the normal execution because the user supplies the parameters.

Any help is greatly appreciated.

A. J. Green
  • 93
  • 2
  • 8

1 Answers1

2

In this particular case , why don't you try getParametersType(). And when it parameter types matches the type of the param1 and param2 you can actually execute method.invoke(obj, param1, param2);

In general it is bad idea because you don't know how heavy a function is . So it might consume lot of processing time or cpu cycle before ultimately throwing exception.

Geek
  • 627
  • 1
  • 8
  • 18
  • Thank you, I will implement it and it should avoid the majority of unnecessary method executions and exceptions. But I think there might some situations where the code will have to convert the user inputs to different types so I might have to run several overloaded methods trying a parameter both converted to string and converted to date type. I guess in these rarer situations, it would be appropriate to use try and catch while looping? – A. J. Green Sep 23 '20 at 17:57
  • I am not sure about string to date because as I understand it is quite light weight function . So it shouldn't harm the processing time. But if you can figure out a regex to determine if a string is date then I would say that's a better path. But you will know more about your application. Also I think you will run into issue where auto boxing is involved. E.g: a method parameter type is 'int' while the param1 type is Integer. So you might need to construct rules around that. – Geek Sep 23 '20 at 18:06
  • I think getting exception is unavoidable because both the method and the params are set at runtime. I didn't write the methods available for selection and can't alter the them to avoid ambiguity. There will be some methods that accept the appropriate Date format but as a String rather than a Date object and other situations where the method might require an actual Date object parameter. The user cannot be depended on to explicitly cast or convert the data types of the input and the RegEx will match for both. I guess I'm asking if it's appropriate to use try-catch in these rarer cases. – A. J. Green Sep 23 '20 at 18:17
  • I came across such issue where I want to see if a string is purely a number (not alphanumeric). So I found this : https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java (check out the second answer ) here the suggestion is to use NumberFormatException to make a decision. But it also suggest if this function is called repeatedly ( e.g 100s of user per second) then you might see slight degradation of performance. My point is it ultimately rolls down to your application. Given the particular check you are doing and number of methods are less you should be fine. – Geek Sep 23 '20 at 18:26
  • I understand. Thank you for breaking it down for me. – A. J. Green Sep 23 '20 at 18:35