Annoation processor that enables declaring Java interfaces with imitation of structural subtyping support
- Add maven dependency:
<dependency>
<groupId>com.pragmaticobjects.oo.structsubtype</groupId>
<artifactId>structsubtype-codegen</artifactId>
<version>x.y.z</version>
</dependency>
- Declare some interfaces:
interface UserName {
String userName();
}
interface UserAddress {
String zip();
String address();
}
- Place
@StructSubtype
declaration to thepackage-info.java
of the package where you want the subtype to be generated
@StructSubtype(name = "User", inherits = { UserName.class, UserAddress.class }
package com.example;
import com.pragmaticobjects.oo.structsubtype.api.StructSubtype;
- Build the project. A source for new type will be generated at the annotated package. The type will be substitutable at each of it's testators.
interface User extends UserName, UserAddress {}
Note that generated interfaces within one package may be subtypes of each other, if one inherits all features of the other:
@StructSubtype(name = "Recipient", inherits = { UserName.class, UserAddress.class }
@StructSubtype(name = "PassportIdentity", inherits = { UserName.class, PassportId.class }
@StructSubtype(name = "User", inherits = { UserName.class, PassportId.class, UserAddress.class }
package com.example;
// Since `User` inherits every feature of `Recipient` and `PassportIdentity`, it is considered as a subtype of them.
interface User extends UserName, PassportId, UserAddress, Recipient, PassportIdentity {}