Skip to content

Commit 62b58cb

Browse files
committed
Remove Jackson dependency
Issue #355
1 parent e3794d2 commit 62b58cb

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

easy-random-core/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@
5757
<groupId>io.github.classgraph</groupId>
5858
<artifactId>classgraph</artifactId>
5959
</dependency>
60-
<dependency>
61-
<groupId>com.fasterxml.jackson.core</groupId>
62-
<artifactId>jackson-databind</artifactId>
63-
</dependency>
6460
<dependency>
6561
<groupId>org.junit.jupiter</groupId>
6662
<artifactId>junit-jupiter</artifactId>

easy-random-core/src/main/java/org/jeasy/random/util/DateUtils.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
*/
2424
package org.jeasy.random.util;
2525

26+
import java.text.ParseException;
27+
import java.text.SimpleDateFormat;
2628
import java.time.ZonedDateTime;
2729
import java.util.Date;
30+
import java.util.logging.Logger;
2831

2932
import static java.util.Date.from;
3033

@@ -35,10 +38,13 @@
3538
*/
3639
public final class DateUtils {
3740

41+
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
42+
3843
private DateUtils() {
3944
}
4045

41-
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
46+
private static final Logger LOGGER = Logger.getLogger(DateUtils.class.getName());
47+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT);
4248

4349
/**
4450
* Convert a {@link ZonedDateTime} to {@link Date}.
@@ -50,4 +56,19 @@ public static Date toDate(ZonedDateTime zonedDateTime) {
5056
return from(zonedDateTime.toInstant());
5157
}
5258

59+
/**
60+
* Parse a string formatted in {@link DateUtils#DATE_FORMAT} to a {@link Date}.
61+
*
62+
* @param value date to parse. Should be in {@link DateUtils#DATE_FORMAT} format.
63+
* @return parsed date
64+
*/
65+
public static Date parse(String value) {
66+
try {
67+
return SIMPLE_DATE_FORMAT.parse(value);
68+
} catch (ParseException e) {
69+
LOGGER.severe("Unable to convert value " + value + " to a date using format " + DATE_FORMAT);
70+
return null;
71+
}
72+
}
73+
5374
}

easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package org.jeasy.random.util;
2525

26-
import com.fasterxml.jackson.databind.ObjectMapper;
2726
import org.jeasy.random.annotation.RandomizerArgument;
2827
import org.jeasy.random.ObjectCreationException;
2928
import org.jeasy.random.api.Randomizer;
@@ -33,13 +32,16 @@
3332
import java.beans.PropertyDescriptor;
3433
import java.lang.annotation.Annotation;
3534
import java.lang.reflect.*;
36-
import java.text.SimpleDateFormat;
35+
import java.math.BigDecimal;
36+
import java.math.BigInteger;
37+
import java.time.LocalDate;
38+
import java.time.LocalDateTime;
39+
import java.time.LocalTime;
3740
import java.util.*;
3841
import java.util.concurrent.*;
3942
import java.util.function.Supplier;
4043
import java.util.stream.Stream;
4144

42-
import static org.jeasy.random.util.DateUtils.DATE_FORMAT;
4345
import static java.lang.String.format;
4446
import static java.util.Arrays.asList;
4547
import static java.util.Locale.ENGLISH;
@@ -52,12 +54,6 @@
5254
*/
5355
public final class ReflectionUtils {
5456

55-
private static final ObjectMapper objectMapper = new ObjectMapper();
56-
57-
static {
58-
objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
59-
}
60-
6157
private ReflectionUtils() {
6258
}
6359

@@ -612,15 +608,45 @@ private static Object[] convertArguments(final RandomizerArgument[] declaredArgu
612608
Object[] arguments = new Object[numberOfArguments];
613609
for (int i = 0; i < numberOfArguments; i++) {
614610
Class<?> type = declaredArguments[i].type();
615-
String argument = declaredArguments[i].value();
616-
Object value = argument;
611+
String value = declaredArguments[i].value();
617612
// issue 299: if argument type is array, split values before conversion
618613
if (type.isArray()) {
619-
value = Stream.of(argument.split(",")).map(String::trim).toArray();
614+
Object[] values = Stream.of(value.split(",")).map(String::trim).toArray();
615+
arguments[i] = convertArray(values, type);
616+
} else {
617+
arguments[i] = convertValue(value, type);
620618
}
621-
arguments[i] = objectMapper.convertValue(value, type);
622619
}
623620
return arguments;
624621
}
625622

623+
private static Object convertValue(String value, Class<?> targetType) {
624+
if(Boolean.class.equals(targetType) || Boolean.TYPE.equals(targetType)) return Boolean.parseBoolean(value);
625+
if(Byte.class.equals(targetType) || Byte.TYPE.equals(targetType)) return Byte.parseByte(value);
626+
if(Short.class.equals(targetType) || Short.TYPE.equals(targetType)) return Short.parseShort(value);
627+
if(Integer.class.equals(targetType) || Integer.TYPE.equals(targetType)) return Integer.parseInt(value);
628+
if(Long.class.equals(targetType) || Long.TYPE.equals(targetType)) return Long.parseLong(value);
629+
if(Float.class.equals(targetType) || Float.TYPE.equals(targetType)) return Float.parseFloat(value);
630+
if(Double.class.equals(targetType) || Double.TYPE.equals(targetType)) return Double.parseDouble(value);
631+
if(BigInteger.class.equals(targetType)) return new BigInteger(value);
632+
if(BigDecimal.class.equals(targetType)) return new BigDecimal(value);
633+
if(Date.class.equals(targetType)) return DateUtils.parse(value);
634+
if(java.sql.Date.class.equals(targetType)) return java.sql.Date.valueOf(value);
635+
if(java.sql.Time.class.equals(targetType)) return java.sql.Time.valueOf(value);
636+
if(java.sql.Timestamp.class.equals(targetType)) return java.sql.Timestamp.valueOf(value);
637+
if(LocalDate.class.equals(targetType)) return LocalDate.parse(value);
638+
if(LocalTime.class.equals(targetType)) return LocalTime.parse(value);
639+
if(LocalDateTime.class.equals(targetType)) return LocalDateTime.parse(value);
640+
return value;
641+
}
642+
643+
private static Object convertArray(Object array, Class<?> targetType) {
644+
Object[] values = (Object[]) array;
645+
Object convertedValuesArray = Array.newInstance(targetType.getComponentType(), values.length);
646+
for (int i = 0; i < values.length; i++) {
647+
Array.set(convertedValuesArray, i, convertValue((String) values[i], targetType.getComponentType()));
648+
}
649+
return convertedValuesArray;
650+
}
651+
626652
}

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<classgraph.version>4.8.65</classgraph.version>
2929
<hibernate-validator.version>6.1.2.Final</hibernate-validator.version>
3030
<javax.el.version>3.0.0</javax.el.version>
31-
<jackson.version>2.10.3</jackson.version>
3231
<mockito.version>3.3.0</mockito.version>
3332
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
3433
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
@@ -105,11 +104,6 @@
105104
<artifactId>validation-api</artifactId>
106105
<version>${validation-api.version}</version>
107106
</dependency>
108-
<dependency>
109-
<groupId>com.fasterxml.jackson.core</groupId>
110-
<artifactId>jackson-databind</artifactId>
111-
<version>${jackson.version}</version>
112-
</dependency>
113107
<dependency>
114108
<groupId>org.objenesis</groupId>
115109
<artifactId>objenesis</artifactId>

0 commit comments

Comments
 (0)