generating code regarding inherited structure

Hi,

I have three structure in my project, Indicator wich is abstract and EMAIndicator and SMAIndicator that extend from Indicator. At generation time EMAIndicator and SMAIndicator should generate instantiation of existing java class of the same name that extend Indicator interface. Here is the main template


public class $TemplateImpl extends AbstractTemplate implements <none> {
  <<static fields>>

  <<static initializer>>
  $LOOP$ private Indicator $indicator;
  <<properties>>
  <<initializer>>
  
  public TemplateImpl() {
    $LOOP${
  this.indicator = new ?
}
  }

  <<static methods>>

  <<inner classifiers>>
}

After generation I would like to have something like:

import be.jskills.financial.trading.template.AbstractTemplate;
import be.jskills.financial.trading.model.indicator.Indicator;
import be.jskills.financial.trading.model.indicator.EMAIndicator2;
import be.jskills.financial.trading.model.util.TimeFrame;
import be.jskills.financial.trading.model.util.ETemplate;

public class EmaCrossTemplate extends AbstractTemplate {
  private Indicator ema510;
  private Indicator ema58;

  private Indicator sma200;

  public EmaCrossTemplate() {
    this.ema510 = new EMAIndicator2();
    this.ema58 = new EMAIndicator2();

   this.sma200 = new SMAIndicator2();
  }


}

with the correct instanciation (new EMAIndicator2() or new SMAIndicator2()) regarding the the type of concept I introduce in the editor :

TEMPLATE: EmaCrossTemplate
direction BULLISH
  EMA ema510
    timeframe MIN5
    period 34
  EMA ema58
    timeframe MIN5
    period 5
  SMA sma200
    timeframe MIN5
    period <no period>
  << ... >>

Thanks a lot.

Kind regards

Thomas

2 comments
Comment actions Permalink

If your problem is only in generating creating of instance of Indicator, you can do the following:

  1. Write „new Object()“.
  2. Select „Object()” cell and create node macro of $COPY_SRC$ type.
  3. In code for macro, type something like:

if (node.isInstanceOf(EMAIndicator)) {

  return <EMAIndicator2()>;

} else if (node.isInstanceOf(SMAIndicator) {

  return <SMAIndicator2()>;

} else {

  return null;

}

where <> means quotations of ClassCreator concept.

Of course, it is better to replace „isInstanceOf” checks to abstract virtual method node<ClassCreator> getClassCreator() in Indicator concept behavior which is overriden in EMAIndicator and SMAIndicator concept behaviors.

0

Please sign in to leave a comment.