A Java library of bean utilities for manipulating and inspecting Java classes implementing the Java Beans standard.
Licensed under BSD License.
You can obtain the eXparity-bean jar from maven central. To include your project in:
A maven project
<dependency>
<groupId>org.exparity</groupId>
<artifactId>exparity-bean</artifactId>
<version>1.0.3</version>
</dependency>
A project which uses ivy for dependency management
<dependency org="org.exparity" name="exparity-bean" rev="1.0.3"/>
The utilites are exposed from three class, for Type, Bean, and Graph. To clarify the difference between Bean and a Graph in the library is that Bean will limit it's scope to just the instance passed into into, Graph will descend into the children of the instance as well i.e. perform a deep inspection.
Property level operations are exposed by two classes, BeanProperty or TypeProperty, depending on if you're inspecting an Instance or a Class respectively.
Some example usages are below.
To get a list of the get/set properties on a Class you can use any of the 2 approaches below.
List<TypeProperty> properties = Type.type(MyClass.class).propertyList();
List<TypeProperty> properties = new Type(MyClass.class).propertyList();
which after static importing will look like this
for ( TypeProperty property : type(MyClass.class).propertyList() ) {
System.out.println("Property " + property.getName() + " is type " + property.getType());
}
A similar operation can be performed on an instance. For example
List<BeanProperty> properties = Bean.bean(new MyClass()).propertyList();
List<BeanProperty> properties = new Bean(new MyClass()).propertyList();
which after static importing will look like this
for ( BeanProperty property : bean(new MyClass()).propertyList() ) {
System.out.println("Property " + property.getName() + " has value " + property.getValue() );
}
Lastly the same operations are available on an entire object graph. For example
List<BeanProperty> properties = Graph.graph(new MyClass()).propertyList();
List<BeanProperty> properties = new Graph(new MyClass()).propertyList();
which after static importing will look like this
for ( BeanPropertyInstance property : graph(new MyClass()).propertyList() ) {
System.out.println("Property " + property.getName() + " has value " + property.getValue() );
}
To get an individual property on a Class you can use the TypeProperty class directly. For example:
TypeProperty property = Type.typeProperty(MyClass.class, "myProperty");
which after static importing can be used like this
if ( typeProperty(MyClass.class, "myProperty").isList() ) {
// Do something
}
To get an individual property on an instance you can use the BeanProperty class directly. For example:
BeanProperty property = Bean.beanProperty(new MyClass(), "myProperty");
which after static importing can be used like this
if ( beanProperty(new MyClass(), "myProperty").isList() ) {
// Do something
}
The library includes methods for Class, instances, and object graphs to:
- propertyList - Return a list of the Java Bean properties on a Class, instance, or object graph.
- propertyMap - Return a map of the Java Bean properties on a Class, instance, or object graph keyed on the property name.
- propertyType - Return the type for a property on a Class, instance, or object graph.
- propertyNamed - Return a property instance for a property on a Class, instance, or object graph.
- propertyValue - Return a value for a property on an instance or object graph.
- hasProperty - Check if the property exists on a Class, instance, or object graph.
- get - Return a property instance for a property on an instance, or object graph. (A synonym for propertyNamed).
- setValue - Set the value a property on a an instance, or object graph.
- find - Find all property instances on a Class, instance, or object graph which match a predicate.
- findAny - Return the first property instance on a Class, instance, or object graph which matches a predicate.
- visit - Visit all Java Bean properties on a Class, instance, or object graph.
- apply - Apply a function to all properties on a Class, instance, or object graph which matches a predicate
- simpleName - Return the class name for a Class or instance.
- canonicalName - Return the full class name including package name for a Class or instance.
- camelName - Return the class name for a Class or instance formatted using camel-case.
- packageName - Return the package name for a Class or instance.
- isArray - Return true if the Class or instance is an array.
- isEnum - Return true if the Class or instance is an enumeration.
- isPrimitive - Return true if the Class or instance is a primitive.
- is - Test if the Class or instance is an instance of the given type.
- superTypes - Return the supertypes of a Class or instance.
- getType - Return the ypes of a Class or instance.
The library includes methods for properties to:
- getName - Return the property name.
- hasName - Test if the property has the given name.
- getType - Return the type of the property.
- getTypeCanonicalName - Return the full name including package of the Class of the property.
- getTypeSimpleName - Return the name of the Class of the property.
- getAccessor - Return the property accessor Method.
- getMutator - Return the property mutator Method.
- getDeclaringType - Return the Class the property is declared on.
- getDeclaringTypeCanonicalName - Return the full name including packaging of Class the property is declared on.
- getDeclaringTypeSimpleName - Return the name of the Class the property is declared on.
- getTypeParameters - Return the generic parameters for the type of the property.
- getTypeParameter - Return the nth generic parameter for the type of the property.
- hasAnyTypeParameter - Test if any of the generic parameter types match any of the supplied types.
- hasAnyTypeParameter - Test if any of the generic parameter types match the supplied type.
- isType - Test if the property type is assignable from the supplied type.
- isIterable - Test if the property type implements Iterable..
- isGeneric - Test if the property type is generic.
- isPrimitive - Test if the property type is primitive.
- isArray - Test if the property type is an array.
- isString - Test if the property type is a String.
- isCharacter - Test if the property type is a Character or char.
- isByte - Test if the property type is a Byte or byte.
- isInteger - Test if the property type is an Integer or int.
- isLong - Test if the property type is a Long or long.
- isDouble - Test if the property type is a Double or double.
- isFloat - Test if the property type is a Float or float.
- isShort - Test if the property type is a Short or short.
- isBoolean - Test if the property type is a Boolean or boolean.
- isDate - Test if the property type is a java.util.Date.
- isMap - Test if the property type implements Map.
- isList - Test if the property type implements List.
- isSet - Test if the property type implements Set.
- isCollection - Test if the property type implements Collection.
- isEnum - Test if the property type is a Java enum.
The Javadocs include examples on all methods so you can look there for examples for specific methods
The source is structured along the lines of the maven standard folder structure for a jar project.
- Core classes [src/main/java]
- Unit tests [src/test/java]
The source includes a pom.xml for building with Maven
Changes 1.0.2 -> 1.0.3
- Drop dependency on exparity-stub
Changes 1.0.0 -> 1.0.1
- Add dump methods to print out properties and their values
- Add ordering of methods to make order consistent
- Remove BeanUtils to simpligy API to use Bean.bean(..)
- Remove GraphUtils to simplify API to use Graph.graph(..)
Developers:
- Stewart Bissett