Before and After Statements
[Previous]  [Next] 
|
User Manual [Previous]  [Next] Before and After StatementsYou can request that certain code be run before or after various Umple-defined actions on attributes, associations, and the components of state machines as well as on user-defined methods.
Using 'before' allows you to enforce preconditions, such that the attribute or association will not be set if the precondition is not true. To reject setting a value, add 'return false'. Code in a setMethod can access the value to be set by prefacing it by 'a' as in the second example or 'an'. You can add before and after clauses to add code to generated methods of the form getX, setX, addX, removeX, getXs (to get all elements of an association), numberOfXs, indexOfX, where X is the name of the attribute or association. You can also add code before and after the constructor. See also this example of using aspect-orientation to help interface Umple with existing libraries. Example with "after" statementclass X { a; whenWasASet; after setA { setWhenWasASet(getA() + System.currentTimeMillis()); } } // @@@skipcppcompile Load the above code into UmpleOnline Example with "before" statementclass Person { name; before setName { if (aName != null && aName.length() > 20) { return false; } } } // @@@skipcppcompile Load the above code into UmpleOnline Example with both "before" and "after" statementsclass Operation { const Boolean DEBUG=true; query; before constructor { if (aQuery == null) { throw new RuntimeException("Please provide a valid query"); } } after constructor { if (DEBUG) { System.out.println("Created " + query); } } } // @@@skipcppcompile Load the above code into UmpleOnline Syntax// Aspect oriented code injection: BeforeandAfterStatements codeInjection- : [[beforeCode]] | [[afterCode]] beforeCode : ( before | [=around] ) [[aspectBody]] afterCode : after [[aspectBody]] |