Home /Modules/slrtp/SLRTPAdvice.sleela

SLRTPAdvice.sleela

lib/slrtp/SLRTPAdvice.sleela · 121 line(s) · sleela

<< lib/slrtp/SLRTPAdvice.sleela
   SLeeLa Standard Library Definition.
   Definition: An SLRTP advice -- the action taken at the join points its
   pointcut selects, akin to Java/AspectJ before / after-returning /
   after-throwing / around advice. Because the SLeeLa semantic checker is
   nominal and does not upcast, advice is DATA-DRIVEN rather than subclassed: an
   advice carries a pointcut plus an action code, and the processor applies the
   built-in action when the advice fires. The built-in actions cover the common
   cross-cutting concerns (tracing, counting, and around short-circuit /
   return-substitution); the join point it receives is mutable, so an around
   advice can decline to proceed or rewrite the return value. >>
#sleela 3.0
material;
class SLRTPAdvice {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 0;
  String metaAuthor = "Max Rupplin";
  String metaDate = "2026-11-06";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "MEARVK LLC";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-06";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 7;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  << Built-in action codes. An around advice may use SKIP or SUBSTITUTE to
     short-circuit the real call; the trace/count actions are side-effect only
     and never alter control flow. >>
  << ACTION_LOG records a trace line for the join point; ACTION_COUNT only
     increments this advice hit counter; ACTION_SKIP makes an around advice not
     proceed to the real call; ACTION_SUBSTITUTE skips and installs a fixed
     return value. >>
  int ACTION_LOG = 0;
  int ACTION_COUNT = 1;
  int ACTION_SKIP = 2;
  int ACTION_SUBSTITUTE = 3;

  String name = "advice";
  SLRTPPointcut pointcut;
  int action = 0;
  String substituteValue = "";

  << Running state: how many times this advice has fired, and the last trace
     line it produced (so a test or report can read it without stdout). >>
  int hits = 0;
  String lastTrace = "";

  << Build an advice. The pointcut selects where it applies; the action selects
     what it does. For ACTION_SUBSTITUTE, subValue is the return to install. >>
  void define(String adviceName, SLRTPPointcut pc, int act, String subValue) {
    name = adviceName;
    pointcut = pc;
    action = act;
    substituteValue = subValue;
  }

  String getName() { return name; }
  SLRTPPointcut getPointcut() { return pointcut; }
  int getAction() { return action; }
  int getHits() { return hits; }
  String getLastTrace() { return lastTrace; }

  << True when this advice applies to the join point. >>
  boolean appliesTo(SLRTPJoinPoint jp) {
    SLRTPPointcut pc = pointcut;
    return pc.matches(jp);
  }

  << Fire the advice at a (matched) join point. Returns true when the advice
     short-circuits an around call (SKIP or SUBSTITUTE); the processor reads
     that to decide whether the real call should still proceed. Always records
     a hit and a trace line so behaviour is observable without stdout. >>
  boolean fire(SLRTPJoinPoint jp) {
    hits = hits + 1;
    lastTrace = "[slrtp] " + name + " @" + jp.phaseName() + " " + jp.signature()
              + " (args=" + jp.getArgs() + ")";
    if (action == ACTION_SUBSTITUTE) {
      jp.setProceeded(false);
      jp.setReturnValue(substituteValue);
      return true;
    }
    if (action == ACTION_SKIP) {
      jp.setProceeded(false);
      return true;
    }
    << ACTION_LOG and ACTION_COUNT are observe-only. >>
    return false;
  }
}