Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language AspectJ

(Requires AspectJ Version 5)

Date:11/30/05
Author:Tilo Dickopp
URL:http://www.tiloswelt.de/
Comments:0
Info:http://eclipse.org/aspectj/
Score: (3.01 in 70 votes)
aspect BeerAspect {
  static String[][] REPLACE = {
    { "1 bottles", "1 bottle" },
    { "^0", "No more" },
    { "0", "no more" },
    { ".*-1",  "Go to the store and buy some more, 99" },
  };
  
  void around (String text, int count) : 
      call(* *.println(..)) && 
      args(text) && 
      cflowbelow(call (* Beer.lyrics(..)) && args(count)) && 
      !within(BeerAspect) &&
      if(count <= 2) {
    for (String[] replace : REPLACE) {
      text = text.replaceAll(replace[0], replace[1]);
    }
    proceed(text, count);
  }

  after(int count) :
      call (* Beer.lyrics(..)) && 
      args(count) && 
      if (count > 0) {
    ((Beer)thisJoinPoint.getTarget()).lyrics(count - 1);
  }
}

class Beer {
  static final String BEER = " bottles of beer";
  static final String BEER_WALL = BEER + " on the wall";
  static final String TAKE = "Take one down and pass it around, ";
  
  void lyrics(int count) {
    System.out.println(count + BEER_WALL + ", " + count + BEER + ".");
    System.out.println(TAKE + (count - 1) + BEER_WALL + ".");
    System.out.println();
  }
  
  public static void main(String[] args) {
    new Beer().lyrics(99);
  }
}

Download Source | Write Comment

Alternative Versions

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: