// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
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;
    }
}
