@Test public void getGenericsTypesByClass() { Class<? extends Serializer> clazz = SerializerImpl.class; Type[] types = TypeUtils.getGenericsTypesByClass(clazz, Serializer.class); assertThat(types.length).isEqualTo(2); assertThat(types[0].getTypeName()).isEqualTo("java.lang.String"); assertThat(types[1].getTypeName()).isEqualTo("java.lang.Integer"); }
public static Type[] getGenericsTypesByClass(Class clazz, Type type) { Class targetClass = clazz; while (targetClass != null) { for (Type in : targetClass.getGenericInterfaces()) { if (in instanceof ParameterizedType && type.equals(((ParameterizedType) in).getRawType())) { return ((ParameterizedType) in).getActualTypeArguments(); } } Type superClazz = targetClass.getGenericSuperclass(); if (superClazz instanceof ParameterizedType && type.equals(((ParameterizedType) superClazz).getRawType())) { return ((ParameterizedType) superClazz).getActualTypeArguments(); } targetClass = targetClass.getSuperclass(); } throw new UnsupportedOperationException("Not found serializer type: " + clazz.getName()); }
TypeUtils { public static Type[] getGenericsTypesByClass(Class clazz, Type type) { Class targetClass = clazz; while (targetClass != null) { for (Type in : targetClass.getGenericInterfaces()) { if (in instanceof ParameterizedType && type.equals(((ParameterizedType) in).getRawType())) { return ((ParameterizedType) in).getActualTypeArguments(); } } Type superClazz = targetClass.getGenericSuperclass(); if (superClazz instanceof ParameterizedType && type.equals(((ParameterizedType) superClazz).getRawType())) { return ((ParameterizedType) superClazz).getActualTypeArguments(); } targetClass = targetClass.getSuperclass(); } throw new UnsupportedOperationException("Not found serializer type: " + clazz.getName()); } }
TypeUtils { public static Type[] getGenericsTypesByClass(Class clazz, Type type) { Class targetClass = clazz; while (targetClass != null) { for (Type in : targetClass.getGenericInterfaces()) { if (in instanceof ParameterizedType && type.equals(((ParameterizedType) in).getRawType())) { return ((ParameterizedType) in).getActualTypeArguments(); } } Type superClazz = targetClass.getGenericSuperclass(); if (superClazz instanceof ParameterizedType && type.equals(((ParameterizedType) superClazz).getRawType())) { return ((ParameterizedType) superClazz).getActualTypeArguments(); } targetClass = targetClass.getSuperclass(); } throw new UnsupportedOperationException("Not found serializer type: " + clazz.getName()); } private  TypeUtils(); }
TypeUtils { public static Type[] getGenericsTypesByClass(Class clazz, Type type) { Class targetClass = clazz; while (targetClass != null) { for (Type in : targetClass.getGenericInterfaces()) { if (in instanceof ParameterizedType && type.equals(((ParameterizedType) in).getRawType())) { return ((ParameterizedType) in).getActualTypeArguments(); } } Type superClazz = targetClass.getGenericSuperclass(); if (superClazz instanceof ParameterizedType && type.equals(((ParameterizedType) superClazz).getRawType())) { return ((ParameterizedType) superClazz).getActualTypeArguments(); } targetClass = targetClass.getSuperclass(); } throw new UnsupportedOperationException("Not found serializer type: " + clazz.getName()); } private  TypeUtils(); static TypeName unbox(TypeName typeName); static Type[] getGenericsTypesByClass(Class clazz, Type type); static List<? extends TypeMirror> getGenericsTypesByTypeElement(TypeElement element, Class clazz); }
TypeUtils { public static Type[] getGenericsTypesByClass(Class clazz, Type type) { Class targetClass = clazz; while (targetClass != null) { for (Type in : targetClass.getGenericInterfaces()) { if (in instanceof ParameterizedType && type.equals(((ParameterizedType) in).getRawType())) { return ((ParameterizedType) in).getActualTypeArguments(); } } Type superClazz = targetClass.getGenericSuperclass(); if (superClazz instanceof ParameterizedType && type.equals(((ParameterizedType) superClazz).getRawType())) { return ((ParameterizedType) superClazz).getActualTypeArguments(); } targetClass = targetClass.getSuperclass(); } throw new UnsupportedOperationException("Not found serializer type: " + clazz.getName()); } private  TypeUtils(); static TypeName unbox(TypeName typeName); static Type[] getGenericsTypesByClass(Class clazz, Type type); static List<? extends TypeMirror> getGenericsTypesByTypeElement(TypeElement element, Class clazz);  }