Skip to content

Commit

Permalink
remove expicit modelmapper exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
eye2web committed May 25, 2020
1 parent abc2cfb commit 195154a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 68 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# model-mapper
Easy to use annotation based model mapper with advanced capabilities.

## Highlights 2.2.1
- ModelMapper now throws implicit exceptions

## Highlights 2.2.0
- Now supports custom mapping of each element in a Iterable field.
- Each ValueMapper class will now receive the ModelMapper Object to support mapping of nested Objects.
Expand All @@ -20,15 +23,15 @@ This library is available on mvn central.

Gradle
```groovy
implementation 'com.github.eye2web:model-mapper:2.2.0'
implementation 'com.github.eye2web:model-mapper:2.2.1'
```

Maven
```xml
<dependency>
<groupId>com.github.eye2web</groupId>
<artifactId>model-mapper</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ext {
lombokVersion = '1.18.10'
groupName = 'com.github.eye2web'
artifactIdentifier = 'model-mapper'
projectVersion = '2.2.0'
projectVersion = '2.2.1'
}

group groupName
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/eye2web/modelmapper/ModelMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eye2web.modelmapper;

import eye2web.modelmapper.exception.NoArgsConstructorException;
import eye2web.modelmapper.exception.ModelMapperException;
import eye2web.modelmapper.model.Options;
import eye2web.modelmapper.strategy.FieldStrategy;
import eye2web.modelmapper.strategy.MethodStrategy;
Expand All @@ -14,7 +14,6 @@
import lombok.NonNull;
import lombok.Setter;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@AllArgsConstructor
Expand Down Expand Up @@ -43,8 +42,7 @@ public ModelMapper(final List<? extends ValueMapper> valueMappers,
}

@Override
public <D> D map(final Object source, final Class<D> destinationType)
throws Exception {
public <D> D map(final Object source, final Class<D> destinationType) {

final D destinationObj = destinationType.cast(createEmptyInstanceOfNoArgsClass(destinationType));

Expand All @@ -54,8 +52,7 @@ public <D> D map(final Object source, final Class<D> destinationType)
}

@Override
public <D> void map(final Object source, final D destinationObj)
throws Exception {
public <D> void map(final Object source, final D destinationObj) {
getCurrentStrategy().mapObjects(source, destinationObj, this);
}

Expand Down Expand Up @@ -88,13 +85,12 @@ private Strategy getCurrentStrategy() {
return strategy;
}

private Object createEmptyInstanceOfNoArgsClass(final Class<?> classType)
throws NoArgsConstructorException, InstantiationException, IllegalAccessException, InvocationTargetException {
private Object createEmptyInstanceOfNoArgsClass(final Class<?> classType) {
try {
return classType.getConstructor()
.newInstance();
} catch (NoSuchMethodException ex) {
throw new NoArgsConstructorException(classType.getName());
} catch (final Exception ex) {
throw new ModelMapperException(ex);
}
}
}
14 changes: 7 additions & 7 deletions src/main/java/eye2web/modelmapper/ModelMapperI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ public interface ModelMapperI {

/**
* Map object to new instance of destination type
*
* @param <D> ClassType definition
* @param source Source where data should be copied from
* @param destinationType Type of class used to map source data to.
* @return returns a instance of destinationType including the copied data from source.
* @throws Exception mapping error.
* @throws eye2web.modelmapper.exception.ModelMapperException .
*/
<D> D map(final Object source, final Class<D> destinationType)
throws Exception;
<D> D map(final Object source, final Class<D> destinationType);

/**
* Map object to existing instance
* @param <D> ClassType definition
*
* @param <D> ClassType definition
* @param source Source where data should be copied from
* @param destinationObj Destination object.
* @throws Exception mapping error.
* @throws eye2web.modelmapper.exception.ModelMapperException .
*/
<D> void map(final Object source, final D destinationObj)
throws Exception;
<D> void map(final Object source, final D destinationObj);


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eye2web.modelmapper.exception;

public class ModelMapperException extends RuntimeException {

public ModelMapperException(final Exception ex) {
super(ex);
}

}

This file was deleted.

68 changes: 37 additions & 31 deletions src/main/java/eye2web/modelmapper/strategy/FieldStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eye2web.modelmapper.ModelMapperI;
import eye2web.modelmapper.annotations.MapValue;
import eye2web.modelmapper.annotations.MapValues;
import eye2web.modelmapper.exception.ModelMapperException;
import eye2web.modelmapper.model.FromField;

import java.lang.reflect.Field;
Expand All @@ -26,7 +27,7 @@ public static Strategy getInstance() {
}

@Override
public void mapObjects(final Object source, final Object destinationObj, final ModelMapperI modelMapper) throws Exception {
public void mapObjects(final Object source, final Object destinationObj, final ModelMapperI modelMapper) {
final List<Map.Entry<String, Object>> sourceFieldValues = StrategyUtil.getFieldValues(source);

for (final Field field : destinationObj.getClass().getDeclaredFields()) {
Expand All @@ -40,7 +41,7 @@ public void mapObjects(final Object source, final Object destinationObj, final M
}

private void tryMapMultiValueField(final Field field, final List<Map.Entry<String, Object>> sourceFieldValues,
final Object destinationObj) throws Exception {
final Object destinationObj) {

final String[] multiValueFieldNames = StrategyUtil.getFieldNames(field);

Expand Down Expand Up @@ -71,7 +72,7 @@ private void tryMapMultiValueField(final Field field, final List<Map.Entry<Strin

private void tryMapSingleValueField(final Field field, final List<Map.Entry<String, Object>> sourceFieldValues,
final Object destinationObj,
ModelMapperI modelMapper) throws Exception {
ModelMapperI modelMapper) {

final String fieldName = StrategyUtil.getFieldName(field);

Expand All @@ -87,8 +88,7 @@ private void tryMapSingleValueField(final Field field, final List<Map.Entry<Stri

private void mapFieldValue(final Object destinationObj, final Field field, final String fieldName,
final Object fieldValue,
final ModelMapperI modelMapper)
throws Exception {
final ModelMapperI modelMapper) {

final Object value;

Expand All @@ -98,45 +98,51 @@ private void mapFieldValue(final Object destinationObj, final Field field, final
return;
}

final var objectValueMapper = StrategyUtil.getSingleValueMapper(field);
try {
final var objectValueMapper = StrategyUtil.getSingleValueMapper(field);

if (Objects.isNull(mapValue)) {
value = fieldValue;
} else if (mapValue.iterate()) {
value = StrategyUtil.iterateElements(fieldValue, fieldName, objectValueMapper, modelMapper);
if (Objects.isNull(mapValue)) {
value = fieldValue;
} else if (mapValue.iterate()) {
value = StrategyUtil.iterateElements(fieldValue, fieldName, objectValueMapper, modelMapper);

} else {
final var mapFromField = FromField.builder()
.fieldName(fieldName)
.fieldValue(fieldValue)
.build();

value = objectValueMapper.mapToValue(mapFromField, modelMapper);
}
} else {
final var mapFromField = FromField.builder()
.fieldName(fieldName)
.fieldValue(fieldValue)
.build();

value = objectValueMapper.mapToValue(mapFromField, modelMapper);
}

boolean isPrivate = StrategyUtil.setFieldPublic(field, destinationObj);
boolean isPrivate = StrategyUtil.setFieldPublic(field, destinationObj);

field.set(destinationObj, value);
field.set(destinationObj, value);

if (isPrivate) {
field.setAccessible(false);
if (isPrivate) {
field.setAccessible(false);
}
} catch (final Exception ex) {
throw new ModelMapperException(ex);
}
}

private void mapFieldValues(final Object destinationObj, final Field field, final Set<FromField> fromFieldSet)
throws Exception {
private void mapFieldValues(final Object destinationObj, final Field field, final Set<FromField> fromFieldSet) {

final var value = field.isAnnotationPresent(MapValues.class) ?
StrategyUtil.getMultiValueMapper(field).mapToValue(fromFieldSet) :
null;
try {
final var value = field.isAnnotationPresent(MapValues.class) ?
StrategyUtil.getMultiValueMapper(field).mapToValue(fromFieldSet) :
null;

boolean isPrivate = StrategyUtil.setFieldPublic(field, destinationObj);
boolean isPrivate = StrategyUtil.setFieldPublic(field, destinationObj);

field.set(destinationObj, value);
field.set(destinationObj, value);

if (isPrivate) {
field.setAccessible(false);
if (isPrivate) {
field.setAccessible(false);
}
} catch (final Exception ex) {
throw new ModelMapperException(ex);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/eye2web/modelmapper/strategy/Strategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public interface Strategy {


void mapObjects(final Object source, final Object destinationObj, final ModelMapperI modelMapper) throws Exception;
void mapObjects(final Object source, final Object destinationObj, final ModelMapperI modelMapper);

}
12 changes: 9 additions & 3 deletions src/main/java/eye2web/modelmapper/strategy/StrategyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eye2web.modelmapper.ModelMapperI;
import eye2web.modelmapper.annotations.MapValue;
import eye2web.modelmapper.annotations.MapValues;
import eye2web.modelmapper.exception.ModelMapperException;
import eye2web.modelmapper.model.FieldProperties;
import eye2web.modelmapper.model.FromField;
import eye2web.modelmapper.value.map.*;
Expand All @@ -23,7 +24,7 @@ public static String[] getFieldNames(final Field field) {
return Objects.nonNull(mapValues) ? mapValues.fieldNames() : null;
}

public static List<Map.Entry<String, Object>> getFieldValues(final Object obj) throws IllegalAccessException {
public static List<Map.Entry<String, Object>> getFieldValues(final Object obj) {

final List<Map.Entry<String, Object>> fieldValues = new ArrayList<>();

Expand All @@ -32,9 +33,14 @@ public static List<Map.Entry<String, Object>> getFieldValues(final Object obj) t
boolean isPrivate = setFieldPublic(field, obj);

final String name = getFieldName(field);
final Object value = field.get(obj);

fieldValues.add(new AbstractMap.SimpleEntry<>(name, value));
try {
final Object value = field.get(obj);
fieldValues.add(new AbstractMap.SimpleEntry<>(name, value));
} catch (final IllegalAccessException ex) {
throw new ModelMapperException(ex);
}

if (isPrivate) {
field.setAccessible(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ModelMapperSpringTest {


@Test
public void shouldMapArticleIdToArticleObject() throws Exception {
public void shouldMapArticleIdToArticleObject() {

final var articleGroupRequest = ArticleGroupRequest.builder()
.groupId(1)
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/eye2web/modelmapper/ModelMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void setup() {
}

@Test
public void shouldFullyMapModelAResponseToRequest() throws Exception {
public void shouldFullyMapModelAResponseToRequest() {

final var age = Period.between(LocalDate.of(1990, 3, 14), LocalDate.now()).getYears();

Expand Down Expand Up @@ -61,7 +61,7 @@ public void shouldFullyMapModelAResponseToRequest() throws Exception {
}

@Test
public void shouldNotMapEmptyFirstName() throws Exception {
public void shouldNotMapEmptyFirstName() {

final ModelA modelA = ModelA.builder()
.lastName("van der Heijden")
Expand All @@ -75,7 +75,7 @@ public void shouldNotMapEmptyFirstName() throws Exception {
}

@Test
public void shouldMapToExistingTarget() throws Exception {
public void shouldMapToExistingTarget() {
final ModelA modelA = ModelA.builder()
.id(1)
.firstName("Remco")
Expand All @@ -98,7 +98,7 @@ public void shouldMapToExistingTarget() throws Exception {
}

@Test
public void shouldMapToExistingTargetIgnoringNulls() throws Exception {
public void shouldMapToExistingTargetIgnoringNulls() {
final ModelA modelA = ModelA.builder()
.id(1)
.firstName(null)
Expand Down

0 comments on commit 195154a

Please sign in to comment.