Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3200

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.5.7, 1.6-beta-2
    • 1.6-rc-1, 1.5.8, 1.7-beta-1
    • None
    • None

    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.

      Attachments

        Issue Links

          Activity

            People

              brownj Jeff Brown
              guillaume Guillaume Sauthier
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: