Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I map a String to a function in Java?
    primarykey
    data
    text
    <p>Currently, I have a bunch of Java classes that implement a <code>Processor</code> interface, meaning they all have a <code>processRequest(String key)</code> method. The idea is that each class has a few (say, &lt;10) member <code>Strings</code>, and each of those maps to a method in that class via the <code>processRequest</code> method, like so:</p> <pre><code>class FooProcessor implements Processor { String key1 = "abc"; String key2 = "def"; String key3 = "ghi"; // and so on... String processRequest(String key) { String toReturn = null; if (key1.equals(key)) toReturn = method1(); else if (key2.equals(key)) toReturn = method2(); else if (key3.equals(key)) toReturn = method3(); // and so on... return toReturn; } String method1() { // do stuff } String method2() { // do other stuff } String method3() { // do other other stuff } // and so on... } </code></pre> <p>You get the idea.</p> <p>This was working fine for me, but now I need a runtime-accessible mapping from key to function; not every function <em>actually</em> returns a String (some return void) and I need to dynamically access the return type (using reflection) of each function in each class that there's a key for. I already have a manager that knows about all the keys, but <em>not</em> the mapping from key to function.</p> <p>My first instinct was to replace this mapping using <code>if-else</code> statements with a <code>Map&lt;String, Function&gt;</code>, like I could do in Javascript. But, Java doesn't support first-class functions so I'm out of luck there. I could probably dig up a third-party library that lets me work with first-class functions, but I haven't seen any yet, and I doubt that I need an entire new library.</p> <p>I also thought of putting these <code>String</code> keys into an array and using reflection to invoke the methods by name, but I see two downsides to this method:</p> <ul> <li>My keys would have to be named the same as the method - or be named in a particular, consistent way so that it's easy to map them to the method name.</li> <li>This seems WAY slower than the <code>if-else</code> statements I have right now. Efficiency is something of a concern because these methods will tend to get called pretty frequently, and I want to minimize unnecessary overhead.</li> </ul> <hr> <p><strong>TL; DR:</strong> I'm looking for a clean, minimal-overhead way to map a <code>String</code> to some sort of a <code>Function</code> object that I can invoke and call (something like) <code>getReturnType()</code> on. I don't especially mind using a 3rd-party library if it <em>really</em> fits my needs. I also don't mind using reflection, though I would strongly prefer to avoid using reflection every single time I do a method lookup - maybe using some caching strategy that combines the <code>Map</code> with reflection.</p> <p>Thoughts on a good way to get what I want? Cheers!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload