FieldMappingExtractorImpl implements FieldMappingExtractor { @Override public MappedField extract(Field field) { final Mapped mapped = field.getAnnotation(Mapped.class); final Id id = field.getAnnotation(Id.class); final Collection collection = field.getAnnotation(Collection.class); final Property property = field.getAnnotation(Property.class); final boolean isMappedField = (mapped != null) || (id != null) || (collection != null) || (property != null); if (!isMappedField) { LOGGER.trace("Field {} is not annotated, ignoring in mapping.", field.getName()); return null; } LOGGER.trace("Found annotated field {}", field.getName()); final Mapping mapping = extractMapping(field); final Class<?> fieldType = field.getType(); final Class<?> implementationType = mapping.getImplementationType(); final boolean validImplementationType = String.class.equals(implementationType) || EntityUtils.isEntity(implementationType) || ClassUtils.isPrimitiveOrAutoboxed(implementationType); if (!validImplementationType) { throw new MappingException("Target type " + fieldType + " is not an entity."); } final MappedField mappedField = new ImmutableMappedField(field.getName(), fieldType, mapping, getMissingStrategy(mapped), getMissingException(mapped)); return mappedField; }  @Override MappedField extract(Field field); Mapping extractMapping(Field field);  }
@Test public void testExtractIdProperty() throws Exception { final MappedField mappedField = extractor.extract(EntityWithPrimitiveProperties.class.getDeclaredField("id")); assertThat(mappedField, notNullValue()); assertThat(mappedField.getName(), is("id")); assertThat(mappedField.getMapping(), instanceOf(IdMapping.class)); }
@Test public void testExtractReferenceFieldWithCustomLookup() throws Exception { final MappedField mappedField = extractor.extract(EntityWithReferenceProperties.class.getDeclaredField("customLookupMode")); assertThat(mappedField.getName(), is("customLookupMode")); assertThat(mappedField.getMapping(), instanceOf(ReferenceMapping.class)); final ReferenceMapping mapping = (ReferenceMapping) mappedField.getMapping(); assertThat(mapping.getPath(), is("foobar")); assertThat(mapping.getLookupMode(), is(LookupMode.Direct)); }
@Test public void testExtractFieldWithReference() throws Exception { final MappedField mappedField = extractor.extract(EntityWithReferenceProperties.class.getDeclaredField("entityWithPrimitiveProperties")); assertThat(mappedField.getName(), is("entityWithPrimitiveProperties")); assertThat(mappedField.getMapping(), instanceOf(ReferenceMapping.class)); final ReferenceMapping mapping = (ReferenceMapping) mappedField.getMapping(); assertThat(mapping.getPath(), is("entityWithPrimitiveProperties")); }
@Test public void testExtractFieldWithReferenceAndCustomName() throws Exception { final MappedField mappedField = extractor.extract(EntityWithReferenceProperties.class.getDeclaredField("customNamedReference")); assertThat(mappedField.getName(), is("customNamedReference")); assertThat(mappedField.getMapping(), instanceOf(ReferenceMapping.class)); final ReferenceMapping mapping = (ReferenceMapping) mappedField.getMapping(); assertThat(mapping.getPath(), is("foo/bar")); }