You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jochen Theodorou (JIRA)" <ji...@apache.org> on 2016/09/14 12:51:20 UTC
[jira] [Commented] (GROOVY-7939) Direct field access operator
[ https://issues.apache.org/jira/browse/GROOVY-7939?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15490360#comment-15490360 ]
Jochen Theodorou commented on GROOVY-7939:
------------------------------------------
"Documentation says that groovy will use the getter method first unless '@' is used. " - can you point me to exactly where that is? Then I can correct this.
Right is "Groovy will use the getter method first unless @ is used and unless we are accessing a field using (implicit) this."
So while prinlnt foo, and println this.foo may access the field foo, def x = this; println x.foo; will use the getter, unless you do x.@foo of course
> Direct field access operator
> ----------------------------
>
> Key: GROOVY-7939
> URL: https://issues.apache.org/jira/browse/GROOVY-7939
> Project: Groovy
> Issue Type: Bug
> Reporter: Matthew Struensee
>
> Documentation says that groovy will use the getter method first unless '@' is used.
> class User {
> public final String name
> User(String name) { this.name = name}
> String getName() { "Name: $name" }
> }
> def user = new User('Bob')
> assert user.name == 'Name: Bob'
> I have ran into scenarios where that was not the case...
> This class tries to do Springs @Value annotation, but instead will store it in a database and retrieve from there instead of using spring code...
> @RestController
> @RequestMapping('liveConfigExample')
> class LiveConfigExampleController {
> @Autowired
> LiveConfigExampleService liveConfigExampleService
> @LiveConfigProperty('base.url')
> String baseUrl
> @LiveConfigProperty('application.online')
> boolean applicationOnline
> @LiveConfigProperty('timeout.counter')
> int timeoutCounter
> @Value('${timeout.counter}')
> int timeoutCounterValue
> @RequestMapping('test')
> def test() {
> [
> baseUrlController : baseUrl, < --- returns default null value
> applicationOnlineController : applicationOnline, < --- returns default null value
> timeoutCounterController : timeoutCounter, < --- returns default null value
> timeoutCounterValueController: timeoutCounterValue, < --- returns 10 (from property file)
> baseUrlService : liveConfigExampleService.baseUrl, < --- returns "abc.com"
> applicationOnlineService : liveConfigExampleService.applicationOnline,< --- returns true
> timeoutCounterService : liveConfigExampleService.timeoutCounter,< --- returns 10 (from database)
> timeoutCounterValueService : liveConfigExampleService.timeoutCounterValue< --- returns 10 (from property file)
> ]
> }
> }
> All fields accessed inside the controller were using the field accessor and returning a default value (which was never set) while the service layer was using the AST Transformed getter method which called the DB to get the value.
> I tried removing the field during the AST Transformation, but that removed my AST Transformed getter methods.
> I tried removing the field during the AST Transformation and then adding a new one with the same name, type, etc but the compiler complains I have the fieldNode more than once.
> The field 'a' is declared multiple times.
> Even though the debugger showed the default values, when I did use "Evaluate Expression" I did get the database value vs default.
> I did get it to force the getter in the controller by renaming the fieldName.name, but that still shows up in the debugger as, you guessed it, the default values.
> Is there any way I can FORCE the getter method, have the debugger show the database value vs default value without doing this rename trick? I am perfectly fine with removing the fields but would prefer to see them in the debugger with the not default null values.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)