javax.enterprise.util.TypeLiteral,
made generally available outside the JEE context. Allows the passing around of
a "token" that represents a type in a typesafe manner, as opposed to
passing the (non-parameterized) Type object itself. Consider:
You might see such a typesafe API as:
class Typesafe {
<T> T obtain(Class<T> type, ...);
}
Consumed in the manner of:
Foo foo = typesafe.obtain(Foo.class, ...);Yet, you run into problems when you want to do this with a parameterized type:
List<String> listOfString = typesafe.obtain(List.class, ...); // could only give us a raw List
java.lang.reflect.Type might provide some value:
Type listOfStringType = ...; // firstly, how to obtain this? Doable, but not straightforward.
List<String> listOfString = (List<String>) typesafe.obtain(listOfStringType, ...); // nongeneric Type would necessitate a cast
The "type literal" concept was introduced to provide an alternative, i.e.:
class Typesafe {
<T> T obtain(TypeLiteral<T> type, ...);
}
Consuming code looks like:
List<String> listOfString = typesafe.obtain(new TypeLiteral<List<String>>() {}, ...);
This has the effect of "jumping up" a level to tie a java.
to a type variable while simultaneously making it short work to obtain a
Type instance for any given type, inline.
Additionally TypeLiteral implements the Typed interface which
is a generalization of this concept, and which may be implemented in custom classes.
It is suggested that APIs be defined in terms of the interface, in the following manner:
<T> T obtain(Typed<T> typed, ...);
| Modifier and Type | Field and Description |
|---|---|
| private static final TypeVariable | |
| private final String | |
| public final Type | value
Represented type. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public final boolean | equals(final Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public Type | getType()
Implements org. Gets the |
| public int | |
| public String |
| T | back to summary |
|---|---|
private static final TypeVariable<Class<TypeLiteral<T>>> T
| |
| toString | back to summary |
|---|---|
| private final String toString | |
| value | back to summary |
|---|---|
| public final Type value Represented type. | |
| TypeLiteral | back to summary |
|---|---|
| protected TypeLiteral() Constructs a new instance. | |
| equals | back to summary |
|---|---|
| public final boolean equals(final Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| getType | back to summary |
|---|---|
| public Type getType() Implements org. Doc from org. Gets the
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |