Void function wrapper



  • I recently came across this little piece of java-ingenuity...can't imagine wtf could possibly have caused someone to code this:

    class voidFunctionWrapper {
      public static Object voidFunctionWrapper(String nameOfClassToInstantiate, 
                                               String voidMethodToCall,
                                               Object methodArgs[]) {
        // snip - code to instantiate the class, and locate the method
        Method theVoidMethodToInvoke = ... // snip: code to reflect to get the method on the object
    
        // Invoke the method with return type: void
        theVoidMethodToInvoke.invoke(theObjectOnWhichToCallTheMethod,methodArgs);
    
        return null;  // wtf?
      }
    }
    


  • <FONT face=Tahoma>The first thing that comes to my mind is calling dynamic methods derived from some sort of configuration file...plugins perhaps? Although I haven't worked on plugin related apps yet so I have little idea on how it should be implemented.

    Anyway, the function always returns a null object, why not just use void?



    </FONT>



  • @xrT said:

    <FONT face=Tahoma>The first thing that comes to my mind is calling dynamic methods derived from some sort of configuration file...plugins perhaps? Although I haven't worked on plugin related apps yet so I have little idea on how it should be implemented.

    Anyway, the function always returns a null object, why not just use void?
    </FONT>

    The configuration idea is plausible - what I don't get is why they didn't make the function just return the value returned by method.invoke(...), which would be null in the case of a void method, and something appropriate to the called method (if not void), as opposed to hard-wiring null and ignoring the return type in all cases 😉


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.