@TupleConstructor together with @TupleConstructor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

@TupleConstructor together with @TupleConstructor

Pavel Alexeev
Hello.

I'he try use two annotations but seams not fully understand logic. I
want copy constructors (one really) from parent, and generate others by
fields of class:

class Base{
    Base(Map map){
        println "Called Base(Map map)"
    }
}

@TupleConstructor(force=true)
@InheritConstructors
class Person extends Base{
    def firstName
    def age
}

println Person.constructors

It give me output:
[public Person(java.util.Map), public Person(java.lang.Object), public
Person(java.lang.Object,java.lang.Object), public Person()]

But on instantiate of object:
Person p = new Person(['UserName', 33]);
got exception:
java.lang.NoSuchMethodError: Base: method <init>()V not found

What also looks reasonable (meantime note I call constructor with
parameters).
Adding empty constructor like Base(){} also thrown error:
java.lang.ClassFormatError: Duplicate method name&signature in class
file Person
Indeed empty constructor already was generated as we seen early.

Is there way pick up what exactly constructor I would like to copy? Or
any other suggestions please.
--
With best wishes, Pavel Alexeev (aka Pahan-Hubbitus). For fast contact
with me use jabber: [hidden email]

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @TupleConstructor together with @TupleConstructor

paulk_asert

You don't have a List constructor so the NoSuchMethodError seems appropriate.
In terms of giving you advice on what you should have I am not sure there is
enough info in your example? Is "map" a property of Base?  Perhaps you could
elaborate just a little more?

Cheers, Paul.

On 24/09/2014 3:40 AM, Pavel Alexeev wrote:

> Hello.
>
> I'he try use two annotations but seams not fully understand logic. I
> want copy constructors (one really) from parent, and generate others by
> fields of class:
>
> class Base{
>      Base(Map map){
>          println "Called Base(Map map)"
>      }
> }
>
> @TupleConstructor(force=true)
> @InheritConstructors
> class Person extends Base{
>      def firstName
>      def age
> }
>
> println Person.constructors
>
> It give me output:
> [public Person(java.util.Map), public Person(java.lang.Object), public
> Person(java.lang.Object,java.lang.Object), public Person()]
>
> But on instantiate of object:
> Person p = new Person(['UserName', 33]);
> got exception:
> java.lang.NoSuchMethodError: Base: method <init>()V not found
>
> What also looks reasonable (meantime note I call constructor with
> parameters).
> Adding empty constructor like Base(){} also thrown error:
> java.lang.ClassFormatError: Duplicate method name&signature in class
> file Person
> Indeed empty constructor already was generated as we seen early.
>
> Is there way pick up what exactly constructor I would like to copy? Or
> any other suggestions please.
>


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @TupleConstructor together with @TupleConstructor

Pavel Alexeev
Hi Paul.
Thank you for the answer.
Obviously in my first example was mistake.
Person p = new Person(['UserName', 33]);
should be:
Person p = new Person(name: 'UserName', age: 33]);

Ok, another example:

class Base{
    //!!! Without defining that constructor:
java.lang.NoSuchMethodError: Base: method <init>()V not found
    //Base(){ println "Called Base()" }

    Base(Map map){
        println "Called Base(Map map)"
        for (entry in map) {
            if (entry.value instanceof Closure){
                ((Closure)entry.value).delegate = this;
                ((Closure)entry.value).resolveStrategy =
Closure.DELEGATE_ONLY;
            }
            this."${entry.key}" = entry.value;
        }
    }
}

@TupleConstructor(force=true, includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
    String name
    public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I got:
[public Person(), public Person(java.lang.String), public
Person(java.lang.String,java.lang.Integer)]
Exception thrown
java.lang.NoSuchMethodError: Base: method <init>()V not found

So "public Person()" defined is it really required explicitly define
Base() constructor too?

24.09.2014 00:20, Paul King wrote:

>
> You don't have a List constructor so the NoSuchMethodError seems
> appropriate.
> In terms of giving you advice on what you should have I am not sure
> there is
> enough info in your example? Is "map" a property of Base?  Perhaps you
> could
> elaborate just a little more?
>
> Cheers, Paul.
>
> On 24/09/2014 3:40 AM, Pavel Alexeev wrote:
>> Hello.
>>
>> I'he try use two annotations but seams not fully understand logic. I
>> want copy constructors (one really) from parent, and generate others by
>> fields of class:
>>
>> class Base{
>>      Base(Map map){
>>          println "Called Base(Map map)"
>>      }
>> }
>>
>> @TupleConstructor(force=true)
>> @InheritConstructors
>> class Person extends Base{
>>      def firstName
>>      def age
>> }
>>
>> println Person.constructors
>>
>> It give me output:
>> [public Person(java.util.Map), public Person(java.lang.Object), public
>> Person(java.lang.Object,java.lang.Object), public Person()]
>>
>> But on instantiate of object:
>> Person p = new Person(['UserName', 33]);
>> got exception:
>> java.lang.NoSuchMethodError: Base: method <init>()V not found
>>
>> What also looks reasonable (meantime note I call constructor with
>> parameters).
>> Adding empty constructor like Base(){} also thrown error:
>> java.lang.ClassFormatError: Duplicate method name&signature in class
>> file Person
>> Indeed empty constructor already was generated as we seen early.
>>
>> Is there way pick up what exactly constructor I would like to copy? Or
>> any other suggestions please.
>>
>
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @TupleConstructor together with @TupleConstructor

paulk_asert

Thanks, that helps a bit. I guess I am still not sure what you are trying to do with the Map constructor in Base. Isn't the default behavior good enough?

import groovy.transform.*

class Base{ }

@TupleConstructor(includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
     String name
     public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I presume there is something additional you want to do but I am unsure what?

Cheers, Paul.

On 24/09/2014 8:18 PM, Pavel Alexeev wrote:

> Hi Paul.
> Thank you for the answer.
> Obviously in my first example was mistake.
> Person p = new Person(['UserName', 33]);
> should be:
> Person p = new Person(name: 'UserName', age: 33]);
>
> Ok, another example:
>
> class Base{
>      //!!! Without defining that constructor:
> java.lang.NoSuchMethodError: Base: method <init>()V not found
>      //Base(){ println "Called Base()" }
>
>      Base(Map map){
>          println "Called Base(Map map)"
>          for (entry in map) {
>              if (entry.value instanceof Closure){
>                  ((Closure)entry.value).delegate = this;
>                  ((Closure)entry.value).resolveStrategy =
> Closure.DELEGATE_ONLY;
>              }
>              this."${entry.key}" = entry.value;
>          }
>      }
> }
>
> @TupleConstructor(force=true, includeFields=true)
> @ToString(includeNames=true, includeFields=true)
> class Person extends Base{
>      String name
>      public Integer age
> }
>
> println Person.constructors
> [ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]
>
> I got:
> [public Person(), public Person(java.lang.String), public
> Person(java.lang.String,java.lang.Integer)]
> Exception thrown
> java.lang.NoSuchMethodError: Base: method <init>()V not found
>
> So "public Person()" defined is it really required explicitly define
> Base() constructor too?
>
> 24.09.2014 00:20, Paul King wrote:
>>
>> You don't have a List constructor so the NoSuchMethodError seems
>> appropriate.
>> In terms of giving you advice on what you should have I am not sure
>> there is
>> enough info in your example? Is "map" a property of Base?  Perhaps you
>> could
>> elaborate just a little more?
>>
>> Cheers, Paul.
>>
>> On 24/09/2014 3:40 AM, Pavel Alexeev wrote:
>>> Hello.
>>>
>>> I'he try use two annotations but seams not fully understand logic. I
>>> want copy constructors (one really) from parent, and generate others by
>>> fields of class:
>>>
>>> class Base{
>>>       Base(Map map){
>>>           println "Called Base(Map map)"
>>>       }
>>> }
>>>
>>> @TupleConstructor(force=true)
>>> @InheritConstructors
>>> class Person extends Base{
>>>       def firstName
>>>       def age
>>> }
>>>
>>> println Person.constructors
>>>
>>> It give me output:
>>> [public Person(java.util.Map), public Person(java.lang.Object), public
>>> Person(java.lang.Object,java.lang.Object), public Person()]
>>>
>>> But on instantiate of object:
>>> Person p = new Person(['UserName', 33]);
>>> got exception:
>>> java.lang.NoSuchMethodError: Base: method <init>()V not found
>>>
>>> What also looks reasonable (meantime note I call constructor with
>>> parameters).
>>> Adding empty constructor like Base(){} also thrown error:
>>> java.lang.ClassFormatError: Duplicate method name&signature in class
>>> file Person
>>> Indeed empty constructor already was generated as we seen early.
>>>
>>> Is there way pick up what exactly constructor I would like to copy? Or
>>> any other suggestions please.
>>>
>>
>>
>> ---
>> This email is free from viruses and malware because avast! Antivirus
>> protection is active.
>> http://www.avast.com
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @TupleConstructor together with @TupleConstructor

Pavel Alexeev
Ok. I think it will be good start from beginning.
I want use use Objects for some policy or configuration. Off course I known about great ConfigSlurper but I want it be typed and checked, so provide some interface in form of class. For some configuration options I allow provide closures, but want it act only in context Closure.DELEGATE_ONLY.

Unfortunately we can't mix groovy map-style object creation and anonymous class creation for methods override. So I'm stick with closures as fields (properties). For example:
class Policy{
    URL source;
    File dest;
    def content;

    Closure load = { content = source.text }
    Closure process = {  }
    Closure<Boolean> save = { dest.write(content) }
}

Configuration for many cases will be written only for thus parts which should have different from default behavior, like:
new Policy(
    source: new URL('http://google.com')
    ,dest: new File('place.there')
    ,process: { content.replaceAll(…) }
)

I want to closures will access defined in object fields (like content in example before). Some fields also injected later in runtime (like named logging). For that I need always set for new closures delegate to owner object transparently. For that was shown before constructor with Map argument. I also plan implement several kind of such sub Policies like:
class PolicyA extends Policy{…}
so copy/paste of that constructor implementation is bad solution.
Traits can't contain constructors.

Unfortunately due to the http://jira.codehaus.org/browse/GROOVY-7087 (@TupleConstructor does not call setters ) I realize I can't use it too now.

So now I look at overriding setProperty method.

24.09.2014 15:44, Paul King wrote:

Thanks, that helps a bit. I guess I am still not sure what you are trying to do with the Map constructor in Base. Isn't the default behavior good enough?

import groovy.transform.*

class Base{ }

@TupleConstructor(includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
    String name
    public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I presume there is something additional you want to do but I am unsure what?

Cheers, Paul.

On 24/09/2014 8:18 PM, Pavel Alexeev wrote:
Hi Paul.
Thank you for the answer.
Obviously in my first example was mistake.
Person p = new Person(['UserName', 33]);
should be:
Person p = new Person(name: 'UserName', age: 33]);

Ok, another example:

class Base{
     //!!! Without defining that constructor:
java.lang.NoSuchMethodError: Base: method <init>()V not found
     //Base(){ println "Called Base()" }

     Base(Map map){
         println "Called Base(Map map)"
         for (entry in map) {
             if (entry.value instanceof Closure){
                 ((Closure)entry.value).delegate = this;
                 ((Closure)entry.value).resolveStrategy =
Closure.DELEGATE_ONLY;
             }
             this."${entry.key}" = entry.value;
         }
     }
}

@TupleConstructor(force=true, includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
     String name
     public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I got:
[public Person(), public Person(java.lang.String), public
Person(java.lang.String,java.lang.Integer)]
Exception thrown
java.lang.NoSuchMethodError: Base: method <init>()V not found

So "public Person()" defined is it really required explicitly define
Base() constructor too?

24.09.2014 00:20, Paul King wrote:

You don't have a List constructor so the NoSuchMethodError seems
appropriate.
In terms of giving you advice on what you should have I am not sure
there is
enough info in your example? Is "map" a property of Base?  Perhaps you
could
elaborate just a little more?

Cheers, Paul.

On 24/09/2014 3:40 AM, Pavel Alexeev wrote:
Hello.

I'he try use two annotations but seams not fully understand logic. I
want copy constructors (one really) from parent, and generate others by
fields of class:

class Base{
      Base(Map map){
          println "Called Base(Map map)"
      }
}

@TupleConstructor(force=true)
@InheritConstructors
class Person extends Base{
      def firstName
      def age
}

println Person.constructors

It give me output:
[public Person(java.util.Map), public Person(java.lang.Object), public
Person(java.lang.Object,java.lang.Object), public Person()]

But on instantiate of object:
Person p = new Person(['UserName', 33]);
got exception:
java.lang.NoSuchMethodError: Base: method <init>()V not found

What also looks reasonable (meantime note I call constructor with
parameters).
Adding empty constructor like Base(){} also thrown error:
java.lang.ClassFormatError: Duplicate method name&signature in class
file Person
Indeed empty constructor already was generated as we seen early.

Is there way pick up what exactly constructor I would like to copy? Or
any other suggestions please.



---
This email is free from viruses and malware because avast! Antivirus
protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

     http://xircles.codehaus.org/manage_email





---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @TupleConstructor together with @InheritConstructors

Pavel Alexeev
In reply to this post by paulk_asert
Ok. I think it will be good start from beginning.
I want use use Objects for some policy or configuration. Off course I known about great ConfigSlurper but I want it be typed and checked, so provide some interface in form of class. For some configuration options I allow provide closures, but want it act only in context Closure.DELEGATE_ONLY.

Unfortunately we can't mix groovy map-style object creation and anonymous class creation for methods override. So I'm stick with closures as fields (properties). For example:
class Policy{
    URL source;
    File dest;
    def content;

    Closure load = { content = source.text }
    Closure process = {  }
    Closure<Boolean> save = { dest.write(content) }
}

Configuration for many cases will be written only for thus parts which should have different from default behavior, like:
new Policy(
    source: new URL('http://google.com')
    ,dest: new File('place.there')
    ,process: { content.replaceAll(…) }
)

I want to closures will access defined in object fields (like content in example before). Some fields also injected later in runtime (like named logging). For that I need always set for new closures delegate to owner object transparently. For that was shown before constructor with Map argument. I also plan implement several kind of such sub Policies like:
class PolicyA extends Policy{…}
so copy/paste of that constructor implementation is bad solution.
Traits can't contain constructors.

Unfortunately due to the http://jira.codehaus.org/browse/GROOVY-7087 (@TupleConstructor does not call setters ) I realize I can't use it too now.

So now I look at overriding setProperty method.

24.09.2014 15:44, Paul King wrote:

Thanks, that helps a bit. I guess I am still not sure what you are trying to do with the Map constructor in Base. Isn't the default behavior good enough?

import groovy.transform.*

class Base{ }

@TupleConstructor(includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
    String name
    public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I presume there is something additional you want to do but I am unsure what?

Cheers, Paul.

On 24/09/2014 8:18 PM, Pavel Alexeev wrote:
Hi Paul.
Thank you for the answer.
Obviously in my first example was mistake.
Person p = new Person(['UserName', 33]);
should be:
Person p = new Person(name: 'UserName', age: 33]);

Ok, another example:

class Base{
     //!!! Without defining that constructor:
java.lang.NoSuchMethodError: Base: method <init>()V not found
     //Base(){ println "Called Base()" }

     Base(Map map){
         println "Called Base(Map map)"
         for (entry in map) {
             if (entry.value instanceof Closure){
                 ((Closure)entry.value).delegate = this;
                 ((Closure)entry.value).resolveStrategy =
Closure.DELEGATE_ONLY;
             }
             this."${entry.key}" = entry.value;
         }
     }
}

@TupleConstructor(force=true, includeFields=true)
@ToString(includeNames=true, includeFields=true)
class Person extends Base{
     String name
     public Integer age
}

println Person.constructors
[ new Person(name: 'Pavel', age: 100), new Person('Pavel1', 200) ]

I got:
[public Person(), public Person(java.lang.String), public
Person(java.lang.String,java.lang.Integer)]
Exception thrown
java.lang.NoSuchMethodError: Base: method <init>()V not found

So "public Person()" defined is it really required explicitly define
Base() constructor too?

24.09.2014 00:20, Paul King wrote:

You don't have a List constructor so the NoSuchMethodError seems
appropriate.
In terms of giving you advice on what you should have I am not sure
there is
enough info in your example? Is "map" a property of Base?  Perhaps you
could
elaborate just a little more?

Cheers, Paul.

On 24/09/2014 3:40 AM, Pavel Alexeev wrote:
Hello.

I'he try use two annotations but seams not fully understand logic. I
want copy constructors (one really) from parent, and generate others by
fields of class:

class Base{
      Base(Map map){
          println "Called Base(Map map)"
      }
}

@TupleConstructor(force=true)
@InheritConstructors
class Person extends Base{
      def firstName
      def age
}

println Person.constructors

It give me output:
[public Person(java.util.Map), public Person(java.lang.Object), public
Person(java.lang.Object,java.lang.Object), public Person()]

But on instantiate of object:
Person p = new Person(['UserName', 33]);
got exception:
java.lang.NoSuchMethodError: Base: method <init>()V not found

What also looks reasonable (meantime note I call constructor with
parameters).
Adding empty constructor like Base(){} also thrown error:
java.lang.ClassFormatError: Duplicate method name&signature in class
file Person
Indeed empty constructor already was generated as we seen early.

Is there way pick up what exactly constructor I would like to copy? Or
any other suggestions please.



---
This email is free from viruses and malware because avast! Antivirus
protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

     http://xircles.codehaus.org/manage_email





---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Loading...