users@jaxb.java.net

Re: jaxb2-commons value-constructor generate two no-arg constructors

From: Pierre Buyle <pierre.buyle_at_irislink.com>
Date: Tue, 27 Nov 2007 00:27:13 -0800 (PST)

Hi,

thank you for the quick response. In your updated code, you duplicated the
static field check in getInstanceFields(...) and in
generateConstructorParameter(...). It could be a nicer to use
generateConstructorParameter(...) as check in getInstanceFields(...) to
avoid updating one check and not the other. Also, as the two fields lists
would only contains fields for which constructor parameters are to be
generated, other calls to generateConstructorParameter(...) could be
removed.

something like
    protected Collection<JFieldVar> getInstanceFields(final
Collection<JFieldVar> fields) {
        final List<JFieldVar> instanceFields = new ArrayList<JFieldVar>();
        for (final JFieldVar fieldVar : fields) {
                        if (generateConstructorParameter(fieldVar)) {
                                instanceFields.add(fieldVar);
                        }
                }
        return instanceFields;
    }

and

    public boolean run(final Outline outline, final Options options, final
ErrorHandler errorHandler) {
        ...
                if (implClass._extends() instanceof JDefinedClass) {
                    
                        final JInvocation superInvocation =
valueConstructor.body().invoke("super");
                    
                    // Add each argument to the super constructor.
                    for (JFieldVar superClassField :
superClassInstanceFields) {
                                final JVar arg = valueConstructor.param(JMod.FINAL,
superClassField.type(), superClassField.name());
                        superInvocation.arg(arg);
                    }
                }
                
                // Now add constructor parameters for each field in "this"
class, and assign them to
                // our fields.
                for (final JFieldVar field : thisClassInstanceFields) {
                            final JVar arg = valueConstructor.param(JMod.FINAL,
field.type(), field.name());
                   
valueConstructor.body().assign(JExpr.refthis(field.name()), arg);
                }
            }
        }


Kenny MacLeod wrote:
>
> Hi Pierre,
>
> Thanks for reporting this. I've put a new version up which should fix
> the issue.
>
> https://jaxb2-commons.dev.java.net/value-constructor/
>
>
> cheers
> kenny
>
>
> Pierre Buyle wrote:
>> When I generate a class from an empty complexType using the
>> jaxb:serializable
>> customization and the value-constructor plugin , two no-arg constructors
>> are
>> generated.
>>
>> I quickly looked at the plugin code and found the bug. The value
>> constructor
>> is generated if one of the thisFields and superFields collections are not
>> empty. These collections contains static fields, for which no constructor
>> parameters are generated. Since the jxb:serializable customization add
>> the
>> static serialVersionUID field, thisFields is not empty. But as no
>> constructor is generated for this field, the value constructor become a
>> dupe
>> of the no-arg constructor.
>>
>> The fix is to filter static fields out from both collections before
>> testing
>> if a value constructor is needed.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>

-- 
View this message in context: http://www.nabble.com/jaxb2-commons-value-constructor-generate-two-no-arg-constructors-tf4875842.html#a13966661
Sent from the java.net - jaxb users mailing list archive at Nabble.com.