@Test public void testThatWeCannotCreateInstancesFromClassesWithoutAnEmptyConstructor() { FakeContext context = new FakeContext() .setParameter("name", "wisdom") .setParameter("id", "0") .setParameter("time", "1000") .setParameter("flavors", ImmutableList.of("a", "b")); assertThat(parser.invoke(context, Stuff.class)).isNull(); }
@Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; } }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  @Override T invoke(Context context, Class<T> classOfT, Type genericType); @Override T invoke(Context context, Class<T> classOfT); @Override T invoke(byte[] bytes, Class<T> classOfT); List<String> getContentTypes(); }
BodyParserForm implements BodyParser { @Override public <T> T invoke(Context context, Class<T> classOfT, Type genericType) { T t; try { t = classOfT.newInstance(); } catch (Exception e) { LOGGER.error("Failed to create a new instance of {}", classOfT, e); return null; } Map<String, ReflectionHelper.Property> properties = ReflectionHelper.getProperties(classOfT, genericType); for (Entry<String, List<String>> ent : context.parameters().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } final Map<String, String> fromPath = context.route().getPathParametersEncoded(context.request().uri()); for (Entry<String, String> ent : fromPath .entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValue(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.debug(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.form() == null || context.form().isEmpty()) { return t; } for (Entry<String, List<String>> ent : context.form().entrySet()) { try { ReflectionHelper.Property property = properties.get(ent.getKey()); if (property != null) { Object value = converters.convertValues(ent.getValue(), property.getClassOfProperty(), property.getGenericTypeOfProperty(), null); property.set(t, value); } } catch (Exception e) { LOGGER.warn(ERROR_KEY + ent.getKey() + ERROR_AND + ent.getValue(), e); } } if (context.files() == null || context.files().isEmpty()) { return t; } for (FileItem item : context.files()) { try { ReflectionHelper.Property property = properties.get(item.field()); if (property != null) { if (InputStream.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item.stream()); } else if (FileItem.class.isAssignableFrom(property.getClassOfProperty())) { property.set(t, item); } else if (property.getClassOfProperty().isArray() && property.getClassOfProperty().getComponentType().equals(Byte.TYPE)) { property.set(t, item.bytes()); } } } catch (Exception e) { LOGGER.warn(ERROR_KEY + item.field() + ERROR_AND + item, e); } } return t; }  @Override T invoke(Context context, Class<T> classOfT, Type genericType); @Override T invoke(Context context, Class<T> classOfT); @Override T invoke(byte[] bytes, Class<T> classOfT); List<String> getContentTypes();  }