@Test public void testTemplateWithConstant() throws Exception { final ObjectMapper mapper = new ObjectMapper(); final Set<Class<?>> classes = Sets.<Class<?>>newHashSet(ClassWithConstant.class); final Map<String, Object> template = JSONDocTemplateBuilder.build(ClassWithConstant.class, classes); Assert.assertEquals("", template.get("identifier")); Assert.assertEquals(null, template.get(THIS_IS_A_CONSTANT)); final String serializedTemplate = "{" + "\"identifier\":\"\"" + "}"; assertThat(mapper.writeValueAsString(template), is(serializedTemplate)); }
public static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects) { final JSONDocTemplate jsonDocTemplate = new JSONDocTemplate(); if(jsondocObjects.contains(clazz)) { try { Set<JSONDocFieldWrapper> fields = getAllDeclaredFields(clazz); for (JSONDocFieldWrapper jsondocFieldWrapper : fields) { Field field = jsondocFieldWrapper.getField(); if (isFinal(field.getModifiers()) && isStatic(field.getModifiers())) { continue; } String fieldName = field.getName(); ApiObjectField apiObjectField = field.getAnnotation(ApiObjectField.class); if (apiObjectField != null && !apiObjectField.name().isEmpty()) { fieldName = apiObjectField.name(); } Object value; if (field.getType().equals(clazz) || (apiObjectField != null && !apiObjectField.processtemplate())) { value = getValue(Object.class, field.getGenericType(), fieldName, jsondocObjects); } else { value = getValue(field.getType(), field.getGenericType(), fieldName, jsondocObjects); } jsonDocTemplate.put(fieldName, value); } } catch (Exception e) { log.error("Error in JSONDocTemplate creation for class [" + clazz.getCanonicalName() + "]", e); } } return jsonDocTemplate; }
JSONDocTemplateBuilder { public static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects) { final JSONDocTemplate jsonDocTemplate = new JSONDocTemplate(); if(jsondocObjects.contains(clazz)) { try { Set<JSONDocFieldWrapper> fields = getAllDeclaredFields(clazz); for (JSONDocFieldWrapper jsondocFieldWrapper : fields) { Field field = jsondocFieldWrapper.getField(); if (isFinal(field.getModifiers()) && isStatic(field.getModifiers())) { continue; } String fieldName = field.getName(); ApiObjectField apiObjectField = field.getAnnotation(ApiObjectField.class); if (apiObjectField != null && !apiObjectField.name().isEmpty()) { fieldName = apiObjectField.name(); } Object value; if (field.getType().equals(clazz) || (apiObjectField != null && !apiObjectField.processtemplate())) { value = getValue(Object.class, field.getGenericType(), fieldName, jsondocObjects); } else { value = getValue(field.getType(), field.getGenericType(), fieldName, jsondocObjects); } jsonDocTemplate.put(fieldName, value); } } catch (Exception e) { log.error("Error in JSONDocTemplate creation for class [" + clazz.getCanonicalName() + "]", e); } } return jsonDocTemplate; } }
JSONDocTemplateBuilder { public static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects) { final JSONDocTemplate jsonDocTemplate = new JSONDocTemplate(); if(jsondocObjects.contains(clazz)) { try { Set<JSONDocFieldWrapper> fields = getAllDeclaredFields(clazz); for (JSONDocFieldWrapper jsondocFieldWrapper : fields) { Field field = jsondocFieldWrapper.getField(); if (isFinal(field.getModifiers()) && isStatic(field.getModifiers())) { continue; } String fieldName = field.getName(); ApiObjectField apiObjectField = field.getAnnotation(ApiObjectField.class); if (apiObjectField != null && !apiObjectField.name().isEmpty()) { fieldName = apiObjectField.name(); } Object value; if (field.getType().equals(clazz) || (apiObjectField != null && !apiObjectField.processtemplate())) { value = getValue(Object.class, field.getGenericType(), fieldName, jsondocObjects); } else { value = getValue(field.getType(), field.getGenericType(), fieldName, jsondocObjects); } jsonDocTemplate.put(fieldName, value); } } catch (Exception e) { log.error("Error in JSONDocTemplate creation for class [" + clazz.getCanonicalName() + "]", e); } } return jsonDocTemplate; }  }
JSONDocTemplateBuilder { public static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects) { final JSONDocTemplate jsonDocTemplate = new JSONDocTemplate(); if(jsondocObjects.contains(clazz)) { try { Set<JSONDocFieldWrapper> fields = getAllDeclaredFields(clazz); for (JSONDocFieldWrapper jsondocFieldWrapper : fields) { Field field = jsondocFieldWrapper.getField(); if (isFinal(field.getModifiers()) && isStatic(field.getModifiers())) { continue; } String fieldName = field.getName(); ApiObjectField apiObjectField = field.getAnnotation(ApiObjectField.class); if (apiObjectField != null && !apiObjectField.name().isEmpty()) { fieldName = apiObjectField.name(); } Object value; if (field.getType().equals(clazz) || (apiObjectField != null && !apiObjectField.processtemplate())) { value = getValue(Object.class, field.getGenericType(), fieldName, jsondocObjects); } else { value = getValue(field.getType(), field.getGenericType(), fieldName, jsondocObjects); } jsonDocTemplate.put(fieldName, value); } } catch (Exception e) { log.error("Error in JSONDocTemplate creation for class [" + clazz.getCanonicalName() + "]", e); } } return jsonDocTemplate; }  static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects); }
JSONDocTemplateBuilder { public static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects) { final JSONDocTemplate jsonDocTemplate = new JSONDocTemplate(); if(jsondocObjects.contains(clazz)) { try { Set<JSONDocFieldWrapper> fields = getAllDeclaredFields(clazz); for (JSONDocFieldWrapper jsondocFieldWrapper : fields) { Field field = jsondocFieldWrapper.getField(); if (isFinal(field.getModifiers()) && isStatic(field.getModifiers())) { continue; } String fieldName = field.getName(); ApiObjectField apiObjectField = field.getAnnotation(ApiObjectField.class); if (apiObjectField != null && !apiObjectField.name().isEmpty()) { fieldName = apiObjectField.name(); } Object value; if (field.getType().equals(clazz) || (apiObjectField != null && !apiObjectField.processtemplate())) { value = getValue(Object.class, field.getGenericType(), fieldName, jsondocObjects); } else { value = getValue(field.getType(), field.getGenericType(), fieldName, jsondocObjects); } jsonDocTemplate.put(fieldName, value); } } catch (Exception e) { log.error("Error in JSONDocTemplate creation for class [" + clazz.getCanonicalName() + "]", e); } } return jsonDocTemplate; }  static JSONDocTemplate build(Class<?> clazz, Set<Class<?>> jsondocObjects);  }