I need a recursive function, that would print a class signature, including class name and values uf type parameters.
MyNewClass< String,List< Integer>,Map< List< Integer>,String>> x = new MyNewClass<...>(); Assert.assert(getSimpleNameWithGenerics(x).equals("MyNewClass< String,List< Integer>,Map< List< Integer>,String>>"));
Since I'm a newbie in Java, I was quite surprised, not to find such function anywhere around Java community, nor to be able to implement it easily myself.
Here is my try:
public static String getGenericClassSimpleName(Class< ?> c) {
String s = c.getSimpleName(); TypeVariable[] tv = c.getTypeParameters(); for(int i = 0; i < tv.length; i++) { s += i == 0 ? "<" : ","; s += getGenericClassSimpleName(tv[i].getGenericDeclaration().getClass()); } if(tv.length > 0) s += ">"; return s;
}
, but it ends up with stack overflow (that's why I decided to ask here, lol), bumping the TypeVariable class... I never found the way to get to real "faces" of the type variables.