TemplateConfiguration.getXxx has thrown NPE instead of the more helpful IllegalStateException when the parent Configuration wasn't yet set.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/d661acc3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/d661acc3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/d661acc3 Branch: refs/heads/2.3 Commit: d661acc3797f9e4b70f9de194bcdbff71d89f0e9 Parents: e6ec6c4 Author: ddekany <[email protected]> Authored: Sun Feb 5 19:17:21 2017 +0100 Committer: ddekany <[email protected]> Committed: Sun Feb 5 19:17:21 2017 +0100 ---------------------------------------------------------------------- .../freemarker/core/TemplateConfiguration.java | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d661acc3/src/main/java/freemarker/core/TemplateConfiguration.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/TemplateConfiguration.java b/src/main/java/freemarker/core/TemplateConfiguration.java index fe1062a..804446b 100644 --- a/src/main/java/freemarker/core/TemplateConfiguration.java +++ b/src/main/java/freemarker/core/TemplateConfiguration.java @@ -49,7 +49,7 @@ import freemarker.template.utility.NullArgumentException; * <p> * Note that the result value of the reader methods (getter and "is" methods) is usually not useful unless the value of * that setting was already set on this object. Otherwise you will get the value from the parent {@link Configuration}, - * which is {@link Configuration#getDefaultConfiguration()} before this object is associated to a {@link Configuration}. + * or an {@link IllegalStateException} before this object is associated to a {@link Configuration}. * * <p> * If you are using this class for your own template loading and caching solution, rather than with the standard one, @@ -147,6 +147,11 @@ public final class TemplateConfiguration extends Configurable implements ParserC public Configuration getParentConfiguration() { return parentConfigurationSet ? (Configuration) getParent() : null; } + + private Configuration getNonNullParentConfiguration() { + checkParentConfigurationSet(); + return (Configuration) getParent(); + } /** * Set all settings in this {@link TemplateConfiguration} that were set in the parameter @@ -278,8 +283,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC * If the parent configuration wasn't yet set. */ public void apply(Template template) { - checkParentConfigurationSet(); - Configuration cfg = getParentConfiguration(); + Configuration cfg = getNonNullParentConfiguration(); if (template.getConfiguration() != cfg) { // This is actually not a problem right now, but for future BC we enforce this. throw new IllegalArgumentException( @@ -386,7 +390,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC * The getter pair of {@link #setTagSyntax(int)}. */ public int getTagSyntax() { - return tagSyntax != null ? tagSyntax.intValue() : getParentConfiguration().getTagSyntax(); + return tagSyntax != null ? tagSyntax.intValue() : getNonNullParentConfiguration().getTagSyntax(); } /** @@ -408,7 +412,8 @@ public final class TemplateConfiguration extends Configurable implements ParserC * The getter pair of {@link #setNamingConvention(int)}. */ public int getNamingConvention() { - return namingConvention != null ? namingConvention.intValue() : getParentConfiguration().getNamingConvention(); + return namingConvention != null ? namingConvention.intValue() + : getNonNullParentConfiguration().getNamingConvention(); } /** @@ -430,7 +435,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC */ public boolean getWhitespaceStripping() { return whitespaceStripping != null ? whitespaceStripping.booleanValue() - : getParentConfiguration().getWhitespaceStripping(); + : getNonNullParentConfiguration().getWhitespaceStripping(); } /** @@ -453,7 +458,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC */ public int getAutoEscapingPolicy() { return autoEscapingPolicy != null ? autoEscapingPolicy.intValue() - : getParentConfiguration().getAutoEscapingPolicy(); + : getNonNullParentConfiguration().getAutoEscapingPolicy(); } /** @@ -475,7 +480,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC * The getter pair of {@link #setOutputFormat(OutputFormat)}. */ public OutputFormat getOutputFormat() { - return outputFormat != null ? outputFormat : getParentConfiguration().getOutputFormat(); + return outputFormat != null ? outputFormat : getNonNullParentConfiguration().getOutputFormat(); } /** @@ -497,7 +502,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC */ public boolean getRecognizeStandardFileExtensions() { return recognizeStandardFileExtensions != null ? recognizeStandardFileExtensions.booleanValue() - : getParentConfiguration().getRecognizeStandardFileExtensions(); + : getNonNullParentConfiguration().getRecognizeStandardFileExtensions(); } /** @@ -519,7 +524,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC */ public boolean getStrictSyntaxMode() { return strictSyntaxMode != null ? strictSyntaxMode.booleanValue() - : getParentConfiguration().getStrictSyntaxMode(); + : getNonNullParentConfiguration().getStrictSyntaxMode(); } /** @@ -536,7 +541,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC } public String getEncoding() { - return encoding != null ? encoding : getParentConfiguration().getDefaultEncoding(); + return encoding != null ? encoding : getNonNullParentConfiguration().getDefaultEncoding(); } /** @@ -577,7 +582,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC */ public int getTabSize() { return tabSize != null ? tabSize.intValue() - : getParentConfiguration().getTabSize(); + : getNonNullParentConfiguration().getTabSize(); } /** @@ -597,8 +602,7 @@ public final class TemplateConfiguration extends Configurable implements ParserC * If the parent configuration wasn't yet set. */ public Version getIncompatibleImprovements() { - checkParentConfigurationSet(); - return getParentConfiguration().getIncompatibleImprovements(); + return getNonNullParentConfiguration().getIncompatibleImprovements(); } private void checkParentConfigurationSet() {
