You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/12/20 13:51:00 UTC

[jira] [Resolved] (GROOVY-7939) Direct field access operator

     [ https://issues.apache.org/jira/browse/GROOVY-7939?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles resolved GROOVY-7939.
---------------------------------
    Resolution: Information Provided

> Direct field access operator
> ----------------------------
>
>                 Key: GROOVY-7939
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7939
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Matthew Struensee
>            Priority: Major
>         Attachments: Screen Shot 2016-09-14 at 8.41.32 AM.png
>
>
> Documentation says that groovy will use the getter method first unless '@' is used.  
> {code}
> 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'   
> {code}
> 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...
> {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)
>         ]
>     }
> }
> {code}
> 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
(v8.20.1#820001)