Generic Lazy initialize state solution using CGLIB

2011-03-23

I have meanwhile made my solution in my 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.

This article does not necessarily reflect the technical opinion of EDC4IT, but purely of the writer. If you want to discuss about this content, please use thecontact ussection of the site