@Test public void checkList() { String cls; List<String> list = new ArrayList<>(); cls = ReflectTool.tryGetGeneric(list); assertEquals(cls, null); Mark<List<String>> mark = new Mark<List<String>>() { }; cls = ReflectTool.tryGetGeneric(mark); assertEquals(cls, "java.util.List<java.lang.String>"); IMark<List<String>> iMark = new IMark<List<String>>() { }; cls = ReflectTool.tryGetGeneric(iMark); assertEquals(cls, "java.util.List<java.lang.String>"); MarkImpl<List<String>> markImpl = new MarkImpl<List<String>>() { }; cls = ReflectTool.tryGetGeneric(markImpl); assertEquals(cls, "java.util.List<java.lang.String>"); }
public static <T> String tryGetGeneric(T t) { ParameterizedType pt = null; Type[] types = t.getClass().getGenericInterfaces(); if (types.length != 0 && types[0] instanceof ParameterizedType) { pt = (ParameterizedType) types[0]; } else { Type type = t.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { pt = (ParameterizedType) type; } } if (pt == null) { return null; } Type[] actual= pt.getActualTypeArguments(); String unsafe = actual[0].toString(); String genericString; if (unsafe.contains(",")) { genericString = unsafe; } else { String[] elms = unsafe.split(" "); genericString = elms.length <= 1 ? unsafe : elms[1]; } return genericString.length() < 4 ? null : genericString; }
ReflectTool { public static <T> String tryGetGeneric(T t) { ParameterizedType pt = null; Type[] types = t.getClass().getGenericInterfaces(); if (types.length != 0 && types[0] instanceof ParameterizedType) { pt = (ParameterizedType) types[0]; } else { Type type = t.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { pt = (ParameterizedType) type; } } if (pt == null) { return null; } Type[] actual= pt.getActualTypeArguments(); String unsafe = actual[0].toString(); String genericString; if (unsafe.contains(",")) { genericString = unsafe; } else { String[] elms = unsafe.split(" "); genericString = elms.length <= 1 ? unsafe : elms[1]; } return genericString.length() < 4 ? null : genericString; } }
ReflectTool { public static <T> String tryGetGeneric(T t) { ParameterizedType pt = null; Type[] types = t.getClass().getGenericInterfaces(); if (types.length != 0 && types[0] instanceof ParameterizedType) { pt = (ParameterizedType) types[0]; } else { Type type = t.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { pt = (ParameterizedType) type; } } if (pt == null) { return null; } Type[] actual= pt.getActualTypeArguments(); String unsafe = actual[0].toString(); String genericString; if (unsafe.contains(",")) { genericString = unsafe; } else { String[] elms = unsafe.split(" "); genericString = elms.length <= 1 ? unsafe : elms[1]; } return genericString.length() < 4 ? null : genericString; }  }
ReflectTool { public static <T> String tryGetGeneric(T t) { ParameterizedType pt = null; Type[] types = t.getClass().getGenericInterfaces(); if (types.length != 0 && types[0] instanceof ParameterizedType) { pt = (ParameterizedType) types[0]; } else { Type type = t.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { pt = (ParameterizedType) type; } } if (pt == null) { return null; } Type[] actual= pt.getActualTypeArguments(); String unsafe = actual[0].toString(); String genericString; if (unsafe.contains(",")) { genericString = unsafe; } else { String[] elms = unsafe.split(" "); genericString = elms.length <= 1 ? unsafe : elms[1]; } return genericString.length() < 4 ? null : genericString; }  static Context getApplication(); static String tryGetGeneric(T t); @SuppressWarnings("all") static String getFieldTypeWithGeneric(Field f); static boolean debugable(); }
ReflectTool { public static <T> String tryGetGeneric(T t) { ParameterizedType pt = null; Type[] types = t.getClass().getGenericInterfaces(); if (types.length != 0 && types[0] instanceof ParameterizedType) { pt = (ParameterizedType) types[0]; } else { Type type = t.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { pt = (ParameterizedType) type; } } if (pt == null) { return null; } Type[] actual= pt.getActualTypeArguments(); String unsafe = actual[0].toString(); String genericString; if (unsafe.contains(",")) { genericString = unsafe; } else { String[] elms = unsafe.split(" "); genericString = elms.length <= 1 ? unsafe : elms[1]; } return genericString.length() < 4 ? null : genericString; }  static Context getApplication(); static String tryGetGeneric(T t); @SuppressWarnings("all") static String getFieldTypeWithGeneric(Field f); static boolean debugable();  }