groovy
  1. groovy
  2. GROOVY-3200

Map coercion should not throw an NPE for non implemented methods from the map

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.5.7, 1.6-beta-2
    • Fix Version/s: 1.6-rc-1, 1.5.8, 1.7-beta-1
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Let's consider the following example:

      interface Foo {
         def methodOne()
         def methodTwo()
      }
      
      def f = [methodOne: {-> "ONE" }] as Foo
      
      assert f.methodOne() == "ONE"
      
      // f.methodTwo()
      

      We've not provided an implementation for methodTwo(), and if we call that method, it throws a NullPointerException, leaking an implementation detail.

      As per the discussion on the mailing-list (http://www.nabble.com/Map-coercion-throwing-NPE--td20974198.html), the best thing is to indeed throw an exception, rather than returning a default value, but NPE is not the most appropriate exception to throw. It'd be best to throw an UnsupportedOperationException, like this is sometimes the case in some APIs like JDBC, AWT, JMS and others when the implementation of an API is not 100% complete when the provider doesn't support all the features possible.

        Issue Links

          Activity

          Hide
          Jeff Brown added a comment -

          The change to throw UnsupportedOperationException instead of NullPointerException has been committed to all 3 branches (1.5, 1.6 and 1.7)

          Show
          Jeff Brown added a comment - The change to throw UnsupportedOperationException instead of NullPointerException has been committed to all 3 branches (1.5, 1.6 and 1.7)
          Hide
          Paul King added a comment -

          We should make:

          ProxyGenerator.instantiateDelegateFromInterface(Map, Class)
          

          follow the same behavior.

          Show
          Paul King added a comment - We should make: ProxyGenerator.instantiateDelegateFromInterface(Map, Class ) follow the same behavior.

            People

            • Assignee:
              Jeff Brown
              Reporter:
              Guillaume Laforge
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: