Generic Lazy initialize state solution using CGLIB

...
Raphaël ParréePublished on

I have meanwhile made my solution from the previous post more generic using annotations (@StateInitializeMethod for the state-initialisation 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=%dn",instance.getX());
  }
---

The source code (maven) for the LazyInitalizeStateClass is available here: blog.lazy.zip . The project contains unit tests written in Scala/Specs.