From 6a125c3134604a172f7776166a9cdbf5e6ee6342 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 4 Dec 2023 17:52:36 -0800 Subject: [PATCH] Warnings cleanup (getCurrentName() vs currentName() etc) --- .../dataformat/xml/deser/FromXmlParser.java | 32 +++-- .../xml/ser/XmlSerializerProvider.java | 1 - .../jackson/dataformat/xml/XmlTestBase.java | 2 +- .../xml/lists/StringListRoundtripTest.java | 114 +++++++++--------- .../xml/stream/XmlParserNextXxxTest.java | 4 +- .../dataformat/xml/stream/XmlParserTest.java | 18 +-- .../xml/stream/dos/DeepNestingParserTest.java | 19 ++- 7 files changed, 101 insertions(+), 89 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java index 12a716ed8..cf9e4806c 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java @@ -473,7 +473,7 @@ public void addVirtualWrapping(Set namesToWrap0, boolean caseInsensitive * the current event. */ @Override - public String getCurrentName() throws IOException + public String currentName() throws IOException { // start markers require information from parent String name; @@ -490,6 +490,12 @@ public String getCurrentName() throws IOException return name; } + @Deprecated // since 2.17 + @Override + public String getCurrentName() throws IOException { + return currentName(); + } + @Override public void overrideCurrentName(String name) { @@ -500,7 +506,7 @@ public void overrideCurrentName(String name) } ctxt.setCurrentName(name); } - + @Override public void close() throws IOException { @@ -536,7 +542,7 @@ public XmlReadContext getParsingContext() { * that starts the current token. */ @Override - public JsonLocation getTokenLocation() { + public JsonLocation currentTokenLocation() { return _xmlTokens.getTokenLocation(); } @@ -545,10 +551,22 @@ public JsonLocation getTokenLocation() { * usually for error reporting purposes */ @Override - public JsonLocation getCurrentLocation() { + public JsonLocation currentLocation() { return _xmlTokens.getCurrentLocation(); } + @Deprecated // since 2.17 + @Override + public JsonLocation getCurrentLocation() { + return currentLocation(); + } + + @Deprecated // since 2.17 + @Override + public JsonLocation getTokenLocation() { + return currentTokenLocation(); + } + /** * Since xml representation can not really distinguish between array * and object starts (both are represented with elements), this method @@ -659,7 +677,7 @@ public JsonToken nextToken() throws IOException final String loc = (_parsingContext == null) ? "NULL" : String.valueOf(_parsingContext.pathAsPointer()); switch (t) { case FIELD_NAME: - System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.FIELD_NAME '%s'\n", loc, _parsingContext.getCurrentName()); + System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.FIELD_NAME '%s'\n", loc, _parsingContext.currentName()); break; case VALUE_STRING: System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.VALUE_STRING '%s'\n", loc, getText()); @@ -874,7 +892,7 @@ public JsonToken nextToken() throws IOException @Override public String nextFieldName() throws IOException { if (nextToken() == JsonToken.FIELD_NAME) { - return getCurrentName(); + return currentName(); } return null; } @@ -1027,7 +1045,7 @@ public String getText() throws IOException } switch (_currToken) { case FIELD_NAME: - return getCurrentName(); + return currentName(); case VALUE_STRING: return _currText; default: diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java index f53f90a49..7a2d3d78c 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java @@ -13,7 +13,6 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.ser.SerializerFactory; -import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.Impl; import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider; import com.fasterxml.jackson.databind.util.TokenBuffer; diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java index 4a75778a3..b794ff979 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java @@ -293,7 +293,7 @@ protected void verifyFieldName(JsonParser jp, String expName) throws IOException { assertEquals(expName, jp.getText()); - assertEquals(expName, jp.getCurrentName()); + assertEquals(expName, jp.currentName()); } protected void verifyException(Throwable e, String... matches) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/StringListRoundtripTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/StringListRoundtripTest.java index ea9a75f21..09297c614 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/StringListRoundtripTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/StringListRoundtripTest.java @@ -18,34 +18,35 @@ import static org.junit.Assert.assertNull; // [dataformat-xml#584] -public class StringListRoundtripTest { - private final static String[] TEST_DATA = new String[] {"", "test", null, "test2"}; - - private final static XmlMapper MAPPER_READ_WRITE_NULLS = XmlMapper.builder() - .enable(PROCESS_XSI_NIL) - .enable(WRITE_NULLS_AS_XSI_NIL) - .build(); - private final static XmlMapper MAPPER_READ_NULLS = XmlMapper.builder() - .enable(PROCESS_XSI_NIL) - .disable(WRITE_NULLS_AS_XSI_NIL) - .build(); - private final static XmlMapper MAPPER_WRITE_NULLS = XmlMapper.builder() - .disable(PROCESS_XSI_NIL) - .enable(WRITE_NULLS_AS_XSI_NIL) - .build(); - - @Test - public void testStringArray() throws Exception - { - // default mode, should get back empty string - _stringArrayRoundtrip(MAPPER_READ_NULLS, false); - - // xsi null enabled, should get back null - _stringArrayRoundtrip(MAPPER_READ_WRITE_NULLS, true); - - // xsi null write enabled but processing disabled, should get back empty string - _stringArrayRoundtrip(MAPPER_WRITE_NULLS, false); - } +public class StringListRoundtripTest +{ + private final static String[] TEST_DATA = new String[] {"", "test", null, "test2"}; + + private final static XmlMapper MAPPER_READ_WRITE_NULLS = XmlMapper.builder() + .enable(PROCESS_XSI_NIL) + .enable(WRITE_NULLS_AS_XSI_NIL) + .build(); + private final static XmlMapper MAPPER_READ_NULLS = XmlMapper.builder() + .enable(PROCESS_XSI_NIL) + .disable(WRITE_NULLS_AS_XSI_NIL) + .build(); + private final static XmlMapper MAPPER_WRITE_NULLS = XmlMapper.builder() + .disable(PROCESS_XSI_NIL) + .enable(WRITE_NULLS_AS_XSI_NIL) + .build(); + + @Test + public void testStringArray() throws Exception + { + // default mode, should get back empty string + _stringArrayRoundtrip(MAPPER_READ_NULLS, false); + + // xsi null enabled, should get back null + _stringArrayRoundtrip(MAPPER_READ_WRITE_NULLS, true); + + // xsi null write enabled but processing disabled, should get back empty string + _stringArrayRoundtrip(MAPPER_WRITE_NULLS, false); + } private void _stringArrayRoundtrip(ObjectMapper mapper, boolean shouldBeNull) throws Exception { @@ -215,35 +216,34 @@ public void testStringMapPojo() throws Exception _stringMapPojoRoundtrip(MAPPER_WRITE_NULLS, false); } - private void _stringMapPojoRoundtrip(ObjectMapper mapper, boolean shouldBeNull) throws Exception - { - Map map = new HashMap() {{ - put("a", ""); - put("b", "test"); - put("c", null); - put("d", "test2"); - }}; - MapPojo mapPojo = new MapPojo(); - mapPojo.setMap(map); - - // serialize to string - String xml = mapper.writeValueAsString(mapPojo); - assertNotNull(xml); - - // then bring it back - MapPojo result = mapper.readValue(xml, MapPojo.class); - assertEquals(4, result.map.size()); - assertEquals("", result.map.get("a")); - assertEquals("test", result.map.get("b")); - if (shouldBeNull) - { - assertNull(result.map.get("c")); - } else - { - assertEquals("", result.map.get("c")); - } - assertEquals("test2", result.map.get("d")); - } + private void _stringMapPojoRoundtrip(ObjectMapper mapper, boolean shouldBeNull) throws Exception + { + @SuppressWarnings("serial") + Map map = new HashMap() {{ + put("a", ""); + put("b", "test"); + put("c", null); + put("d", "test2"); + }}; + MapPojo mapPojo = new MapPojo(); + mapPojo.setMap(map); + + // serialize to string + String xml = mapper.writeValueAsString(mapPojo); + assertNotNull(xml); + + // then bring it back + MapPojo result = mapper.readValue(xml, MapPojo.class); + assertEquals(4, result.map.size()); + assertEquals("", result.map.get("a")); + assertEquals("test", result.map.get("b")); + if (shouldBeNull) { + assertNull(result.map.get("c")); + } else { + assertEquals("", result.map.get("c")); + } + assertEquals("test2", result.map.get("d")); + } static class MapPojo { Map map; diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserNextXxxTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserNextXxxTest.java index b2ee2e9e7..ac9b6921f 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserNextXxxTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserNextXxxTest.java @@ -36,12 +36,12 @@ public void testXmlAttributesWithNextTextValue() throws Exception // First: verify handling without forcing array handling: assertToken(JsonToken.START_OBJECT, xp.nextToken()); // assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("max", xp.getCurrentName()); + assertEquals("max", xp.currentName()); assertEquals("7", xp.nextTextValue()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("offset", xp.getCurrentName()); + assertEquals("offset", xp.currentName()); assertEquals("offset", xp.getText()); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java index e45f9ad11..c19d58868 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlParserTest.java @@ -228,21 +228,21 @@ public void testForceElementAsArray() throws Exception // First: verify handling without forcing array handling: assertToken(JsonToken.START_OBJECT, xp.nextToken()); // assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("elem", xp.getCurrentName()); + assertEquals("elem", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertEquals("value", xp.getText()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("elem", xp.getCurrentName()); + assertEquals("elem", xp.currentName()); assertToken(JsonToken.START_OBJECT, xp.nextToken()); // assertToken(JsonToken.FIELD_NAME, xp.nextToken()); - assertEquals("property", xp.getCurrentName()); + assertEquals("property", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertEquals("123", xp.getText()); assertToken(JsonToken.END_OBJECT, xp.nextToken()); // assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("elem", xp.getCurrentName()); + assertEquals("elem", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertEquals("1", xp.getText()); @@ -268,7 +268,7 @@ public void testForceElementAsArray() throws Exception assertToken(JsonToken.START_OBJECT, xp.nextToken()); // assertTrue(xp.getParsingContext().inObject()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); - assertEquals("property", xp.getCurrentName()); + assertEquals("property", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertEquals("123", xp.getText()); @@ -298,12 +298,12 @@ public void testXmlAttributes() throws Exception // First: verify handling without forcing array handling: assertToken(JsonToken.START_OBJECT, xp.nextToken()); // assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("max", xp.getCurrentName()); + assertEquals("max", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertEquals("7", xp.getText()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // - assertEquals("offset", xp.getCurrentName()); + assertEquals("offset", xp.currentName()); StringWriter w = new StringWriter(); assertEquals(6, xp.getText(w)); @@ -344,7 +344,7 @@ public void testInferredNumbers() throws Exception assertEquals("abc", xp.getText()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // @value2 - assertEquals("value2", xp.getCurrentName()); + assertEquals("value2", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertTrue(xp.isExpectedNumberIntToken()); assertEquals(JsonToken.VALUE_NUMBER_INT, xp.currentToken()); @@ -352,7 +352,7 @@ public void testInferredNumbers() throws Exception assertEquals(42, xp.getIntValue()); assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // implicit for text - assertEquals("", xp.getCurrentName()); + assertEquals("", xp.currentName()); assertToken(JsonToken.VALUE_STRING, xp.nextToken()); assertTrue(xp.isExpectedNumberIntToken()); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/dos/DeepNestingParserTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/dos/DeepNestingParserTest.java index c201d7135..1f6c9a8e0 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/dos/DeepNestingParserTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/dos/DeepNestingParserTest.java @@ -1,9 +1,10 @@ package com.fasterxml.jackson.dataformat.xml.stream.dos; import com.ctc.wstx.stax.WstxInputFactory; -import com.fasterxml.jackson.core.JsonParseException; + import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.core.exc.StreamReadException; + import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlTestBase; @@ -14,12 +15,9 @@ public void testDeepDoc() throws Exception final XmlMapper xmlMapper = newMapper(); final String XML = createDeepNestedDoc(1050); try (JsonParser p = xmlMapper.createParser(XML)) { - JsonToken jt; - while ((jt = p.nextToken()) != null) { - - } - fail("expected JsonParseException"); - } catch (JsonParseException e) { + while (p.nextToken() != null) { } + fail("expected StreamReadException"); + } catch (StreamReadException e) { assertTrue(e.getMessage().contains("Maximum Element Depth limit (1000) Exceeded")); } } @@ -31,10 +29,7 @@ public void testDeepDocWithCustomDepthLimit() throws Exception final XmlMapper xmlMapper = new XmlMapper(wstxInputFactory); final String XML = createDeepNestedDoc(1050); try (JsonParser p = xmlMapper.createParser(XML)) { - JsonToken jt; - while ((jt = p.nextToken()) != null) { - - } + while (p.nextToken() != null) { } } }