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

[jira] [Commented] (GROOVY-8219) Add @TupleConstructor(includeFields = true) to a class implements a trait which contains properties will cause NPE

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

Paul King commented on GROOVY-8219:
-----------------------------------

I had a look at what would be involved to make your example behave more as you expect. In reality, it is behaving "kind of" as designed. By including "includeFields" you are also including the trait backing field. That field is only set in the constructor since you could be inheriting from additional traits that also have an "x" property and so isn't going to pick up the default value.

I had a look at what would be involved with marking the backing fields as "Trait$Implemented" or "Generated" and excluding them from the constructor field processing. It works but if applied in a general way possibly breaks other transforms. For example, we rely on {{@ToString}} processing the trait backing fields in {{Groovy7584Bug#testTraitFieldModifiersAreRetained()}} for example. I don't know if other folks are also relying on such behavior.

For your example, I would instead use one of the following:
{code}
@TupleConstructor(includeFields=true, excludes='T__x')
{code}
or
{code}
@TupleConstructor(includeFields=true, includes='a,b')
{code}


> Add @TupleConstructor(includeFields = true) to a class implements a trait which contains properties will cause NPE
> ------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8219
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8219
>             Project: Groovy
>          Issue Type: Bug
>          Components: xforms
>    Affects Versions: 2.4.11
>         Environment: Win 8.1, Oracle JDK 8u112
>            Reporter: Huabin Zhang
>            Priority: Major
>              Labels: trait, traits
>
> Hi, guys:
> I got NPE when I wrote the following codes:
> {code:java|title=Groovy Script|borderStyle=solid}
> trait T {
>     def x = 'abc'
> }
> import groovy.transform.TupleConstructor
> @TupleConstructor(includeFields = true)
> class A implements T {
>     def a
>     private b
> }
> assert 'ABC' == new A().x.toUpperCase()
> {code}
> I found that it will works well if I remove {{(includefields = true)}}.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)