Skip to content

Commit 157f2c4

Browse files
committed
Fix #502
1 parent dc5c82e commit 157f2c4

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ JSON library.
2525
`writeRaw(..)`) more efficiently
2626
#495: Create `StreamReadFeature` to move non-json specific `JsonParser.Feature`s to
2727
#496: Create `StreamWriteFeature` to take over non-json-specific `JsonGenerator.Feature`s
28+
#502: Make `DefaultPrettyPrinter.createInstance()` to fail for sub-classes
2829

2930
2.9.8 (not yet released)
3031

src/main/java/com/fasterxml/jackson/core/util/DefaultPrettyPrinter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
157157
}
158158

159159
/**
160-
* @since 2.6.0
160+
* @since 2.6
161161
*/
162162
public DefaultPrettyPrinter withRootSeparator(String rootSeparator) {
163163
return withRootSeparator((rootSeparator == null) ? null : new SerializedString(rootSeparator));
@@ -252,6 +252,10 @@ public DefaultPrettyPrinter withSeparators(Separators separators) {
252252

253253
@Override
254254
public DefaultPrettyPrinter createInstance() {
255+
if (getClass() != DefaultPrettyPrinter.class) { // since 2.10
256+
throw new IllegalStateException("Failed `createInstance()`: "+getClass().getName()
257+
+" does not override method; it has to");
258+
}
255259
return new DefaultPrettyPrinter(this);
256260
}
257261

src/test/java/com/fasterxml/jackson/core/BaseTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,12 @@ protected JsonFactory sharedStreamFactory() {
566566
protected JsonFactory newStreamFactory() {
567567
return new JsonFactory();
568568
}
569-
569+
570+
// @since 2.9.8
571+
protected JsonFactoryBuilder streamFactoryBuilder() {
572+
return (JsonFactoryBuilder) JsonFactory.builder();
573+
}
574+
570575
protected String fieldNameFor(int index)
571576
{
572577
StringBuilder sb = new StringBuilder(16);

src/test/java/com/fasterxml/jackson/core/util/TestDefaultPrettyPrinter.java

+15
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,19 @@ private String _printTestData(PrettyPrinter pp, boolean useBytes) throws IOExcep
159159
}
160160
return sw.toString();
161161
}
162+
163+
// [core#502]: Force sub-classes to reimplement `createInstance`
164+
public void testInvalidSubClass() throws Exception
165+
{
166+
DefaultPrettyPrinter pp = new MyPrettyPrinter();
167+
try {
168+
pp.createInstance();
169+
fail("Should not pass");
170+
} catch (IllegalStateException e) {
171+
verifyException(e, "does not override");
172+
}
173+
}
174+
175+
@SuppressWarnings("serial")
176+
static class MyPrettyPrinter extends DefaultPrettyPrinter { }
162177
}

0 commit comments

Comments
 (0)