list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Code Injection in Custom Methods

Much of the usefulness of code injection is for generated methods, but it is also possible to inject code into custom methods, using the custom keyword.

Example

// This illustrates separate code injection
// for custom and generated methods
// The output will be as follows
// Sound of beep
// Warning
//  ... this is injected after the warning
// Sound of beep
// Warning
//  ... this is injected after the warning
// Sound of beep
// Warning
// ... this is injected after the warning
//
class Car {
  queued radio {
    on {
      radioToggle -> off;
    }
    off {
      radioToggle -> on;
    }
  }
  after generated radioToggle* {soundBeep();}
  
  void soundBeep() {
    System.out.println("Sound of beep");
    radioToggleWarning();
  }

  void radioToggleWarning() {
    System.out.println("Warning");
  }
  
  after custom radioToggle* {
    System.out.println(
      " ... this is injected after the warning");  
  }
  
  public static void main(String[] args) {
    Car x = new Car();
    x.radioToggle();
    x.radioToggle();
    x.radioToggle();   
  }
}

      

Load the above code into UmpleOnline