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 2018/02/01 02:39:00 UTC

[jira] [Comment Edited] (GROOVY-7078) make @Immutable play nice with constructor injection

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

Paul King edited comment on GROOVY-7078 at 2/1/18 2:38 AM:
-----------------------------------------------------------

I know it's been a while but the recent split of @Immutable into a meta-annotation (see GROOVY-8440) now supports this functionality. One way:

{code:java}
import groovy.transform.*

@ImmutableBase
@Canonical(defaults=false)
class Person {
  String first, last
  Date born
  List items
}

println new Person('John', 'Smith', new Date(), [])
{code}
This results in a constructed class with all the properties of @Immutable but with just one constructor.


was (Author: paulk):
I know it's been a while but the recent split of @Immutable into a meta-annotation now supports this functionality. One way:

{code:java}
import groovy.transform.*

@ImmutableBase
@Canonical(defaults=false)
class Person {
  String first, last
  Date born
  List items
}

println new Person('John', 'Smith', new Date(), [])
{code}
This results in a constructed class with all the properties of @Immutable but with just one constructor.

> make @Immutable play nice with constructor injection
> ----------------------------------------------------
>
>                 Key: GROOVY-7078
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7078
>             Project: Groovy
>          Issue Type: Improvement
>          Components: xforms
>    Affects Versions: 2.3.6
>            Reporter: Christopher Smith
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 2.5.0-beta-3
>
>
> I have some Spring services that take injected components, and I use constructor injection whenever possible. These services can't be reconfigured after startup, and usually make the fields {{final}}.
> I would like to be able to use {{@Immutable}} to automatically generate the constructors and related plumbing for me, but {{@Immutable}} creates two, one that takes a {{Map}} and one that takes regular arguments. Spring/CDI can't resolve a single constructor and won't inject.
> Would it be feasible to have a way to add an annotation like {{@Inject}} to the generated tuple constructor, or is there another way that I can make an {{@Immutable}} class eligible for injection?



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)