/espresso

Streamlined Java Beans for Java 8+

Primary LanguageJavaApache License 2.0Apache-2.0

ESPRESSO tests Security Rating Maintainability Rating Reliability Rating Maven Central

Espresso is a streamlined JavaBean library for version 8 or higher.

Goals

Provide a high-level library that makes it easy to:

  • Scan JavaBean classes for metadata
  • Create new instances of JavaBeans
  • Manipulate existing instances of JavaBeans

Non-Goals

  • Create a low-level library to help applications roll their own JavaBean processing capabilities. Plenty of libraries already do that well, e.g. Java Beans and Apache Commons BeanUtils.
  • Create a general-purpose reflections library for assisting in reflections tasks. Plenty of libraries already do that well, e.g. reflections.

What is a JavaBean?

A JavaBean is a Java class that adheres to the JavaBean spec. In short, a JavaBean is any Java class such that:

  • All its properties are private, with public getters and setters
  • It has a public no-argument constructor
  • It implements Serializable.

How does Espresso implement the JavaBean spec?

Espresso's implementation is generally faithful, but relaxes a few constraints. In Espresso:

  • Only instance properties are allowed. All static fields and methods are ignored.
  • Properties are not required to have precisely a private field and public getter/setter pair. Instead, a property may consist of any of the following:
    • A private field with a public getter/setter pair
    • A public field with or without a public getter and/or setter
    • A public getter/setter pair with no field
  • Classes are not requried to implement Serializable.

Espresso adds the following limitations out of practicality:

  • Classes must be instantiable (so, no abstract classes, interfaces, etc.).
  • Fields that hide or are hidden by another field in a superclass are ignored to avoid ambiguity.
  • Getters with multiple covariant signatures are ignored to avoid ambiguity. Method overriding with the same signature is allowed and respected.
  • Setters with multiple covariant signatures are ignored to avoid ambiguity. Method overriding with the same signature is allowed and respected.

Usage

Scan a class

BeanClass bc;
try {
    bc = BeanClass.scan(Example.class);
}
catch(IllegalArgumentException e) {
    // The given class cannot be parsed as a bean class.
    throw e;
}

for(BeanProperty p : bc)
    System.out.println("Example has a property named "+p.getName()+" of type "+p.getGenericType());

Create and manipulate a bean instance

class Example {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

BeanClass bc=BeanClass.scan(Example.class);

BeanInstance instance=bc.newInstance();

instance.set("value", "hello");

Example example=(Example) instance.getInstance();

System.out.println(example.getValue()); // prints "hello"

Manipulate an existing bean instance

class Example {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

BeanClass bc=BeanClass.scan(Example.class);

Example example=new Example();

BeanInstance instance=BeanInstance.wrap(example);

instance.set("value", "hello");

System.out.println(example.getValue()); // prints "hello"

Colophon

Espresso is a method for brewing delicious, high-caffeine coffee from a variety of different types of coffee beans.