jeudi 24 août 2017

How to avoid duplicate code with generated classes from different packages

We generate Java classes based on two XSD files. The classes land in their package respective to the XSD file (a and b). The methods we would like to use of the generated classes are the same. But they don't implement an interface or a super class. We do not have control over the XSD, it's provided.

Example:

package code.generated.a;
class X {
  void do() {...}
}

package code.generated.b;
class X {
  void do() {...}
}

We would now like to use those classes in our own written code, but would not like to duplicate it.

So as of now we have to consume the interfaces by duplicating the code:

package code.ours.a
class Consumer {
  void use(code.generated.a.X x) {...}
}

package code.ours.b
class Consumer {
  void use(code.generated.b.X x) {...}
}

What is the recommended way of resolving the duplicate code?

Implicit interfaces of Go come to mind, but we're dealing with Java. Is there a way to this with generics?

Aucun commentaire:

Enregistrer un commentaire