k3 icon indicating copy to clipboard operation
k3 copied to clipboard

How to call correct method in xtend when having inheritance

Open tongbong opened this issue 10 years ago • 0 comments

Given the following piece of metamodel:

2014-06-19_1158

See ActionSequenceAspect, this code will call the correct perform operation (ie operation from the aspect) if and only if the perform operation is not defined on Action in the metamodel and if the perform operation in the aspect is overriding the base operation.

Note that "a.perform" will actually call the perform operation on the ActionAspect class that will delegate the task by calling the perform operation on the correct aspect class.

This is very tricky and require a deep understanding of the java code generation process in order to write some worling code.

A nice documentation would certainly help.

@Aspect(className=typeof(Action))
class ActionAspect {
    def public void perform() { 
        throw new UnsupportedOperationException();
    }
}

@Aspect(className=typeof(StopAction))
class StopActionAspect extends ActionAspect {

    @OverrideAspectMethod
    def public void perform() { 
        RobotFacadeRegistry.getUniqueFacade().stop();
    }
}

@Aspect(className=typeof(MoveForwardAction))
class MoveForwardActionAspect extends ActionAspect  {
    @OverrideAspectMethod
    def public void perform() { 
        RobotFacadeRegistry.getUniqueFacade().moveOneStepForward();
    }
}

@Aspect(className=typeof(ActionSequence))
class ActionSequenceAspect extends ActionAspect 
{

    @OverrideAspectMethod
    def public void perform() { 
        for(Action a : _self.actions)
        {
            a.perform()
            //ActionAspect.perform(a)
        }
    }

}

tongbong avatar Jun 19 '14 11:06 tongbong