An answer to an old question.
I took a look at Apache Zest. Maybe it was just me but I find the examples a little bit cumbersome. And I couldn't quite get the point. Another alternative may be object teams.
But I suggest you may take a look at this repo:
https://github.com/Mashashi/javaroles/
It might partially cover what you want to do. It seems simple.
Here is an example:
Defining the interface for the roles:
public interface Human {
String hello();
String die(String age);
String eat();
String dance();
}
public interface Monkey {String hello(); String eat();}
Defining rigid type AnimalRoles...
public class AnimalRoles implements Human, Monkey{
public static final String HALLO = "Default hallo";
public static final String DIE = "Default they kill me...";
public static final String EAT = "Default eat...";
@ObjectForRole public Human human;
@ObjectForRole public Monkey monkey;
public AnimalRoles(Human human, Monkey monkey){
this.human = human;
this.monkey = monkey;
if(this.human!=null){
((Portuguese)this.human).core = this;
}
}
@Override
public String hello() {
return HALLO;
}
@Override
public String die(String age) {
return DIE+age;
}
@Override
@TurnOffRole
public String eat() {
return EAT;
}
@Override
public String dance() {
return "Just dance";
}
public String notInRole(){
return "Oh oh";
}
}
Defining class role Bonobo...
public class Bonobo implements Monkey{
public Bonobo() {}
@Override
public String hello(){
return "Ugauga";
}
@Override
public String eat() {
return "Nhamnham";
}
}
Defining class role Portuguese...
@RoleObject(types = { AnimalRoles.class })
public class Portuguese implements Human{
public static final String HALLO = "Hey there";
public static final String DIE = "They killed me";
public static final String EAT = "Eating boiled pork now";
public AnimalRoles core;
public Portuguese() {}
@Override
public String hello() {
return HALLO;
}
@Override
public String die(String age) {
return DIE+age;
}
@Override
public String eat() {
return EAT;
}
@Override
public String dance() {
return core.dance()+" modified!";
}
}
Running the test...
new RoleRegisterComposition().registerRools();
AnimalRoles a = new AnimalRoles(new Portuguese(), new Bonobo());
System.out.println(a.hello());
System.out.println(a.dance());
Would print...
"Hey there"
"Dance modified!"