Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1433

MethodUtils will throw a NPE if invokeMethod() is called for a var-args method

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.8.1
    • Fix Version/s: 3.10
    • Component/s: lang.reflect.*
    • Labels:
      None

      Description

      Whenever calling a var-args method using invokeMethod() of MethodUtils it will throw a NPE if the last parameter value is null.

      Example: Given is the following class with var-args method

      public class MyClass {
         public String myMethod(final String... args) {
            ...
         }
      
         public String myMethod(final Integer... args) {
            ...
         }
      }
      

      The following code will work:

      MethodUtils.invokeMethod(myObject, "myMethod", "a", null, "c");
      

      But this will throw a NPE:

      MethodUtils.invokeMethod(myObject, "myMethod", "a", "b", null);
      

      Further more it is not deterministic which method of the two will be called if the method search of invokeMethod() can not find a single unique match. So the code

      MethodUtils.invokeMethod(myObject, "myMethod", new Object[] {null});
      

      might call any of the two methods of MyClass and it might change with each and every invocation. The search is not deterministic due to Class.getMethods () is not deterministic.

      I would categorize such a method call as a bug, due to it should not be the intention of the developer to just pick any of the methods. However, debugging such an issue is really hard if the "right" method is called "sometimes" and it can be solved by just doing a sort, which should be no big deal I belive.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                cfranzen Christian Franzen
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m