POJO handling with mhu-lib

mhu-lib brings a full featured POJO handler. The framework is able to parse POJO objects uses the found attributes to get and set values. Also a toolset to transform JSON or XML structures directly into/from POJO objects. It’s al located in the package ‘de.mhus.lib.core.pojo’.

The base class is the POJO Parser. It’s not working a lot but it brings all together. First important choice is how to parse. Parse strategies are implemented by default looking for attributes (AttributesStrategy) or functions (FunctionsStrategy). The default strategy (DefaultStrategy) combines both but it’s possible to change the strategy object for the parser. Strategies are also looking for the @Embedded annotation and parse deeper inside this attributes. Important: The attribute based Strategy is also able to access ‘private’ declared values! Is no need to declare them all ‘public’.

The strategy creates a PojoModel which can be manipulated by filters. The default filter (DefaultFilter) remove @Hidden tagged attributes. The resulting model allows the coder to access the attributes.

POJO handling with mhu-lib

An example POJO:

public class MyPojo {
  private String name;
  private long id;
  private String displayName;

  public String getName() {
    return name;
  }
  public String getDisplayName() {
    return displayName;
  }
  public void getId() {
    return id;
  }
}

Now use the POJOParser to create the POJOModel:

model = new PojoParser().parse(MyPojo.class).getModel();

More complex, use only @Public tagged attributes and concat @Embedded with “_”.:

model = new PojoParser().parse(MyPojo.class, "_", new Class[] {Public.class}).filter(true,false,true,false,true).getModel();

To set the values get the model parameters. Identifiers will be lower case. To access ‘displayName’ the identifier is ‘displayname’:

model.getAttribute("displayname").set(instance, "this is a sample");

The framework is a fast, stable and flexible way to use POJO objects in a common way.

Tipp: Create the model once and use it for all POJOs of the same type.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s