1

I have the following problem:

I have two generated classes that have only 1 difference. The version-number in the namespace (http://www.sample.com/soap/xsd/2/ia).

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleType", namespace = "http://www.sample.com/soap/xsd/2/ia", propOrder = {
    "decision"})
public class SampleType {
   protected boolean isChecked;
   //getter & setter
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleType", namespace = "http://www.sample.com/soap/xsd/3/ia", propOrder = {
    "decision"})
public class SampleType {
   protected boolean isChecked;
   //getter & setter
}

project structure:

com.sample.myapp
     |
     |_ v2
     |   |_ SampleType.class
     |
     |_ v3
         |_ SampleType.class

com.sample.myOtherApp
     |
     |_ FlagChecker.class

caller class:

@Service("com.sample.myOtherApp.FlagChecker")
public class FlagChecker {
    public Boolean isChecked(SampleType sample){
       return sample.isChecked();
    }
}

What I tried:

  • In the case my Sample.class would be a POJO I'd simply make an Interface and use it in the signature of my isChecked()-method. This is not possible because Sample.classare generated from a xsd or I don't know how to add the interface. I use JAXB to generate the java-classes. Manually adding is no option.

  • I tried to use generics but it feels like I'm off the path.

Question: Is it possible to inject an interface in the jaxb-class-generation-process or is it possible to achieve what I want with generics or is there another straight forward solution?

EDIT: The solution mentioned by @Erwin Boldwit pointing to this answer would be awesome but I get my xsd's from thirdparty and it is generally a bad idea to alter xsd's isn't it? There is no need to alter the xsd. The xjb is in a second file. I refused the linked answer before because I thought it needs alteration of the basefile.

Community
  • 1
  • 1
SWiggels
  • 2,159
  • 1
  • 21
  • 35
  • Generics won't help much and I'm not sure about the interfaces. What you could try is provide overloads for your method in `FlagChecker` (although that might be hard to read since you normally don't see the packages), accept an `Object` parameter or provide a wrapper that knows about those versions. – Thomas Nov 17 '16 at 08:31
  • 2
    Possible duplicate of [Generating a JAXB class that implements an interface](http://stackoverflow.com/questions/1271980/generating-a-jaxb-class-that-implements-an-interface) – Erwin Bolwidt Nov 17 '16 at 08:40
  • Thank you for your answer @Thomas. The optimal solution would be that I don't have to alter the code in future versions, means a v4 should not need changes in `FlagChecker`. What do you mean with `provide a wrapper that knows about those versions`? – SWiggels Nov 17 '16 at 08:41
  • 2
    I suggest you look at the duplicate. It's easy to have JAXB add an "implements interface" while generating the Java code and you can configure it in an external configuration file. – Erwin Bolwidt Nov 17 '16 at 08:43
  • You actually did not stated *what you want*. – lexicore Nov 17 '16 at 09:40
  • By wrapper I meant a class that provides the interface and delegates the calls to the SampleType it gets (in fact the more correct pattern name would be adapter). If you'd check whether the wrapped object is a `SampleType` and use reflection to call `isChecked()` you'd probably not have to change the class if you get a v4 which still fulfills the contract. But that's a fragile solution and if adding interfaces via JAXB works - as pointed out by Erwin - I'd prefer that. – Thomas Nov 17 '16 at 09:52
  • The answer that @ErwinBolwidt pointed you to is the actual answer you need. You don't need to change your xsd for that you need to specify a jaxb binding file. In that file specify an interface to implement and program to that interface (instead of the actual class) and as long as each version has the same signature you don't need to change the code. – M. Deinum Nov 17 '16 at 15:45
  • I agree to @M.Deinum. And would accept an answer from Erwin Boldwidt. I upvoted his comment already. – SWiggels Nov 24 '16 at 10:59

0 Answers0