public G getType(String name) { return type(name); }
public static <T> Generic<T, Fields, Generic> of(Class<T> clazz) { return new Generic<T, Fields, Generic>(clazz); }
public Fields(Class clazz, FieldMapper<T, Generic> mapper) { this(Generic.of(clazz), mapper); }
public Generic(Class<T> c, G... generics) { clazz = c; parent = null; if (generics == null) typeParameters = createArray(0); else typeParameters = generics; types = getTypes(c, typeParameters); Type[] interfaces = clazz.getGenericInterfaces(); this.interfaces = createArray(interfaces.length); initInterfaces(this.interfaces, interfaces, types, new HashMap<Type, Generic<T, F, G>>()); }
public Generic(Class<T> c, G... generics) { init(); clazz = c; parent = null; if (generics == null) typeParameters = createArray(0); else typeParameters = generics; types = getTypes(c, typeParameters); Type[] interfaces = clazz.getGenericInterfaces(); this.interfaces = createArray(interfaces.length); initInterfaces(this.interfaces, interfaces, types, new HashMap<Type, Generic<T, F, G>>()); }
@Test public void interfacesTest() { Generic generic = new Generic(String.class); Assert.assertEquals(String.class, generic.clazz); Assert.assertEquals(3, generic.interfaces.length); for (int i = 0; i < generic.interfaces.length; i++) { Generic g = generic.interfaces[i]; if (g.clazz == Serializable.class) continue; if (g.clazz == CharSequence.class) continue; if (g.clazz == Comparable.class) { Assert.assertEquals(1, g.typesCount()); Generic type = g.type(0); Assert.assertEquals(String.class, type.clazz); Assert.assertSame(generic.interfaces, type.interfaces); continue; } Assert.assertTrue(false); } }
@Test public void wildcardTypeTest() { Fields<FieldInfo> fields = new Fields<FieldInfo>(WildcardTypeTestPojo.class); Assert.assertEquals(2,fields.size()); FieldInfo fieldInfo; fieldInfo = fields.get("superNumbers"); Assert.assertEquals(List.class, fieldInfo.generic.clazz); Assert.assertEquals(1, fieldInfo.generic.typesCount()); Assert.assertEquals(Number.class, fieldInfo.generic.type(0).clazz); Assert.assertEquals(0, fieldInfo.generic.type(0).typesCount()); fieldInfo = fields.get("extendsNumbers"); Assert.assertEquals(List.class, fieldInfo.generic.clazz); Assert.assertEquals(1, fieldInfo.generic.typesCount()); Assert.assertEquals(Number.class, fieldInfo.generic.type(0).clazz); Assert.assertEquals(0, fieldInfo.generic.type(0).typesCount()); } }
@Test public void interfacesTest_2() { Generic generic = new Generic(ChildInterface.class); Assert.assertEquals(ChildInterface.class, generic.clazz); Assert.assertEquals(1, generic.interfaces.length); Generic parent = generic.interfaces[0]; Assert.assertEquals(ParentInterface.class, parent.clazz); Assert.assertEquals(String.class, parent.type(0).clazz); Assert.assertEquals(1, parent.interfaces.length); Generic supr = parent.interfaces[0]; Assert.assertEquals(SuperInterface.class, supr.clazz); Assert.assertEquals(String.class, supr.type(0).clazz); }
protected void fillMethods(List<GenericMethod> methods, Generic generic) { Method[] declaredMethods = generic.clazz.getDeclaredMethods(); for (Method method : declaredMethods) { Generic returnType = new Generic(method.getGenericReturnType(), generic.types); Type[] genericParameterTypes = method.getGenericParameterTypes(); List<Generic> args = new ArrayList<Generic>(genericParameterTypes.length); for (Type genericParameterType : genericParameterTypes) { args.add(new Generic(genericParameterType, generic.types)); } methods.add(new GenericMethod(method, returnType, Collections.unmodifiableList(args))); } for (Generic g : generic.interfaces) { fillMethods(methods, g); } if (generic.parent != null) fillMethods(methods, generic.parent); }
this.typeParameters = createArray(args.length); for (int i = 0; i < args.length; i++) { this.typeParameters[i] = create(args[i], types, cyclicDependencies); this.types.put(variables[i].getName(), this.typeParameters[i]); parent = create(clazz.getGenericSuperclass(), this.types, cyclicDependencies); else parent = null; if (g == null) { Type type = variable.getBounds()[0]; g = create(type, types, cyclicDependencies); clazz = (Class<T>) Object.class; parent = null; typeParameters = createArray(0); typeParameters = createArray(1); typeParameters[0] = create(((GenericArrayType) c).getGenericComponentType()); } else if (c instanceof WildcardType) { WildcardType wildcardType = (WildcardType) c; Type type = wildcardType.getLowerBounds().length > 0 ? wildcardType.getLowerBounds()[0] : wildcardType.getUpperBounds()[0]; Generic<T, F, G> g = create(type); clazz = g.clazz; typeParameters = g.typeParameters; if (cl.isArray()) { clazz = (Class<T>) Array.class; typeParameters = createArray(1);
@Test public void firstTest() throws NoSuchFieldException { Generic generic = new Generic(AnotherWrapper.class); Assert.assertEquals(AnotherWrapper.class.getSimpleName(), generic.toString()); Assert.assertEquals(AnotherWrapper.class, generic.clazz); Assert.assertEquals(ListWrapper.class, generic.parent.clazz); Assert.assertEquals(Wrapper.class, generic.parent.parent.clazz); Assert.assertEquals(Object.class, generic.parent.parent.parent.clazz); Assert.assertEquals(null, generic.parent.parent.parent.parent); Generic type = generic.getGenericType(Wrapper.class.getDeclaredField("value")); Assert.assertEquals(List.class, type.clazz); Assert.assertEquals(Wrapper.class, type.typeParameters[0].clazz); Assert.assertEquals(String.class, type.typeParameters[0].typeParameters[0].clazz); }
public int getTypesCount() { return typesCount(); }
@Test public void thirdTest() throws NoSuchFieldException { Generic generic = new Generic(KeyValueStrings.class); Assert.assertEquals(KeyValueStrings.class, generic.clazz); Assert.assertEquals(KeyValueWrapper.class, generic.parent.clazz); Assert.assertEquals(KeyValue.class, generic.parent.parent.clazz); Assert.assertEquals(Object.class, generic.parent.parent.parent.clazz); Assert.assertEquals(null, generic.parent.parent.parent.parent); Generic type = generic.getGenericType(KeyValue.class.getDeclaredField("key")); Assert.assertEquals(String.class, type.clazz); }
@Test public void methodsTest() { Generic<ChildInterface, Fields, Generic> generic = Generic.of(ChildInterface.class); List<GenericMethod> methods = generic.methods(); GenericMethod method = methods.get(0); Assert.assertEquals("get", method.method.getName()); Assert.assertEquals(String.class, method.returnType.clazz); Assert.assertEquals(0, method.args.size()); }
@Test public void fieldsTest() throws NoSuchFieldException { Generic generic = new Generic(ArrayHolder.class); Fields map = generic.getFields(); Assert.assertEquals(2, map.size()); Assert.assertTrue(map.containsKey("array")); Assert.assertTrue(map.containsKey("list")); Assert.assertTrue(map == generic.getFields()); }
public List<GenericMethod> methods() { List<GenericMethod> methods = new ArrayList<GenericMethod>(); fillMethods(methods, this); return methods; }
protected void initInterfaces(Generic[] result, Type[] interfaces, Map<String, G> types, Map<Type, Generic<T, F, G>> cyclicDependencies) { for (int i = 0; i < interfaces.length; i++) { result[i] = create(interfaces[i], types, cyclicDependencies); } }
@Override public JsonGeneric getGenericType(Field f) { return super.getGenericType(f); }
public Generic(Class<T> c, Class... generics) { init(); clazz = c; parent = null; if (generics == null) { typeParameters = createArray(0); } else { typeParameters = createArray(generics.length); for (int i = 0; i < generics.length; i++) { typeParameters[i] = create(generics[i]); } } types = getTypes(c, typeParameters); Type[] interfaces = clazz.getGenericInterfaces(); this.interfaces = createArray(interfaces.length); initInterfaces(this.interfaces, interfaces, types, new HashMap<Type, Generic<T, F, G>>()); }
protected void fillMethods(List<GenericMethod> methods, Generic generic) { Method[] declaredMethods = generic.clazz.getDeclaredMethods(); for (Method method : declaredMethods) { Generic returnType = new Generic(method.getGenericReturnType(), generic.types); Type[] genericParameterTypes = method.getGenericParameterTypes(); List<Generic> args = new ArrayList<Generic>(genericParameterTypes.length); for (Type genericParameterType : genericParameterTypes) { args.add(new Generic(genericParameterType, generic.types)); } methods.add(new GenericMethod(method, returnType, Collections.unmodifiableList(args))); } for (Generic g : generic.interfaces) { fillMethods(methods, g); } if (generic.parent != null) fillMethods(methods, generic.parent); }