diff --git a/pom.xml b/pom.xml index 48d183a..9cd342e 100755 --- a/pom.xml +++ b/pom.xml @@ -136,6 +136,19 @@ ${java.version} UTF-8 + + + test-compile + process-test-sources + + testCompile + + + 1.8 + 1.8 + + + org.apache.maven.plugins diff --git a/src/main/java/br/com/six2six/fixturefactory/transformer/CalendarTransformer.java b/src/main/java/br/com/six2six/fixturefactory/transformer/CalendarTransformer.java index d51355f..afedaa3 100644 --- a/src/main/java/br/com/six2six/fixturefactory/transformer/CalendarTransformer.java +++ b/src/main/java/br/com/six2six/fixturefactory/transformer/CalendarTransformer.java @@ -44,8 +44,14 @@ public T transform(Object value, Class type) { return type.cast(returnValue); } - public boolean accepts(Object value, Class type) { - boolean instanceOfCalendar = value instanceof Calendar; - return JavaVersion.current().gte(JavaVersion.JAVA_8) ? instanceOfCalendar && !java.time.temporal.Temporal.class.isAssignableFrom(type) : instanceOfCalendar; - } + @SuppressWarnings("unchecked") + public boolean accepts(Object value, Class type) { + boolean instanceOfCalendar = value instanceof Calendar; + + try { + return JavaVersion.current().gte(JavaVersion.JAVA_8) ? instanceOfCalendar && !ClassUtils.getClass("java.time.temporal.Temporal").isAssignableFrom(type) : instanceOfCalendar; + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e.getMessage(),e); + } + } } diff --git a/src/main/java/br/com/six2six/fixturefactory/transformer/DateTimeTransformer.java b/src/main/java/br/com/six2six/fixturefactory/transformer/DateTimeTransformer.java index f38e113..80c02d9 100644 --- a/src/main/java/br/com/six2six/fixturefactory/transformer/DateTimeTransformer.java +++ b/src/main/java/br/com/six2six/fixturefactory/transformer/DateTimeTransformer.java @@ -1,51 +1,74 @@ package br.com.six2six.fixturefactory.transformer; -import static br.com.six2six.fixturefactory.util.DateTimeUtils.toZonedDateTime; +import static org.apache.commons.lang.ArrayUtils.EMPTY_OBJECT_ARRAY; +import static org.apache.commons.lang.reflect.MethodUtils.invokeMethod; +import java.lang.reflect.InvocationTargetException; import java.util.Calendar; import org.apache.commons.lang.ClassUtils; +import br.com.six2six.fixturefactory.JavaVersion; +import br.com.six2six.fixturefactory.util.DateTimeUtils; + public class DateTimeTransformer implements Transformer { @Override public T transform(Object value, Class type) { Object returnValue = null; - if (value == null) { + if (value == null || !JavaVersion.current().gte(JavaVersion.JAVA_8)) { return null; } - if (ClassUtils.isAssignable(type, java.time.LocalDateTime.class)) { - returnValue = toZonedDateTime((Calendar) value).toLocalDateTime(); - - } else if (ClassUtils.isAssignable(type, java.time.LocalDate.class)) { - returnValue = toZonedDateTime((Calendar) value).toLocalDate(); - - } else if (ClassUtils.isAssignable(type, java.time.LocalTime.class)) { - returnValue = toZonedDateTime((Calendar) value).toLocalTime(); - - } else if (ClassUtils.isAssignable(type, java.time.OffsetDateTime.class)) { - returnValue = toZonedDateTime((Calendar) value).toOffsetDateTime(); - - } else if (ClassUtils.isAssignable(type, java.time.ZonedDateTime.class)) { - returnValue = toZonedDateTime((Calendar) value); + try { + Object zonedDateTime = DateTimeUtils.toZonedDateTime((Calendar) value); + if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalDateTime"))) { + returnValue = invokeMethod(zonedDateTime, "toLocalDateTime", EMPTY_OBJECT_ARRAY); + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalDate"))) { + returnValue = invokeMethod(zonedDateTime, "toLocalDate", EMPTY_OBJECT_ARRAY); + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalTime"))) { + returnValue = invokeMethod(zonedDateTime, "toLocalTime", EMPTY_OBJECT_ARRAY); + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.OffsetDateTime"))) { + returnValue = invokeMethod(zonedDateTime, "toOffsetDateTime", EMPTY_OBJECT_ARRAY); + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.ZonedDateTime"))) { + returnValue = zonedDateTime; + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.OffsetTime"))) { + returnValue = invokeMethod(zonedDateTime, "toOffsetDateTime", EMPTY_OBJECT_ARRAY); + returnValue = invokeMethod(returnValue, "toOffsetTime", EMPTY_OBJECT_ARRAY); + + } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.Instant"))) { + returnValue = invokeMethod(zonedDateTime, "toInstant", EMPTY_OBJECT_ARRAY); + + } else { + throw new IllegalArgumentException("Incorrect type for transformer: " + type.getCanonicalName()); + } - } else if (ClassUtils.isAssignable(type, java.time.OffsetTime.class)) { - returnValue = toZonedDateTime((Calendar) value).toOffsetDateTime().toOffsetTime(); - - } else if (ClassUtils.isAssignable(type, java.time.Instant.class)) { - returnValue = ((Calendar) value).toInstant(); - - } else { - throw new IllegalArgumentException("Incorrect type for transformer: " + type.getCanonicalName()); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); + } catch (InvocationTargetException e) { + throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); } return type.cast(returnValue); } - @Override + @SuppressWarnings("unchecked") + @Override public boolean accepts(Object value, Class type) { - return value instanceof Calendar && java.time.temporal.Temporal.class.isAssignableFrom(type); + try { + return value instanceof Calendar && ClassUtils.getClass("java.time.temporal.Temporal").isAssignableFrom(type); + } catch (ClassNotFoundException e) { + return false; + } } } diff --git a/src/main/java/br/com/six2six/fixturefactory/util/DateTimeUtils.java b/src/main/java/br/com/six2six/fixturefactory/util/DateTimeUtils.java index 32446e4..709c0a1 100644 --- a/src/main/java/br/com/six2six/fixturefactory/util/DateTimeUtils.java +++ b/src/main/java/br/com/six2six/fixturefactory/util/DateTimeUtils.java @@ -1,9 +1,13 @@ package br.com.six2six.fixturefactory.util; +import java.lang.reflect.InvocationTargetException; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.reflect.MethodUtils; + public class DateTimeUtils { public static Calendar toCalendar(String source, DateFormat format) { @@ -16,8 +20,12 @@ public static Calendar toCalendar(String source, DateFormat format) { return date; } - public static java.time.ZonedDateTime toZonedDateTime(Calendar value) { - return value.toInstant().atZone(value.getTimeZone().toZoneId()); + public static Object toZonedDateTime(Calendar value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Object timeZone = MethodUtils.invokeMethod(value, "getTimeZone", ArrayUtils.EMPTY_OBJECT_ARRAY); + Object zoneId = MethodUtils.invokeMethod(timeZone, "toZoneId", ArrayUtils.EMPTY_OBJECT_ARRAY); + Object instant = MethodUtils.invokeMethod(value, "toInstant", ArrayUtils.EMPTY_OBJECT_ARRAY); + Object zone = MethodUtils.invokeMethod(instant, "atZone", new Object[]{zoneId}); + return zone; } } diff --git a/src/test/java/br/com/six2six/fixturefactory/model/Order.java b/src/test/java/br/com/six2six/fixturefactory/model/Order.java index 3bb03d5..c8952b7 100644 --- a/src/test/java/br/com/six2six/fixturefactory/model/Order.java +++ b/src/test/java/br/com/six2six/fixturefactory/model/Order.java @@ -59,4 +59,4 @@ public void setSendDate(LocalDate sendDate) { this.sendDate = sendDate; } -} \ No newline at end of file +}