Det er godt nok et traels API.
Jeg arbejdede lidt videre med cast'ene.
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TypeInfo {
private Class<?> clz;
private List<TypeInfo> params;
public TypeInfo(Class<?> clz) {
this.clz = clz;
this.params = new ArrayList<>();
}
public Class<?> getRawClass() {
return clz;
}
public List<TypeInfo> getParams() {
return params;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(clz.getName());
if(getParams().size() > 0) {
sb.append("<");
sb.append(getParams().stream().map(TypeInfo::toString).collect(Collectors.joining(",")));
sb.append(">");
}
return sb.toString();
}
public static TypeInfo analyze(Type typ) {
TypeInfo res = null;
if(typ instanceof Class) {
res = new TypeInfo((Class<?>)typ);
}
if(typ instanceof ParameterizedType) {
ParameterizedType ptyp = (ParameterizedType)typ;
res = new TypeInfo((Class<?>)ptyp.getRawType());
for(Type t : ptyp.getActualTypeArguments()) {
res.getParams().add(analyze(t));
}
}
return res;
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
public class GenericsFun {
public void something(final Map<String,List<BigInteger>> ml) {
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Method[] m = GenericsFun.class.getMethods();
for(Method m1 : m) {
System.out.println(m1.getName());
for(Type typ : m1.getGenericParameterTypes()) {
TypeInfo typinf = TypeInfo.analyze(typ);
System.out.println(" " + typinf);
}
}
}
}