Skip to content

FEATURE IDEA: @After

MarcoServetto edited this page Apr 9, 2018 · 4 revisions

Problem

Some time the code generated is quite perfect, but lack some terminal operation. For example we may want to call some validation in the end of the constructor, or do some logging, or anything at all.

Solution

Lets introduce an annotation @After("Name") to put in front of methods, so that the annotated method is called after the method named "Name". Of course, such method need to be a method of the class, or a static method of the class, so there is no need to check code any in other classes. Constructors will need a special name, like init. Overloading can be resolved by using fully qualified method names (as for example @After("init(float,double)")). If the fully qualification is not present, the method is called after all the overloaded versions.

For example,

@Data class A{
  int f1;
  int f2;
  @After("init")void m(){if(this.f1>this.f2){...}}
  @After("getF1")void m2(){...}
  }

Would call m after the constructor and m2 after the getter for f2.