From cb60647c2cebf283e75e571c3b33a5bd733c080b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 15 Aug 2017 19:56:06 +0200 Subject: [PATCH] Prevented possible NullPointerException's --- src/main/java/de/juplo/jpa/converters/DurationConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/InstantConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/LocalDateConverter.java | 4 ++++ .../java/de/juplo/jpa/converters/LocalDateTimeConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/LocaleConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/MonthDayConverter.java | 4 ++++ .../java/de/juplo/jpa/converters/OffsetDateTimeConverter.java | 4 ++++ .../java/de/juplo/jpa/converters/OffsetTimeConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/PeriodConverter.java | 4 ++++ .../juplo/jpa/converters/TemporalAmountToDaysConverter.java | 4 ++++ .../juplo/jpa/converters/TemporalAmountToHoursConverter.java | 4 ++++ .../juplo/jpa/converters/TemporalAmountToMicrosConverter.java | 4 ++++ .../juplo/jpa/converters/TemporalAmountToMillisConverter.java | 4 ++++ .../jpa/converters/TemporalAmountToMinutesConverter.java | 4 ++++ .../juplo/jpa/converters/TemporalAmountToNanosConverter.java | 4 ++++ .../jpa/converters/TemporalAmountToSecondsConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/URIConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/URLConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/YearConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/YearMonthConverter.java | 4 ++++ src/main/java/de/juplo/jpa/converters/ZoneIdConverter.java | 4 ++++ .../java/de/juplo/jpa/converters/ZoneOffsetConverter.java | 4 ++++ .../java/de/juplo/jpa/converters/ZonedDateTimeConverter.java | 4 ++++ 24 files changed, 96 insertions(+) diff --git a/src/main/java/de/juplo/jpa/converters/DurationConverter.java b/src/main/java/de/juplo/jpa/converters/DurationConverter.java index 402dcf3..0304ad4 100644 --- a/src/main/java/de/juplo/jpa/converters/DurationConverter.java +++ b/src/main/java/de/juplo/jpa/converters/DurationConverter.java @@ -15,12 +15,16 @@ public class DurationConverter implements AttributeConverter @Override public String convertToDatabaseColumn(Duration duration) { + if (duration == null) + return null; return duration.toString(); } @Override public Duration convertToEntityAttribute(String string) { + if (string == null) + return null; return Duration.parse(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/InstantConverter.java b/src/main/java/de/juplo/jpa/converters/InstantConverter.java index 28cf149..b9c54ec 100644 --- a/src/main/java/de/juplo/jpa/converters/InstantConverter.java +++ b/src/main/java/de/juplo/jpa/converters/InstantConverter.java @@ -16,12 +16,16 @@ public class InstantConverter implements AttributeConverter @Override public Timestamp convertToDatabaseColumn(Instant instant) { + if (instant == null) + return null; return Timestamp.from(instant); } @Override public Instant convertToEntityAttribute(Timestamp ts) { + if (ts == null) + return null; return ts.toInstant(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java b/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java index 2606602..7db0626 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java @@ -16,12 +16,16 @@ public class LocalDateConverter implements AttributeConverter @Override public Date convertToDatabaseColumn(LocalDate ld) { + if (ld == null) + return null; return Date.valueOf(ld); } @Override public LocalDate convertToEntityAttribute(Date date) { + if (date == null) + return null; return date.toLocalDate(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java index 24bf45b..b4773a4 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java @@ -16,12 +16,16 @@ public class LocalDateTimeConverter implements AttributeConverter @Override public Time convertToDatabaseColumn(LocalTime lt) { + if (lt == null) + return null; return Time.valueOf(lt); } @Override public LocalTime convertToEntityAttribute(Time time) { + if (time == null) + return null; return time.toLocalTime(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocaleConverter.java b/src/main/java/de/juplo/jpa/converters/LocaleConverter.java index da84d15..0174e99 100644 --- a/src/main/java/de/juplo/jpa/converters/LocaleConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocaleConverter.java @@ -16,12 +16,16 @@ public class LocaleConverter implements AttributeConverter @Override public String convertToDatabaseColumn(Locale locale) { + if (locale == null) + return null; return locale.toLanguageTag(); } @Override public Locale convertToEntityAttribute(String string) { + if (string == null) + return null; return Locale.forLanguageTag(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java index 143ced1..9063c33 100644 --- a/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java +++ b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java @@ -15,12 +15,16 @@ public class MonthDayConverter implements AttributeConverter @Override public String convertToDatabaseColumn(MonthDay monthday) { + if (monthday == null) + return null; return monthday.toString(); } @Override public MonthDay convertToEntityAttribute(String string) { + if (string == null) + return null; return MonthDay.parse(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java index 8815cf0..3228aca 100644 --- a/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java @@ -17,12 +17,16 @@ public class OffsetDateTimeConverter implements AttributeConverter @Override public Time convertToDatabaseColumn(OffsetTime ot) { + if (ot == null) + return null; ZoneOffset offset = ot.getOffset(); return Time.valueOf(ot.withOffsetSameInstant(ZoneOffset.UTC).toLocalTime()); } @@ -24,6 +26,8 @@ public class OffsetTimeConverter implements AttributeConverter @Override public OffsetTime convertToEntityAttribute(Time time) { + if (time == null) + return null; return OffsetTime.of(time.toLocalTime(), ZoneOffset.UTC); } } diff --git a/src/main/java/de/juplo/jpa/converters/PeriodConverter.java b/src/main/java/de/juplo/jpa/converters/PeriodConverter.java index b241a56..a760b64 100644 --- a/src/main/java/de/juplo/jpa/converters/PeriodConverter.java +++ b/src/main/java/de/juplo/jpa/converters/PeriodConverter.java @@ -15,12 +15,16 @@ public class PeriodConverter implements AttributeConverter @Override public String convertToDatabaseColumn(Period period) { + if (period == null) + return null; return period.toString(); } @Override public Period convertToEntityAttribute(String string) { + if (string == null) + return null; return Period.parse(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java index f4ca829..fe4635c 100644 --- a/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java @@ -17,12 +17,16 @@ public class TemporalAmountToDaysConverter implements AttributeConverter @Override public String convertToDatabaseColumn(URI uri) { + if (uri == null) + return null; return uri.toASCIIString(); } @Override public URI convertToEntityAttribute(String string) { + if (string == null) + return null; return URI.create(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/URLConverter.java b/src/main/java/de/juplo/jpa/converters/URLConverter.java index 9fe0c0e..b2fe2b1 100644 --- a/src/main/java/de/juplo/jpa/converters/URLConverter.java +++ b/src/main/java/de/juplo/jpa/converters/URLConverter.java @@ -27,6 +27,8 @@ public class URLConverter implements AttributeConverter @Override public String convertToDatabaseColumn(URL url) { + if (url == null) + return null; try { return url.toURI().toASCIIString(); @@ -41,6 +43,8 @@ public class URLConverter implements AttributeConverter @Override public URL convertToEntityAttribute(String string) { + if (string == null) + return null; try { return URI.create(string).toURL(); diff --git a/src/main/java/de/juplo/jpa/converters/YearConverter.java b/src/main/java/de/juplo/jpa/converters/YearConverter.java index 59875a6..6e22267 100644 --- a/src/main/java/de/juplo/jpa/converters/YearConverter.java +++ b/src/main/java/de/juplo/jpa/converters/YearConverter.java @@ -15,12 +15,16 @@ public class YearConverter implements AttributeConverter @Override public Integer convertToDatabaseColumn(Year year) { + if (year == null) + return null; return year.getValue(); } @Override public Year convertToEntityAttribute(Integer integer) { + if (integer == null) + return null; return Year.of(integer); } } diff --git a/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java index 47718d9..f84b676 100644 --- a/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java +++ b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java @@ -15,12 +15,16 @@ public class YearMonthConverter implements AttributeConverter @Override public String convertToDatabaseColumn(YearMonth yearmonth) { + if (yearmonth == null) + return null; return yearmonth.toString(); } @Override public YearMonth convertToEntityAttribute(String string) { + if (string == null) + return null; return YearMonth.parse(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/ZoneIdConverter.java b/src/main/java/de/juplo/jpa/converters/ZoneIdConverter.java index 57f3ba1..6266ad0 100644 --- a/src/main/java/de/juplo/jpa/converters/ZoneIdConverter.java +++ b/src/main/java/de/juplo/jpa/converters/ZoneIdConverter.java @@ -15,12 +15,16 @@ public class ZoneIdConverter implements AttributeConverter @Override public String convertToDatabaseColumn(ZoneId zone) { + if (zone == null) + return null; return zone.getId(); } @Override public ZoneId convertToEntityAttribute(String string) { + if (string == null) + return null; return ZoneId.of(string); } } diff --git a/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java index f948b54..eca057f 100644 --- a/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java +++ b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java @@ -15,12 +15,16 @@ public class ZoneOffsetConverter implements AttributeConverter