groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Theodorou (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-7518) hashCode() throws NPE when using @CompileStatic with @EqualsAndHashCode
Date Thu, 23 Jul 2015 05:40:04 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-7518?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14638186#comment-14638186
] 

Jochen Theodorou commented on GROOVY-7518:
------------------------------------------

to clarify, in Pauls example the NPE comes from unboxing this.getSomeLong(). SomeLong is a
Long, and the HashCodeHelper method called here takes a long in that position, thus unboxing
happens and is causing the NPE. The NPE would not happen with normal Groovy, because there
would be no unboxing at this position, thus the Object taking variant will be called instead.
Possible solution would be to move the null-check into the generated code and open the shift
method, or to add the object variants of the primitives to HashCodeHelper, or one of the other
1000 possible solutions

> hashCode() throws NPE when using @CompileStatic with @EqualsAndHashCode
> -----------------------------------------------------------------------
>
>                 Key: GROOVY-7518
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7518
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.4
>            Reporter: Thomas
>
> Given the following class
> {code:title=Person.groovy}
> import groovy.transform.CompileStatic
> import groovy.transform.EqualsAndHashCode
> @EqualsAndHashCode
> @CompileStatic
> class Person {
> 	Long someLong
> }
> {code}
> and the test
> {code:title=PersonTest.groovy}
> import static org.junit.Assert.*
> import org.junit.Test
> class PersonTest {
> 	@Test
> 	void hashcodeWithoutLongValue() {
> 		def person = new Person()
> 		assert person.hashCode()
> 	}
> }
> {code}
> and the gradle configuration:
> {code:title=build.gradle}
> buildscript {
>     repositories {
>         mavenCentral()
>     }
> }
> apply plugin: 'groovy'
> apply plugin: 'eclipse'
> jar {
>     baseName = 'HashCodeNPE'
>     version = '0.0.1-SNAPSHOT'
> }
> sourceCompatibility = 1.8
> targetCompatibility = 1.8
> repositories {
>     mavenCentral()
> }
> dependencies {
>     compile("org.codehaus.groovy:groovy:2.4.4")
> 	testCompile("junit:junit:4.12")
> }
> eclipse {
>     classpath {
>          containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
>          containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
>     }
> }
> task wrapper(type: Wrapper) {
>     gradleVersion = '2.4'
> }
> {code}
> when I run {code}gradle clean build{code}
> then the test fails with the following message.
> {code}
> PersonTest > hashcodeWithoutLongValue FAILED
>     java.lang.NullPointerException at PersonTest.groovy:10
> 1 test completed, 1 failed
> :test FAILED
> {code}
> With Groovy 2.4.3 the test passes.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message