highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

xjc:superClass not supported by copyable, equals and hashCode plugins #42

Closed krokodylowy closed 8 years ago

krokodylowy commented 8 years ago

I got almost correctly class generated from definitions. Almost because clone, hashCode, equals, copyTo don't use the same methods from superClass so some data in clone and equals are missing. My generated class x extends y implements Cloneable, CopyTo, Serializable, class y implements Cloneable, CopyTo, Serializable.

Copyable plugin code got some superClass code but it seems to me it's not executed. How to configure plugin to generate correct method clone which also cloning attributes from superclass?

highsource commented 8 years ago

This should actually work. Please show your generated code, send a PR with the reproducing test project.

Am 12.12.2015 um 23:54 schrieb krokodylowy notifications@github.com:

I got almost correctly class generated from definitions Almost because clone, hashCode, equals, copyTo don't use the same methods from superClass so some data in clone and equals are missing My generated class x extends y implements Cloneable, CopyTo, Serializable, class y implements Cloneable, CopyTo, Serializable

Copyable plugin code got some superClass code but it seems to me it's not executed How to configure plugin to generate correct method clone which also cloning attributes from superclass?

— Reply to this email directly or view it on GitHub.

krokodylowy commented 8 years ago

OK. I write some but at this moment I see that test use only

<xsd:extension base="a:AType">

notation and not use something like

<xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings choiceContentProperty="true" generateIsSetMethod="true">
                <xjc:superClass name="mco.AbstractMDObject" />
            </jaxb:globalBindings>
    </xs:annotation>

or

<xjc:superInterface name="com.example.model.Person"/>
krokodylowy commented 8 years ago

testcase.txt

Episode c for jaxb2-basics\tests\episodes\c. Class BType extends AbstractMDObject.java AbstractMDObject.java included in Episode a. Clone,hashCode and equals ignore attributes from AbstractMDObject.

Such kind of extension is a part of bigger real project.

highsource commented 8 years ago

I can't apply the diff you sent, sorry. It modifies the episodes project. I'd prefer not to hack an existing project for the new issue.

I see what the problem is:

Actually all of this should work. Plugins check if the superclass implements the expected interface.

But this only works if the superclass is known during the code generation time. It must be compiled before and be present in the classpath when the code is generated.

You've added test.AbstractMDObject as superclass for every class in the episode/a project. But you've placed this class in the same project. It's not compiled before so plugin's can't (easily) check if AbstractMDObject implements CopyTo or any other interface in question. It's only present as a source code and, just from the source code, it's not easy to check, if the class implements a certain interface. It's not impossible, but it's quite hard to do correctly.

So what you should do is:

I think this should work with the current code. If not, we'll have to make a (new) reproducing test project for this under https://github.com/highsource/jaxb2-basics/tree/master/tests.

I'd also be grateful if you'd send me a pull request next time instead of a git diff. Working with PRs is much easier in GitHub, it's the way to provide project changes on GitHub.

krokodylowy commented 8 years ago

Create a new module which you compile before you generate code from you schemas. Move your superclasses (AbstractMDObject and likes) to this module. Compile it and include it into the classpath of the schema compiler.

My code already did it. Module A contains AbstractMDObject and it is on the classpath and pom dependecies in C module. C module compiled correcty and generated output contains C extends AbstractMDObject code. Only some methods is not generated correctly

Test added here https://github.com/krokodylowy/jaxb2-basics/commit/8c064cd1299bd9c1fbcc9c042f897d438a96d4af

krokodylowy commented 8 years ago

Adding autoInheritance also do not resolve problem with equals,clone and hashSet `-XautoInheritance

-XautoInheritance-xmlTypesExtend=test.AbstractMDObject`
krokodylowy commented 8 years ago

A partial workaround exist for equals and hashCode (not clone) Correct equals and hashCode is generated if bindings.xml is extended with `<jaxb:bindings node="xs:complexType[@name='BType']">

test.AbstractMDObject ` In above case equals and hashCode call's super.equals and super.hashCode. super.copyTo isn't called Finally inheritance:extends cannot be embedded in jaxb:globalBindings so it can't replace xjc:superClass
highsource commented 8 years ago

I've reproduced it now, xjc:superClass is indeed not considered by the plugins. It's quite easy to correct.

Do you have further issues with superclasses apart from xjc:superClass?

On Mon, Dec 14, 2015 at 11:26 AM, krokodylowy notifications@github.com wrote:

A partial workaround exist for equals and hashCode (not clone) Correct equals and hashCode is generated if bindings.xml is extended with <jaxb:bindings node="xs:complexType[@name='BType']"> inheritance:extendstest.AbstractMDObject/inheritance:extends /jaxb:bindings In above case equals and hashCode call's super.equals ans super.hashCode. super.copyTo isn't called

— Reply to this email directly or view it on GitHub https://github.com/highsource/jaxb2-basics/issues/42#issuecomment-164402373 .

krokodylowy commented 8 years ago

No. Just #43.

BTW. As a temporary workaround I mixed xjc:superClass with inheritance:extends and results are good but bindings code looks ugly now.

highsource commented 8 years ago

This should be fixed now, please give it a try.

Should work both with xjc:superClass and xjc:superInterface.