This is my first post and I guess everyone must do their first one here. I've been programming professionally in Java off and on since 1998 - but I don't think of myself as an expert by any means.
I have encountered a Java issue that I feel certain I solved once 8 years ago, but I can't remember how I did it, I left the code at that employers, and Google hasn't helped me resolve it this time. It involves making a call to a method in an already instantiated class without knowing until runtime which class that will be.
On the one hand, I could write a case block that would match the String generated method name against a list of known methods and then execute a hard coded method call from there. But - in this case there are over 300 possible methods (all getters) so I'm hoping there's a more elegant way to do it. What I'm hoping to do is execute the appropriate method by name using a String I have generated at runtime to point to the correct method.
I've looked at reflection as a possibility a lot - but the Java.lang.reflect.Method.invoke class seems to insist on instantiating it's own copy of the class - and that would be very slow since there's a SQL Query involved that I'm hoping to only run once for all ~300 calls. Ideally, the class has already been instantiated, the query has already run, and the 300+ fields are already accessible via 300+ getters.
What do you think? Does anyone know what I'm looking for?
Re: Howdy all!
Welcome to the Java Programming Forums. It's always good to have a seasoned professional join the community ;)
Hmm.. I'm going to have to look into this one myself! I will post back as soon as possible.
Re: Howdy all!
To address your question, consider writing an interface whose implementation runs the method in question. Then store the references to each via a Map keyed with the appropriate string and valued with the appropriate interface implementation. For 300 it might be hard to initially hard code the implementations, but - if you want to load at runtime - you could use reflection once to create the single instance of the object in question and populate the Map values with the actual Method object (then just retrieve the Method from the Map for a given string and invoke the method).
Re: Howdy all!
Thanks for the welcome, guys.
copeg - I think that would work, and it's pretty elegant too, but what I'm looking for is simpler. What I recall from the last time I was in this situation is that there is a way to create the call at runtime without doing the legwork ahead of time. It was, like, 3 lines of code and worked great.
What I'm trying to do is a little similar to the Oracle 'execute immidiate' technology in which a string is treated as a command. This allows you to dynamically create a call to an existing method at runtime. It's sort of a dynamic link and exectue.
At this point, I've already coded it as a 1000+ line 'if-else if...' - but I would happily replace that with the above if I could only remember how I did it! I think it would be less efficient, but be much easier to maintain.