This is an automated email from the ASF dual-hosted git repository. ddekany pushed a commit to branch FREEMARKER-35 in repository https://gitbox.apache.org/repos/asf/freemarker.git
The following commit(s) were added to refs/heads/FREEMARKER-35 by this push: new 744a5470 FREEMARKER-35: Some code cleanup 744a5470 is described below commit 744a5470d743a25616dc1da9087c6c62842a924f Author: ddekany <ddek...@apache.org> AuthorDate: Fri Jan 6 19:15:35 2023 +0100 FREEMARKER-35: Some code cleanup --- .../core/AliasTemplateDateFormatFactory.java | 4 +- src/main/java/freemarker/core/Configurable.java | 23 ++++----- src/main/java/freemarker/core/Environment.java | 4 +- src/main/java/freemarker/core/EvalUtil.java | 2 +- .../ISOLikeTemplateTemporalTemporalFormat.java | 2 - .../java/freemarker/core/_JavaTimeBugUtils.java | 2 +- src/main/java/freemarker/core/_MessageUtil.java | 13 ++--- src/main/java/freemarker/core/_TemporalUtils.java | 58 ++++++++++------------ 8 files changed, 51 insertions(+), 57 deletions(-) diff --git a/src/main/java/freemarker/core/AliasTemplateDateFormatFactory.java b/src/main/java/freemarker/core/AliasTemplateDateFormatFactory.java index 4bb788ff..04724045 100644 --- a/src/main/java/freemarker/core/AliasTemplateDateFormatFactory.java +++ b/src/main/java/freemarker/core/AliasTemplateDateFormatFactory.java @@ -27,11 +27,11 @@ import freemarker.template.utility.StringUtil; /** * Creates an alias to another format that's given with a {@link String}, so that the format can be referred with a - * simple name in the template, rather than with a concrete pattern or other kind of format string. Internally, this + * simple name in the template, rather than with a concrete pattern, or other kind of format string. Internally, this * will call {@link Environment#getTemplateDateFormat(String, int, Locale, TimeZone, boolean)} to resolve the other * format. * - * @see Configuration#customDateFormats + * @see Configuration#setCustomDateFormats(Map) * * @since 2.3.24 */ diff --git a/src/main/java/freemarker/core/Configurable.java b/src/main/java/freemarker/core/Configurable.java index 59402304..9952f9ab 100644 --- a/src/main/java/freemarker/core/Configurable.java +++ b/src/main/java/freemarker/core/Configurable.java @@ -1307,10 +1307,10 @@ public class Configurable { * them with {@code _}, like {@code "short_medium"}. ({@code "medium"} means * {@code "medium_medium"} for date-time values.) * The value you set here is possibly automatically adjusted for specific temporal types, in two scenarios: - * Secanrio 1. is when formatting {@link LocalDateTime} or {@link LocalTime} would fail because the format - * contains a time zone field (for most locales "long" anb "full" are like that). To handle this, FreeMarker + * Scenario 1 is when formatting {@link LocalDateTime}, or {@link LocalTime} would fail because the format + * contains a time zone field (for most locales "long" and "full" are like that). To handle this, FreeMarker * will decrease the verbosity if the time part (like "full" is replaced with "long", "long" is replaced with - * "medium", etc.), until successful formatting will be possible. Scenarion 2 is when formatting + * "medium", etc.), until successful formatting will be possible. Scenario 2 is when formatting * {@link OffsetTime} values, and the current time zone has DST, in which case FreeMarker will increase the * verbosity until the offset is displayed (see at {@link #setTimeFormat} why). * @@ -1323,15 +1323,15 @@ public class Configurable { * * </ul> * - * <p>Defaults to {@code ""}, which is equivalent to {@code "medium_medium"}. + * <p>Defaults to {@code ""}, which is equivalent to {@code "medium"}. * * <p>If temporal support is enabled (see {@link Configuration#setIncompatibleImprovements(Version)} at 2.3.33, and * {@link DefaultObjectWrapperBuilder#setTemporalSupport(boolean)}) this is also used for these {@link Temporal} * classes: {@link Instant}, {@link LocalDateTime}, {@link OffsetDateTime}, {@link ZonedDateTime}. * For non-{@code Local} {@link Temporal}-s FreeMarker will detect if the format doesn't show the offset or zone (as * is typically the case for the {@code "medium"} format), and then before formatting it will convert the value to - * the time zone specified in the {@link #setTimeZone(TimeZone) timeZone} setting of FreeMarker, or when parsing - * a string it will assume that it uses that time zone. + * the time zone specified in the {@link #setTimeZone(TimeZone) timeZone} setting of FreeMarker. Also, when parsing + * a string that contains no offset or zone, it will assume that it uses that time zone. */ public void setDateTimeFormat(String dateTimeFormat) { NullArgumentException.check("dateTimeFormat", dateTimeFormat); @@ -1362,9 +1362,8 @@ public class Configurable { * <p>Defaults to {@code "iso"}, which will simply show the year like {@code "2021"} (without the quotation marks). * * @param yearFormat - * See the similar parameter of {@link #setDateTimeFormat(String)}, - * {@code iso}/{@code xs} only the year is shown. - * Java (as of version 8) doesn't support "styles" (like "short", "medium", etc.) for this. + * See the similar parameter of {@link #setDateTimeFormat(String)}, except for {@code iso}/{@code xs} only the + * year is shown, and "styles" (like "short", "medium", etc.) aren't supported. * * @since 2.3.33 */ @@ -1396,9 +1395,9 @@ public class Configurable { * <p>Defaults to {@code "iso"}, which will show the value like {@code "2021-12"} (without the quotation marks). * * @param yearMonthFormat - * See the similar parameter of {@link #setDateTimeFormat(String)}; - * {@code iso}/{@code xs} will look like {@code 2021-12}. - * Java (as of version 8) doesn't support "styles" (like "short", "medium", etc.) for this. + * See the similar parameter of {@link #setDateTimeFormat(String)}, + * except {@code iso}/{@code xs} will look like {@code 2021-12}, and "styles" (like "short", "medium", etc.) + * aren't supported. * * @since 2.3.33 */ diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java index 81450357..4fef2c70 100644 --- a/src/main/java/freemarker/core/Environment.java +++ b/src/main/java/freemarker/core/Environment.java @@ -2686,7 +2686,7 @@ public final class Environment extends Configurable { /** * Returns the current format for the given temporal class. * - * @since 2.3.31 + * @since 2.3.33 */ public TemplateTemporalFormat getTemplateTemporalFormat(Class<? extends Temporal> temporalClass) throws TemplateValueFormatException { @@ -3173,7 +3173,7 @@ public final class Environment extends Configurable { * should only by used with * {@link DateUtil#dateToISO8601String(Date, boolean, boolean, boolean, int, TimeZone, * DateToISO8601CalendarFactory)} and - * {@link DateUtil#dateToXSString(Date, boolean, boolean, boolean, int, TimeZone, DateToISO8601CalendarFactory)} . + * {@link DateUtil#dateToXSString(Date, boolean, boolean, boolean, int, TimeZone, DateToISO8601CalendarFactory)}. */ DateToISO8601CalendarFactory getISOBuiltInCalendarFactory() { if (isoBuiltInCalendarFactory == null) { diff --git a/src/main/java/freemarker/core/EvalUtil.java b/src/main/java/freemarker/core/EvalUtil.java index 08b0382f..3a313650 100644 --- a/src/main/java/freemarker/core/EvalUtil.java +++ b/src/main/java/freemarker/core/EvalUtil.java @@ -96,7 +96,7 @@ class EvalUtil { static Temporal modelToTemporal(TemplateTemporalModel model, Expression expr) throws TemplateModelException { Temporal value = model.getAsTemporal(); - if (value == null) throw newModelHasStoredNullException(Date.class, model, expr); + if (value == null) throw newModelHasStoredNullException(Temporal.class, model, expr); return value; } diff --git a/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java b/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java index 6f1e1d27..036995c7 100644 --- a/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java +++ b/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java @@ -42,8 +42,6 @@ import freemarker.template.TemplateTemporalModel; /** * See {@link ISOTemplateTemporalFormatFactory}, and {@link XSTemplateTemporalFormatFactory}. - * - * @since 2.3.32 */ final class ISOLikeTemplateTemporalTemporalFormat extends DateTimeFormatterBasedTemplateTemporalFormat { private final DateTimeFormatter dateTimeFormatter; diff --git a/src/main/java/freemarker/core/_JavaTimeBugUtils.java b/src/main/java/freemarker/core/_JavaTimeBugUtils.java index 0a746637..9efc05a2 100644 --- a/src/main/java/freemarker/core/_JavaTimeBugUtils.java +++ b/src/main/java/freemarker/core/_JavaTimeBugUtils.java @@ -30,7 +30,7 @@ import freemarker.log.Logger; /** * Used internally only, might change without notice! - * To work around with java.time bugs. + * To work around java.time bugs. */ public final class _JavaTimeBugUtils { private static final Logger LOG = Logger.getLogger("freemarker.runtime"); diff --git a/src/main/java/freemarker/core/_MessageUtil.java b/src/main/java/freemarker/core/_MessageUtil.java index 8b8ac432..843ea27a 100644 --- a/src/main/java/freemarker/core/_MessageUtil.java +++ b/src/main/java/freemarker/core/_MessageUtil.java @@ -55,7 +55,7 @@ public class _MessageUtil { }; static final String FAIL_MISSING_TIME_ZONE_PARSER_POLICY_ERROR_DETAIL - = "The parsed string doesn't contain time zone, nor offset, and that target type is non-local, and the " + = "The parsed string doesn't contain time zone, nor offset, and the target type is non-local, and the " + "specified policy is to fail in that case (see " + MissingTimeZoneParserPolicy.class.getName() + "." + MissingTimeZoneParserPolicy.FAIL + ")."; @@ -335,7 +335,7 @@ public class _MessageUtil { TemplateTemporalFormat format, TemplateTemporalModel ttm, Expression dataSrcExp, Exception formattingException, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( - "Failed to format temporal value of class ", safeGetTemporalClass(ttm), + "Failed to format temporal value of class ", safeGetTemporalClassName(ttm), ", value ", new _DelayedJQuote(new _DelayedToString(safeGetTemporalValue(ttm))), ", with format ", new _DelayedJQuote(format.getDescription()), ": ", formattingException instanceof TemplateValueFormatException @@ -347,10 +347,11 @@ public class _MessageUtil { : new _MiscTemplateException(formattingException, null, desc); } - private static String safeGetTemporalClass(TemplateTemporalModel ttm) { + private static String safeGetTemporalClassName(TemplateTemporalModel ttm) { try { - return ttm.getAsTemporal().getClass().getName(); - } catch (TemplateModelException e) { + Temporal value = ttm.getAsTemporal(); + return value != null ? value.getClass().getName() : "Null"; + } catch (Exception e) { return "[failed to get]"; } } @@ -359,7 +360,7 @@ public class _MessageUtil { try { Temporal value = ttm.getAsTemporal(); return value != null ? value : "null"; - } catch (TemplateModelException e) { + } catch (Exception e) { return "[failed to get]"; } } diff --git a/src/main/java/freemarker/core/_TemporalUtils.java b/src/main/java/freemarker/core/_TemporalUtils.java index 7cfdbbd0..cb879fa8 100644 --- a/src/main/java/freemarker/core/_TemporalUtils.java +++ b/src/main/java/freemarker/core/_TemporalUtils.java @@ -412,8 +412,7 @@ public final class _TemporalUtils { chronologyLocale = locale; } } - Chronology chronology = Chronology.ofLocale(chronologyLocale); - return chronology; + return Chronology.ofLocale(chronologyLocale); } private static String legacyCalendarTypeToJavaTimeApiCompatibleName(String legacyType) { @@ -438,28 +437,28 @@ public final class _TemporalUtils { public static Class<? extends Temporal> normalizeSupportedTemporalClass(Class<? extends Temporal> temporalClass) { if (SUPPORTED_TEMPORAL_CLASSES_ARE_FINAL) { return temporalClass; + } + + if (Instant.class.isAssignableFrom(temporalClass)) { + return Instant.class; + } else if (LocalDate.class.isAssignableFrom(temporalClass)) { + return LocalDate.class; + } else if (LocalDateTime.class.isAssignableFrom(temporalClass)) { + return LocalDateTime.class; + } else if (LocalTime.class.isAssignableFrom(temporalClass)) { + return LocalTime.class; + } else if (OffsetDateTime.class.isAssignableFrom(temporalClass)) { + return OffsetDateTime.class; + } else if (OffsetTime.class.isAssignableFrom(temporalClass)) { + return OffsetTime.class; + } else if (ZonedDateTime.class.isAssignableFrom(temporalClass)) { + return ZonedDateTime.class; + } else if (YearMonth.class.isAssignableFrom(temporalClass)) { + return YearMonth.class; + } else if (Year.class.isAssignableFrom(temporalClass)) { + return Year.class; } else { - if (Instant.class.isAssignableFrom(temporalClass)) { - return Instant.class; - } else if (LocalDate.class.isAssignableFrom(temporalClass)) { - return LocalDate.class; - } else if (LocalDateTime.class.isAssignableFrom(temporalClass)) { - return LocalDateTime.class; - } else if (LocalTime.class.isAssignableFrom(temporalClass)) { - return LocalTime.class; - } else if (OffsetDateTime.class.isAssignableFrom(temporalClass)) { - return OffsetDateTime.class; - } else if (OffsetTime.class.isAssignableFrom(temporalClass)) { - return OffsetTime.class; - } else if (ZonedDateTime.class.isAssignableFrom(temporalClass)) { - return ZonedDateTime.class; - } else if (YearMonth.class.isAssignableFrom(temporalClass)) { - return YearMonth.class; - } else if (Year.class.isAssignableFrom(temporalClass)) { - return Year.class; - } else { - throw new IllegalArgumentException("Unsupported temporal class: " + temporalClass.getName()); - } + throw new IllegalArgumentException("Unsupported temporal class: " + temporalClass.getName()); } } @@ -470,18 +469,15 @@ public final class _TemporalUtils { */ public static boolean isLocalTemporalClass(Class<? extends Temporal> temporalClass) { temporalClass = normalizeSupportedTemporalClass(temporalClass); - if (temporalClass == Instant.class - || temporalClass == OffsetDateTime.class - || temporalClass == ZonedDateTime.class - || temporalClass == OffsetTime.class) { - return false; - } - return true; + return temporalClass != Instant.class + && temporalClass != OffsetDateTime.class + && temporalClass != ZonedDateTime.class + && temporalClass != OffsetTime.class; } /** * Returns the local variation of a non-local class, or {@code null} if no local pair is known, or the class is not - * recognized . + * recognized. * * @throws IllegalArgumentException If the temporal class is not currently supported by FreeMarker. */