GenericNullHandler implements NullHandler { public <T> T instantiate(Class<T> baseType) { if (baseType.isArray()) { return baseType.cast(Array.newInstance(baseType.getComponentType(), 0)); } Class<?> typeToInstantiate = baseType; if (baseType.isInterface() || Modifier.isAbstract(baseType.getModifiers())) { if (!CONCRETE_TYPES.containsKey(baseType)) { throw new IllegalArgumentException("Vraptor does not support this interface or abstract type: " + typeToInstantiate.getName()); } typeToInstantiate = CONCRETE_TYPES.get(baseType); } Object instance = new Mirror().on(typeToInstantiate).invoke().constructor().withoutArgs(); if(Collection.class.isAssignableFrom(typeToInstantiate)) { removal.add((Collection)instance); } return baseType.cast(instance); } GenericNullHandler(EmptyElementsRemoval removal); T instantiate(Class<T> baseType);  }
@Test(expected = IllegalArgumentException.class) public void shouldDenyMostInterfaces() throws Exception { handler.instantiate(TheInterface.class); }
@Test(expected = IllegalArgumentException.class) public void shouldDenyMostAbstractClasses() throws Exception { handler.instantiate(AbstractClass.class); }
@Test public void shouldCreateArrays() throws Exception { Long[] array = handler.instantiate(Long[].class); assertThat(array, is(notNullValue())); }
@Test public void shouldInstantiateGregorianCalendarForAbstractCalendarType() throws Exception { Calendar calendar = handler.instantiate(Calendar.class); assertThat(calendar, is(notNullValue())); assertThat(calendar, is(instanceOf(GregorianCalendar.class))); }
@Test public void shouldInstantiateArrayListForCollectionInterface() throws Exception { Collection<?> collection = handler.instantiate(Collection.class); assertThat(collection, is(notNullValue())); assertThat(collection, is(instanceOf(ArrayList.class))); verify(removal, times(1)).add(new ArrayList<Object>()); }
@Test public void shouldInstantiateArrayListForListInterface() throws Exception { List<?> list = handler.instantiate(List.class); assertThat(list, is(notNullValue())); assertThat(list, is(instanceOf(ArrayList.class))); verify(removal, times(1)).add(new ArrayList<Object>()); }
@Test public void shouldInstantiateLinkedListForQueueInterface() throws Exception { Queue<?> queue = handler.instantiate(Queue.class); assertThat(queue, is(notNullValue())); assertThat(queue, is(instanceOf(LinkedList.class))); verify(removal, times(1)).add(new LinkedList<Object>()); }
@Test public void shouldInstantiateHashSetListForSetInterface() throws Exception { Set<?> set = handler.instantiate(Set.class); assertThat(set, is(notNullValue())); assertThat(set, is(instanceOf(HashSet.class))); verify(removal, times(1)).add(new HashSet<Object>()); }
@Test public void shouldInstantiateTreeSetListForSortedSetInterface() throws Exception { Set<?> set = handler.instantiate(SortedSet.class); assertThat(set, is(notNullValue())); assertThat(set, is(instanceOf(TreeSet.class))); verify(removal, times(1)).add(new TreeSet<Object>()); }