javaRuntype is a compact library aimed at offering a runtime representation of the Java type system.
This is specially useful in metadata-based systems.
This becomes useful because, on the one hand, Java's java.util.Class objects cannot contain generics information (a "List<String>" object at compile time becomes just "List" at runtime) , and on the other, the java.lang.reflect.Type interface and its related hierarchies, which the JVM uses for defining attributes and method signatures, are second-level citizens in the API, and lack an easy-to-use framework to truly enable the use of types as metadata in Java applications.
javaRuntime's type system representation mainly comprises two kinds of objects: Types and TypeDefs.
A Type object represents an instantiation of a Type Definition, for example:
Type objects are obtained through the Types class (org.javaruntype.type.Types), and with the help of the TypeParameters (org.javaruntype.type.TypeParameters) class:
// // "Types.listOf(...)" is a static utility method in Types. // "Types.STRING" is a static constant for the java.lang.String Type. // // listOfStringType = "java.util.List<java.lang.String>" // Type<List<String>> listOfStringType = Types.listOf(Types.STRING); // For this type, we need to create an upper bound Type<List<? extends Serializable>> listOfUnknownExtSerializableType = Types.listOf(TypeParameters.forExtendsType(Types.SERIALIZABLE)); // List<? extends Serializable> is assignable from List<String> Validate.isTrue(listOfUnknownExtSerializableType.isAssignableFrom(listOfStringType));
For more information, go to the Using Types page.
A TypeDef object represents the type definition of a class, and it serves as a base on which Type objects can be created and validated. It is possible to use TypeDef objects directly, although normally their existence will be shadowed by Types.
Some examples of TypeDefs:
TypeDef objects are obtained through the TypeDefs class:
// stringTypeDef = "java.lang.String" TypeDef stringTypeDef = TypeDefs.forClass(String.class); // listTypeDef = "java.util.List<E>" TypeDef listTypeDef = TypeDefs.forClass(List.class);