jaxb-tools
jaxb-tools copied to clipboard
Inheritance Plugin Question
I try to let two generated classes implement their own interface. Example Class A should implement IA Class B should implement IB
This is working so far (wiht XInheritance plugin) as long as class A and B are implementing their interfaces. But the interfaces are not used: e.g. the object factory returns on createA and createB methods A / B classes instead of IA and IB. Even worse: If class A holds a List of type B then the created get method is public List<B> getBs instead of public List<IB> getBs.
Is there a way to achieve this? Or is XInheritance in general only focusing on the class specified in the bindings file and not taken care of the references to the given class?
For me and my current situation: I can work around this because B is not necessarily forced to implement IB. But it would be nicer to work on interfaces everywhere possilbe so I wanted to ask and clarify. If this is not possible at the moment then just tell me and close the issue.
I think this was by design since (not sure but if not it should) you could define 2 or more interface for the same base class with the inheritance plugin.
Moreover, you could downcast by calling methods with interface signature (IA
) when using objects (A
).
The goal of inheritance plugin is to give you ability to specify a special base class (extends) or base interface (implements) in generated code from xjc maven-plugin.
@mattrpav could you confirm ?
@laurentschoelens I can confirm that it is doeing what you described. If intention is to have only a e.g. abstract base class or interface for a class (or all classes) that works fine (and is the way I used in the past). Nevertheless, now I stumbled over the case I described above. No need to have two interfaces implemented. But rather the need to have the specified interface used throughout the references of a class everywhere within the generated code (e.g. ObjectFactory and Classes referencing the class with Interface instead of classname...)
@laurentschoelens @SebasHein XSD has a gap in the spec where there is not a way to define an 'interface' type concept.
Order
+ Collection<Item> getItems() <--- where Item is an interface.
The inheritance plugin allows you to apply existing interfaces to model classes or define classes to extend.
To replace return types on a xjc generated class, you'd need to do the following:
- Create the interface class in the model project (or dependency)
- Use inheritance plugin to have models implement the interface
- Use some OTHER xjc plugin to replace the references of the abstract/base type models with the interface class.
Ok thanks This is what I wanted to know :-) After reading documentation and some trial and error I was not able to found a solution. As written above I just wanted to clarify wether this is possible or not. And, unfortunately, its not (at least not with XInheritance). @mattrpav : Any suggestions what to use instead?
If it's a need, it could be somewhat a new feature of the plugin ?