Examples - leangen/geantyref GitHub Wiki
You use TypeToken
to refer to specific types, and GenericTypeReflector.isSuperType
to check if a type is a supertype of another.
Given the following interface and classes:
interface Processor {
void process(T t);
}
class StringProcessor implements Processor<String> {
public void process(String s) {
System.out.println("processing " + s);
}
}
class IntegerProcessor implements Processor<Integer> {
public void process(Integer i) {
System.out.println("processing " + i);
}
}
We can check that a certain class is the right kind of Processor:
/**
* Returns true if processorClass extends Processor<String>
*/
public boolean isStringProcessor(Class<? extends Processor<?>> processorClass) {
// Use TypeToken to get an instanceof a specific Type
Type type = new TypeToken<Processor<String>>(){}.getType();
// Use GenericTypeReflector.isSuperType to check if a type is a supertype of another return
GenericTypeReflector.isSuperType(type, processorClass);
}
For example:
-
isStringProcessor(StringProcessor.class)
returnstrue
, becauseStringProcessor
extendsProcessor<String>
. -
isStringProcessor(IntegerProcessor.class)
returnfalse
, becauseIntegerProcessor
doesn't extendProcessor<String>
butProcessor<Integer>
Given the following classes:
abstract class Collector { public List list() { ... } public void add(T item) { ... } }
class StringCollector extends Collector<String> {
}
The return type of the Collector.list()
method is List<T>
. For StringCollector
, this would be List<String>
. But, if we simply get the return type from Java, we get List<T>
:
Method listMethod = StringCollector.class.getMethod("list");
Type returnType = listMethod.getGenericReturnType();
Then returnType is List<T>
, not what we want. To get the exact return type, use GenericTypeReflector.getExactReturnType
:
Type exactReturnType = GenericTypeReflector.getExactReturnType(listMethod, StringLister.class);
Now exactReturnType
is List<String>
.
Continuing with the sample above, if you ask Java, the parameter for the add method has type T
:
Method addMethod = StringCollector.class.getMethod("add", Object.class);
// returns [T]
Type[] parameterTypes = addMethod.getGenericParameterTypes();
You can get the exact parameter type using GenericTypeReflector.getExactParameterTypes
:
// returns [String]
Type[] exactParameterTypes = GenericTypeReflector.getExactParameterTypes(addMethod, StringCollector.class);