groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "William Woodman (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-8760) can't instantiate a class that inherits from base class marked with @MapConstructor
Date Tue, 21 Aug 2018 18:45:00 GMT

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

William Woodman commented on GROOVY-8760:
-----------------------------------------

um - its not quite idempotent - i think i mi=ust have a tried in a some order and broken it
- this work 

 

@MapConstructor (post = \{id = SequenceGenerator.standard.next() })
class A {
 long id
 String name

 Boolean isDead
 Boolean died

 A() {}

 String toString() {
 "A ($name, id:$id)"
 }
}

@InheritConstructors
class B extends A {
 String toString() {
 "B ($name)"
 }
}

however if i try this - this will fail 

 

@MapConstructor (post = \{id = SequenceGenerator.standard.next() })
class A {
 long id
 String name

 Boolean isDead
 Boolean died

 A() {}

 String toString() {
 "A ($name, id:$id)"
 }
}

@MapConstructor (post = \{id = SequenceGenerator.standard.next() })
@InheritConstructors
class B extends A {
 String toString() {
 "B ($name)"
 }
}

like this 

A (will, id:1)
Caught: java.lang.NoSuchMethodError: com.softwood.scripts.A: method <init>()V not found
java.lang.NoSuchMethodError: com.softwood.scripts.A: method <init>()V not found
 at com.softwood.scripts.B.<init>(TestOfferingAttributeGroup.groovy)
 at com.softwood.scripts.TestOfferingAttributeGroup.run(TestOfferingAttributeGroup.groovy:39)

 

and only define the map constructor on a chil 


class A {
 long id
 String name

 Boolean isDead
 Boolean died

 A() {}

 String toString() {
 "A ($name, id:$id)"
 }
}


@MapConstructor (post = \{id = SequenceGenerator.standard.next() })
@InheritConstructors
class B extends A {
 String toString() {
 "B ($name)"
 }
}

 

will work - but if you create a constructor on the parent - your map constructor is auto disabled
( i wanted the map constructor and  on both parent and child as i was using that to set this
id field inherited from the parent) 

 

from groovy implementation perspective if you have applied the @MapContructor on a parent
- shouldnt it be idempotent on the child?

 

for now i'm just going have to check carefully and test it does what i expect/doesnt fail 

 

Will 

 

> can't instantiate a class that inherits from base class marked with @MapConstructor
> -----------------------------------------------------------------------------------
>
>                 Key: GROOVY-8760
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8760
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.5.1
>         Environment: intellij 2018.1.4, java jdk 8.0_172, groovy 2.5.1
>            Reporter: William Woodman
>            Priority: Major
>              Labels: features
>
> basic inheritance where parent is tagged with @MapConstructor then child class cant be
instantiated
> example as follows 
>  
> {code}
> @MapConstructor
> class A {
>  String name
>  A() {}
>  String toString() {
>  "A ($name)"
>  }
> }
> class B extends A {
>  String toString() {
>  "B ($name)"
>  }
> }
> def myA = new A(name:"will")
> println myA
> def myB = new B(name:"fred")
> println myB
> {code}
> run this and get a RT exception 
> {noformat}
> A (will)
> Caught: java.lang.NoSuchMethodError: com.softwood.scripts.A: method <init>()V not
found
> java.lang.NoSuchMethodError: com.softwood.scripts.A: method <init>()V not found
>  at com.softwood.scripts.B.<init>(TestOfferingAttributeGroup.groovy)
>  at com.softwood.scripts.TestOfferingAttributeGroup.run(TestOfferingAttributeGroup.groovy:28)
> {noformat}



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

Mime
View raw message