Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.0-JSR-5
-
Fix Version/s: 1.1-rc-1
-
Component/s: class generator
-
Labels:None
-
Environment:JDK1.5 Groovy JSR 05 snapshot
-
Number of attachments :
Description
Implementing a Java interface in Groovy, and forgetting to set the return value / having the return value set to void causes an NPE instead of a meaningful script compilation error.
Example:
import org.springframework.web.servlet.handler.*; import javax.servlet.http.* class Interceptor1 extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) { httpServletRequest.setAttribute( "something", "somevalue") // return true; } }
The above causes an NPE when you call preHandle on this object, the NPE is the result of a failure to unbox the boolean, presumably as it has been set no value.
This is of course correct but it is a very obscure problem and definitely needs a sensible compile-time error message and exception, of the form "No value has been returned from method XXXX which should return a YYYYY"
This is intended behaviour.
Groovy allows you to return null when the return type of the method is a primative and Groovy returns null when there is no expilicit return statement.
It's arguable that the correct behaviour in this case is for the null to be interpreted as false which is the default behaviour when null is treated as a Boolean