3
3
import static com .googlecode .jmapper .api .JMapperAPI .global ;
4
4
import static com .googlecode .jmapper .api .JMapperAPI .mappedClass ;
5
5
6
- import java .awt .geom .Arc2D .Float ;
7
- import java .lang .reflect .InvocationTargetException ;
8
- import java .util .Collection ;
9
6
import java .util .HashMap ;
10
7
import java .util .Map ;
11
8
import java .util .function .BiConsumer ;
15
12
import com .googlecode .jmapper .exceptions .JMapperException ;
16
13
17
14
import info .unterrainer .commons .serialization .objectmapper .exceptions .ObjectMapperMappingException ;
18
- import info .unterrainer .commons .serialization .objectmapper .exceptions .ObjectMapperProcessingException ;
19
15
import info .unterrainer .commons .serialization .objectmapper .key .MappingKey ;
20
16
import lombok .NonNull ;
21
17
@@ -37,14 +33,17 @@ public <A, B> void registerMapping(@NonNull final Class<A> sourceType, @NonNull
37
33
mappings .put (new MappingKey <>(targetType , sourceType ), back );
38
34
}
39
35
40
- public <S , T > T map (@ NonNull final Class <T > targetType ,
41
- @ NonNull final S source ) {
36
+ public <S , T > T map (@ NonNull final Class <T > targetType , @ NonNull final S source ) {
42
37
return map (targetType , source , null );
43
38
}
44
39
45
40
@ SuppressWarnings ("unchecked" )
46
- public <S , T > T map (@ NonNull final Class <T > targetType , @ NonNull final S source ,
47
- final T target ) {
41
+ public <S , T > T map (@ NonNull final S source , @ NonNull final T target ) {
42
+ return map ((Class <T >) target .getClass (), source , target );
43
+ }
44
+
45
+ @ SuppressWarnings ("unchecked" )
46
+ public <S , T > T map (@ NonNull final Class <T > targetType , @ NonNull final S source , final T target ) {
48
47
final Class <S > sourceType = (Class <S >) source .getClass ();
49
48
T result = mapWithJMapper (sourceType , targetType , source , target );
50
49
return mapWithBiConsumer (sourceType , targetType , source , result );
@@ -60,23 +59,8 @@ private <T, S> T mapWithBiConsumer(@NonNull final Class<S> sourceType, @NonNull
60
59
return target ;
61
60
}
62
61
63
- private <T > T constructObjectWithObjectType (final Class <T > targetType ) {
64
- try {
65
- return targetType .getConstructor ().newInstance ();
66
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
67
- | NoSuchMethodException | SecurityException e ) {
68
- throw new ObjectMapperProcessingException ("Error while instantiating target object" , e );
69
- }
70
- }
71
-
72
62
private <T , S > T mapWithJMapper (@ NonNull final Class <S > sourceType , @ NonNull final Class <T > targetType ,
73
63
@ NonNull final S source , final T target ) {
74
- if (IsPrimitveIncludingWrapper (sourceType ) || IsPrimitveIncludingWrapper (targetType )
75
- || IsPrimitiveCollectionOrArrayIncludingWrapper (sourceType )
76
- || IsPrimitiveCollectionOrArrayIncludingWrapper (targetType )) {
77
- return constructObjectWithObjectType (targetType );
78
- }
79
-
80
64
@ SuppressWarnings ("unchecked" )
81
65
JMapper <T , S > jMapper = (JMapper <T , S >) mappers .get (new MappingKey <>(sourceType , targetType ));
82
66
if (jMapper == null ) {
@@ -97,14 +81,4 @@ private <T, S> T mapWithJMapper(@NonNull final Class<S> sourceType, @NonNull fin
97
81
}
98
82
99
83
}
100
-
101
- private boolean IsPrimitveIncludingWrapper (final Class <?> t ) {
102
- return t .isPrimitive () || t == String .class || t == Short .class || t == Integer .class || t == Long .class
103
- || t == Float .class || t == Double .class || t == Character .class || t == Boolean .class ;
104
- }
105
-
106
- private boolean IsPrimitiveCollectionOrArrayIncludingWrapper (final Class <?> t ) {
107
- return t .isArray () && IsPrimitveIncludingWrapper (t .getComponentType ()) || Collection .class .isAssignableFrom (t )
108
- || Map .class .isAssignableFrom (t );
109
- }
110
84
}
0 commit comments