FieldProcessor extends AbstractListProcessor<Field, FieldDto> { @Override protected void process(AnnotatedElement element) { AccessibleObject ac = (AccessibleObject) element; Class<?> classType = MemberUtil.getCorrectType(ac); Class<?> genericType = MemberUtil.getGenericType(element); Class<?> declaringClass = MemberUtil.getDeclaringClass(ac); Class<?> valueType = null; if (null != classType) { if (Map.class.isAssignableFrom(classType)) { valueType = MemberUtil.getGenericType(element, 1); } String fieldName = MemberUtil.getFieldName(ac); FieldDto currentField = getFieldByName(declaringClass.getName(), fieldName); boolean isRelationship = ReflectionsUtil.hasAnnotationClassLoaderSafe( genericType, genericType, Entity.class); boolean isOwningSide = isRelationship && getMappedBy(ac) == null; boolean isCollection = Collection.class.isAssignableFrom(classType); Field annotation = getAnnotationClassLoaderSafe(ac, classType, Field.class); String relatedFieldName = getRelatedFieldName(ac, classType, genericType, declaringClass, isRelationship); java.lang.reflect.Field relatedField = (relatedFieldName != null) ? ReflectionUtils.findField(genericType, relatedFieldName) : null; boolean relatedFieldIsCollection = isRelatedFieldCollection(relatedField); boolean isTextArea = getAnnotationValue(annotation, TYPE, EMPTY).equalsIgnoreCase("text"); TypeDto type = getCorrectType(classType, isCollection, isRelationship, relatedFieldIsCollection); FieldBasicDto basic = new FieldBasicDto(); basic.setDisplayName(isUiChanged(currentField) ? currentField.getBasic().getDisplayName() : getAnnotationValue(annotation, DISPLAY_NAME, convertFromCamelCase(fieldName))); basic.setName(fieldName); basic.setDefaultValue(getDefaultValueForField(annotation, classType)); basic.setRequired(isFieldRequired(annotation, classType)); basic.setUnique(isFieldUnique(ac)); FieldDto field = new FieldDto(); if (null != annotation) { basic.setTooltip(isUiChanged(currentField) ? currentField.getBasic().getTooltip() : annotation.tooltip()); basic.setPlaceholder(annotation.placeholder()); String fn = getAnnotationValue(annotation, NAME, EMPTY); if (!fn.equals(EMPTY)) { field.addMetadata(new MetadataDto(DATABASE_COLUMN_NAME, fn)); } } field.setEntityId(entity.getId()); field.setType(type); field.setBasic(basic); field.setValidation(createValidation(ac, type)); field.setReadOnly(true); field.setNonEditable(false); field.setNonDisplayable(false); field.setUiChanged(isUiChanged(currentField)); field.setUiFilterable(isUiFilterable(currentField)); setFieldSettings(ac, classType, isRelationship, isTextArea, field); setFieldMetadata(classType, genericType, valueType, isCollection, isRelationship, relatedFieldIsCollection, isOwningSide, field, relatedFieldName); add(field); } else { LOGGER.warn("Field type is unknown in: {}", ac); } }  @Override Class<Field> getAnnotationType(); @Override Collection<FieldDto> getProcessingResult(); void setEntity(EntityDto entity); void setClazz(Class clazz);  }
@Test public void shouldProcessField() throws Exception { java.lang.reflect.Field world = getDeclaredField(Sample.class, "world", true); processor.process(world); verify(schemaHolder).getType(Boolean.class); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field = fields.iterator().next(); assertEquals(entity.getId(), field.getEntityId()); assertEquals("World", field.getBasic().getDisplayName()); assertEquals(world.getName(), field.getBasic().getName()); assertFalse(field.getBasic().isRequired()); assertEquals("", field.getBasic().getDefaultValue()); assertEquals("", field.getBasic().getTooltip()); assertEquals("", field.getBasic().getPlaceholder()); assertEquals(entity.getId(), field.getEntityId()); assertEquals(TypeDto.BOOLEAN, field.getType()); }
@Test public void shouldProcessSetter() throws Exception { Method setLocalTime = getAccessibleMethod(Sample.class, "setLocalTime", Time.class); processor.process(setLocalTime); verify(schemaHolder).getType(Time.class); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field = fields.iterator().next(); assertEquals("Local Time", field.getBasic().getDisplayName()); assertEquals("localTime", field.getBasic().getName()); assertTrue(field.getBasic().isRequired()); assertEquals("", field.getBasic().getDefaultValue()); assertEquals("", field.getBasic().getTooltip()); assertEquals("", field.getBasic().getPlaceholder()); assertEquals(entity.getId(), field.getEntityId()); assertEquals(TypeDto.TIME, field.getType()); }
@Test public void shouldProcessGetter() throws Exception { Method getServerDate = getAccessibleMethod(Sample.class, "getServerDate", new Class[0]); processor.process(getServerDate); verify(schemaHolder).getType(Date.class); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field = fields.iterator().next(); assertEquals("Server Date", field.getBasic().getDisplayName()); assertEquals("serverDate", field.getBasic().getName()); assertFalse(field.getBasic().isRequired()); assertEquals("", field.getBasic().getDefaultValue()); assertEquals("", field.getBasic().getTooltip()); assertEquals("yyyy-mm-dd", field.getBasic().getPlaceholder()); assertEquals(entity.getId(), field.getEntityId()); assertEquals(TypeDto.DATE, field.getType()); }
@Test public void shouldReadMaxLengthForStringField() { Method getLength400 = getAccessibleMethod(Sample.class, "getLength400", new Class[0]); processor.process(getLength400); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field = fields.iterator().next(); assertEquals("length400", field.getBasic().getName()); SettingDto lengthSetting = field.getSetting(Constants.Settings.STRING_MAX_LENGTH); assertNotNull(lengthSetting); assertEquals(400, lengthSetting.getValue()); }
@Test public void shouldRecognizeRelationshipTypes() throws NoSuchFieldException { processor.process(Sample.class.getDeclaredField("oneToOneUni")); processor.process(Sample.class.getDeclaredField("oneToOneBi")); processor.process(Sample.class.getDeclaredField("oneToManyUni")); processor.process(Sample.class.getDeclaredField("oneToManyBi")); processor.process(RelatedSample.class.getDeclaredField("oneToOneBi2")); processor.process(RelatedSample.class.getDeclaredField("manyToOneBi")); Collection<FieldDto> fields = processor.getElements(); assertEquals(6, fields.size()); assertRelationshipField(findFieldWithName(fields, "oneToOneUni"), RelatedSample.class, OneToOneRelationship.class, null); assertRelationshipField(findFieldWithName(fields, "oneToOneBi"), RelatedSample.class, OneToOneRelationship.class, "oneToOneBi2"); assertRelationshipField(findFieldWithName(fields, "oneToManyUni"), RelatedSample.class, OneToManyRelationship.class, null, new ExpectedCascadeSettings(true, false, true), Set.class); assertRelationshipField(findFieldWithName(fields, "oneToManyBi"), RelatedSample.class, OneToManyRelationship.class, "manyToOneBi", new ExpectedCascadeSettings(false, false, true), List.class); assertRelationshipField(findFieldWithName(fields, "oneToOneBi2"), Sample.class, OneToOneRelationship.class, "oneToOneBi"); assertRelationshipField(findFieldWithName(fields, "manyToOneBi"), Sample.class, ManyToOneRelationship.class, "oneToManyBi"); }
@Test public void shouldProcessComboboxFields() throws NoSuchFieldException { processor.process(Sample.class.getDeclaredField("enumSet")); processor.process(Sample.class.getDeclaredField("stringSet")); Collection<FieldDto> fields = processor.getElements(); assertEquals(2, fields.size()); FieldDto field= findFieldWithName(fields, "enumSet"); assertEquals(2, field.getMetadata().size()); assertEquals(Set.class.getName(), field.getMetadata(Constants.MetadataKeys.ENUM_COLLECTION_TYPE).getValue()); assertEquals("org.motechproject.mds.annotations.internal.samples.Sample$TestEnum", field.getMetadata(Constants.MetadataKeys.ENUM_CLASS_NAME).getValue()); assertEquals(3, field.getSettings().size()); assertTrue(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS))); assertFalse(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_USER_SUPPLIED))); assertEquals("{ONE=one, TWO=two, THREE=three}", field.getSettingsValueAsString(Constants.Settings.COMBOBOX_VALUES)); field= findFieldWithName(fields, "stringSet"); assertEquals(1, field.getMetadata().size()); assertEquals(Set.class.getName(), field.getMetadata(Constants.MetadataKeys.ENUM_COLLECTION_TYPE).getValue()); assertEquals(3, field.getSettings().size()); assertTrue(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS))); assertTrue(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_USER_SUPPLIED))); assertEquals("{}", field.getSettingsValueAsString(Constants.Settings.COMBOBOX_VALUES)); }
@Test public void shouldProcessComboboxFieldsForSingleEnumInstance() throws NoSuchFieldException { processor.process(Sample.class.getDeclaredField("singleEnum")); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field= findFieldWithName(fields, "singleEnum"); assertEquals(1, field.getMetadata().size()); assertEquals("org.motechproject.mds.annotations.internal.samples.Sample$TestEnum", field.getMetadata(Constants.MetadataKeys.ENUM_CLASS_NAME).getValue()); assertEquals(3, field.getSettings().size()); assertFalse(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS))); assertFalse(Boolean.parseBoolean(field.getSettingsValueAsString(Constants.Settings.ALLOW_USER_SUPPLIED))); assertEquals("{ONE=one, TWO=two, THREE=three}", field.getSettingsValueAsString(Constants.Settings.COMBOBOX_VALUES)); }
@Test public void shouldProcessPrimitiveFields() throws NoSuchFieldException { processor.process(Sample.class.getDeclaredField("primitiveBool")); processor.process(Sample.class.getDeclaredField("primitiveInt")); processor.process(Sample.class.getDeclaredField("primitiveDouble")); processor.process(Sample.class.getDeclaredField("primitiveLong")); Collection<FieldDto> fields = processor.getElements(); assertEquals(4, fields.size()); assertPrimitiveField(findFieldWithName(fields, "primitiveBool"), Boolean.class, "false"); assertPrimitiveField(findFieldWithName(fields, "primitiveInt"), Integer.class, "0"); assertPrimitiveField(findFieldWithName(fields, "primitiveDouble"), Double.class, "0.0"); assertPrimitiveField(findFieldWithName(fields, "primitiveLong"), Long.class, "0"); }
@Test public void shouldRecognizeUniqueFields() throws NoSuchFieldException { processor.process(Sample.class.getDeclaredField("unique")); Collection<FieldDto> fields = processor.getElements(); assertEquals(1, fields.size()); FieldDto field = fields.iterator().next(); assertNotNull(field); assertEquals("unique", field.getBasic().getName()); assertTrue(field.getBasic().isUnique()); }