@Test void createListInstance() { final DefaultValueInspector inspector = new DefaultValueInspector(); final DefaultValueInspector.Instance demoInstance = inspector .createDemoInstance(new Wrapper(), new ParameterMeta(null, new JohnzonParameterizedType(List.class, Foo.class), ARRAY, "foos", "foos", null, singletonList(new ParameterMeta(null, Foo.class, OBJECT, "foos[${index}]", "foos[${index}]", null, singletonList(new ParameterMeta(null, Foo.class, STRING, "foos[${index}].name", "name", null, emptyList(), null, emptyMap(), false)), null, emptyMap(), false)), null, emptyMap(), false)); assertNotNull(demoInstance.getValue()); assertTrue(demoInstance.isCreated()); assertTrue(Collection.class.isInstance(demoInstance.getValue())); final Collection<?> list = Collection.class.cast(demoInstance.getValue()); assertEquals(1, list.size()); final Object first = list.iterator().next(); assertNotNull(first); assertTrue(Foo.class.isInstance(first)); assertNull(Foo.class.cast(first).name); }
public Instance createDemoInstance(final Object rootInstance, final ParameterMeta param) { if (rootInstance != null) { final Object field = findField(rootInstance, param); if (field != null) { return new Instance(field, false); } } final Type javaType = param.getJavaType(); if (Class.class.isInstance(javaType)) { return new Instance(tryCreatingObjectInstance(javaType), true); } else if (ParameterizedType.class.isInstance(javaType)) { final ParameterizedType pt = ParameterizedType.class.cast(javaType); final Type rawType = pt.getRawType(); if (Class.class.isInstance(rawType) && Collection.class.isAssignableFrom(Class.class.cast(rawType)) && pt.getActualTypeArguments().length == 1 && Class.class.isInstance(pt.getActualTypeArguments()[0])) { final Object instance = tryCreatingObjectInstance(pt.getActualTypeArguments()[0]); final Class<?> collectionType = Class.class.cast(rawType); if (Set.class == collectionType) { return new Instance(singleton(instance), true); } if (SortedSet.class == collectionType) { return new Instance(new TreeSet<>(singletonList(instance)), true); } if (List.class == collectionType || Collection.class == collectionType) { return new Instance(singletonList(instance), true); } return null; } } return null; }
DefaultValueInspector { public Instance createDemoInstance(final Object rootInstance, final ParameterMeta param) { if (rootInstance != null) { final Object field = findField(rootInstance, param); if (field != null) { return new Instance(field, false); } } final Type javaType = param.getJavaType(); if (Class.class.isInstance(javaType)) { return new Instance(tryCreatingObjectInstance(javaType), true); } else if (ParameterizedType.class.isInstance(javaType)) { final ParameterizedType pt = ParameterizedType.class.cast(javaType); final Type rawType = pt.getRawType(); if (Class.class.isInstance(rawType) && Collection.class.isAssignableFrom(Class.class.cast(rawType)) && pt.getActualTypeArguments().length == 1 && Class.class.isInstance(pt.getActualTypeArguments()[0])) { final Object instance = tryCreatingObjectInstance(pt.getActualTypeArguments()[0]); final Class<?> collectionType = Class.class.cast(rawType); if (Set.class == collectionType) { return new Instance(singleton(instance), true); } if (SortedSet.class == collectionType) { return new Instance(new TreeSet<>(singletonList(instance)), true); } if (List.class == collectionType || Collection.class == collectionType) { return new Instance(singletonList(instance), true); } return null; } } return null; } }
DefaultValueInspector { public Instance createDemoInstance(final Object rootInstance, final ParameterMeta param) { if (rootInstance != null) { final Object field = findField(rootInstance, param); if (field != null) { return new Instance(field, false); } } final Type javaType = param.getJavaType(); if (Class.class.isInstance(javaType)) { return new Instance(tryCreatingObjectInstance(javaType), true); } else if (ParameterizedType.class.isInstance(javaType)) { final ParameterizedType pt = ParameterizedType.class.cast(javaType); final Type rawType = pt.getRawType(); if (Class.class.isInstance(rawType) && Collection.class.isAssignableFrom(Class.class.cast(rawType)) && pt.getActualTypeArguments().length == 1 && Class.class.isInstance(pt.getActualTypeArguments()[0])) { final Object instance = tryCreatingObjectInstance(pt.getActualTypeArguments()[0]); final Class<?> collectionType = Class.class.cast(rawType); if (Set.class == collectionType) { return new Instance(singleton(instance), true); } if (SortedSet.class == collectionType) { return new Instance(new TreeSet<>(singletonList(instance)), true); } if (List.class == collectionType || Collection.class == collectionType) { return new Instance(singletonList(instance), true); } return null; } } return null; }  }
DefaultValueInspector { public Instance createDemoInstance(final Object rootInstance, final ParameterMeta param) { if (rootInstance != null) { final Object field = findField(rootInstance, param); if (field != null) { return new Instance(field, false); } } final Type javaType = param.getJavaType(); if (Class.class.isInstance(javaType)) { return new Instance(tryCreatingObjectInstance(javaType), true); } else if (ParameterizedType.class.isInstance(javaType)) { final ParameterizedType pt = ParameterizedType.class.cast(javaType); final Type rawType = pt.getRawType(); if (Class.class.isInstance(rawType) && Collection.class.isAssignableFrom(Class.class.cast(rawType)) && pt.getActualTypeArguments().length == 1 && Class.class.isInstance(pt.getActualTypeArguments()[0])) { final Object instance = tryCreatingObjectInstance(pt.getActualTypeArguments()[0]); final Class<?> collectionType = Class.class.cast(rawType); if (Set.class == collectionType) { return new Instance(singleton(instance), true); } if (SortedSet.class == collectionType) { return new Instance(new TreeSet<>(singletonList(instance)), true); } if (List.class == collectionType || Collection.class == collectionType) { return new Instance(singletonList(instance), true); } return null; } } return null; }  Instance createDemoInstance(final Object rootInstance, final ParameterMeta param); String findDefault(final Object instance, final ParameterMeta param); }
DefaultValueInspector { public Instance createDemoInstance(final Object rootInstance, final ParameterMeta param) { if (rootInstance != null) { final Object field = findField(rootInstance, param); if (field != null) { return new Instance(field, false); } } final Type javaType = param.getJavaType(); if (Class.class.isInstance(javaType)) { return new Instance(tryCreatingObjectInstance(javaType), true); } else if (ParameterizedType.class.isInstance(javaType)) { final ParameterizedType pt = ParameterizedType.class.cast(javaType); final Type rawType = pt.getRawType(); if (Class.class.isInstance(rawType) && Collection.class.isAssignableFrom(Class.class.cast(rawType)) && pt.getActualTypeArguments().length == 1 && Class.class.isInstance(pt.getActualTypeArguments()[0])) { final Object instance = tryCreatingObjectInstance(pt.getActualTypeArguments()[0]); final Class<?> collectionType = Class.class.cast(rawType); if (Set.class == collectionType) { return new Instance(singleton(instance), true); } if (SortedSet.class == collectionType) { return new Instance(new TreeSet<>(singletonList(instance)), true); } if (List.class == collectionType || Collection.class == collectionType) { return new Instance(singletonList(instance), true); } return null; } } return null; }  Instance createDemoInstance(final Object rootInstance, final ParameterMeta param); String findDefault(final Object instance, final ParameterMeta param);  }