Generic Lazy initialize state solution using CGLIB

I have meanwhile made my solution in my previous post more generic using annotations (@StateInitializeMethod for the init method(s) and @RequiresState for methods which require the state to have been initialized) , and it can be used like this:

public class DemoClass {
  private int x;
 
  @RequiresState
  public int getX(){
     return x;
  }
 
  @RequiresState
  public int getNumber(){
     return x;
  }
 
  @StateInitializeMethod
  public void init(){
    x =10;
  }
 
  public static void main(String[] args) {
    DemoClass instance = LazyInitalizeStateClass.getInstance(DemoClass.class);
    System.out.printf("x=%d\n",instance.getX());
  }
 
}

The source code for the LazyInitalizeStateClass is available here:  LazyDemo.tar

  • Share/Bookmark

About this entry