Last active
August 29, 2015 14:11
-
-
Save robfletcher/44b115286fd9ef9c8a8c to your computer and use it in GitHub Desktop.
Difference in static & dynamic code dispatching to field or getter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
//@CompileStatic | |
class Person { | |
String name | |
private List<Person> friends | |
String friendNames() { | |
friends.collect { it.name }.join(", ") | |
} | |
} | |
assert new Person(name: "Rob").friendNames() == "" |
Interestingly if I change from friends.collect { it.name }
to friends.name
the dispatch changes!
Tried it in 2.3.8 and 2.4.0-beta-4 with the same result.
I've updated the gist now as apparently this is nothing to do with the presence of a getter. It's simply that dynamically compiled Groovy allows for calling certain GDK methods (all of them? only iterators?) on null
.
I think I've run in to this and reported a bug. I'll try to look it up.
My problem was different indeed, it was http://jira.codehaus.org/browse/GROOVY-7098 . Fixed in 2.3.8 already .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without @CompileStatic the code works as
friends
on line 9 dispatches togetFriends()
. With @CompileStatic it dispatches direct to the private field and results in an NPE.