Skip to content

Commit

Permalink
Fix #502
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 12, 2018
1 parent dc5c82e commit 157f2c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ JSON library.
`writeRaw(..)`) more efficiently
#495: Create `StreamReadFeature` to move non-json specific `JsonParser.Feature`s to
#496: Create `StreamWriteFeature` to take over non-json-specific `JsonGenerator.Feature`s
#502: Make `DefaultPrettyPrinter.createInstance()` to fail for sub-classes

2.9.8 (not yet released)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
}

/**
* @since 2.6.0
* @since 2.6
*/
public DefaultPrettyPrinter withRootSeparator(String rootSeparator) {
return withRootSeparator((rootSeparator == null) ? null : new SerializedString(rootSeparator));
Expand Down Expand Up @@ -252,6 +252,10 @@ public DefaultPrettyPrinter withSeparators(Separators separators) {

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

Expand Down
7 changes: 6 additions & 1 deletion src/test/java/com/fasterxml/jackson/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,12 @@ protected JsonFactory sharedStreamFactory() {
protected JsonFactory newStreamFactory() {
return new JsonFactory();
}


// @since 2.9.8
protected JsonFactoryBuilder streamFactoryBuilder() {
return (JsonFactoryBuilder) JsonFactory.builder();
}

protected String fieldNameFor(int index)
{
StringBuilder sb = new StringBuilder(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,19 @@ private String _printTestData(PrettyPrinter pp, boolean useBytes) throws IOExcep
}
return sw.toString();
}

// [core#502]: Force sub-classes to reimplement `createInstance`
public void testInvalidSubClass() throws Exception
{
DefaultPrettyPrinter pp = new MyPrettyPrinter();
try {
pp.createInstance();
fail("Should not pass");
} catch (IllegalStateException e) {
verifyException(e, "does not override");
}
}

@SuppressWarnings("serial")
static class MyPrettyPrinter extends DefaultPrettyPrinter { }
}

0 comments on commit 157f2c4

Please sign in to comment.