public class Generics extends Object
Provides utilities for generic relevant operations
| Constructor and Description |
|---|
Generics() |
| Modifier and Type | Method and Description |
|---|---|
static Map<String,Class> |
buildTypeParamImplLookup(Class theClass)
Build class type variable name and type variable implementation lookup
|
static void |
buildTypeParamImplLookup(Class theClass,
Map<String,Class> lookup) |
static <T> Class<T> |
classOf(Type type) |
static Class<?> |
getReturnType(Method method,
Class<?> theClass)
Return the real return type of a method.
|
static void |
main(String[] args) |
static Map<String,Class> |
subLookup(Map<String,Class> lookup,
String prefix) |
static List<Type> |
tryGetTypeParamImplementations(Class theClass,
Class rootClass)
Same function with
typeParamImplementations(Class, Class). |
static List<Type> |
typeParamImplementations(Class theClass,
Class rootClass)
Returns implementation of type parameters declared in the root class/interface by the given sub class
|
public static List<Type> typeParamImplementations(Class theClass, Class rootClass)
Returns implementation of type parameters declared in the root class/interface by the given sub class
For example, suppose a super class has generic type params: MyBase<MODEL, QUERY>, and a sub class is declared as MyModel<MyModel, MyQuery>, then passing MyModel.class to this method shall return a list of {MyModel.class, MyQuery.class}
If the specified class doesn’t have generic type declared then it shall return an empty list
theClass - the end classrootClass - the root class or interfacepublic static List<Type> tryGetTypeParamImplementations(Class theClass, Class rootClass)
Same function with typeParamImplementations(Class, Class).
However this method will return an empty list instead of throwing out exception when type parameter not found.
theClass - the end classrootClass - the root class or interfacepublic static Map<String,Class> buildTypeParamImplLookup(Class theClass)
Build class type variable name and type variable implementation lookup
theClass - the class to build the lookuppublic static void buildTypeParamImplLookup(Class theClass, Map<String,Class> lookup)
public static Class<?> getReturnType(Method method, Class<?> theClass)
Return the real return type of a method.
Normally it returns Method.getReturnType()
In case a method is declared in a super type and the return type is declared as a generic TypeVariable, when a sub class with type variable implementation presented, then it shall return the implementation type. E.g
Super type:
public abstract class Foo<T> {
T getFoo();
}
Sub type:
public class StringFoo extends Foo<String> {
String getFoo() {return "foo";}
}
Usage of getReturnType:
Method method = Foo.class.getMethod("getFoo");
Class<?> realReturnType = Generics.getReturnType(method, StringFoo.class); // return String.class
method - the methodtheClass - the class on which the method is invokedpublic static void main(String[] args)
Copyright © 2014–2019 OSGL (Open Source General Library). All rights reserved.