diff --git a/src/main/java/tools/jackson/databind/exc/IgnoredPropertyException.java b/src/main/java/tools/jackson/databind/exc/IgnoredPropertyException.java index e293fac87b..aebb6dc38c 100644 --- a/src/main/java/tools/jackson/databind/exc/IgnoredPropertyException.java +++ b/src/main/java/tools/jackson/databind/exc/IgnoredPropertyException.java @@ -38,8 +38,8 @@ public static IgnoredPropertyException from(JsonParser p, Collection propertyIds) { Class ref; - if (fromObjectOrClass instanceof Class) { - ref = (Class) fromObjectOrClass; + if (fromObjectOrClass instanceof Class class1) { + ref = class1; } else { // also acts as null check: ref = fromObjectOrClass.getClass(); } diff --git a/src/main/java/tools/jackson/databind/exc/UnrecognizedPropertyException.java b/src/main/java/tools/jackson/databind/exc/UnrecognizedPropertyException.java index 435599bcb4..7ea3c56a18 100644 --- a/src/main/java/tools/jackson/databind/exc/UnrecognizedPropertyException.java +++ b/src/main/java/tools/jackson/databind/exc/UnrecognizedPropertyException.java @@ -38,8 +38,8 @@ public static UnrecognizedPropertyException from(JsonParser p, Collection propertyIds) { Class ref; - if (fromObjectOrClass instanceof Class) { - ref = (Class) fromObjectOrClass; + if (fromObjectOrClass instanceof Class class1) { + ref = class1; } else { ref = fromObjectOrClass.getClass(); } diff --git a/src/main/java/tools/jackson/databind/ext/javatime/JavaTimeInitializer.java b/src/main/java/tools/jackson/databind/ext/javatime/JavaTimeInitializer.java index 7327aa7f32..a804c177c7 100644 --- a/src/main/java/tools/jackson/databind/ext/javatime/JavaTimeInitializer.java +++ b/src/main/java/tools/jackson/databind/ext/javatime/JavaTimeInitializer.java @@ -173,8 +173,7 @@ public ValueInstantiator modifyValueInstantiator(DeserializationConfig config, // So... in practice it really should always work, in the end. :) if (ZoneId.class.isAssignableFrom(raw)) { // let's assume we should be getting "empty" StdValueInstantiator here: - if (defaultInstantiator instanceof StdValueInstantiator) { - StdValueInstantiator inst = (StdValueInstantiator) defaultInstantiator; + if (defaultInstantiator instanceof StdValueInstantiator inst) { // one further complication: we need ZoneId info, not sub-class AnnotatedClass ac; if (raw == ZoneId.class) { diff --git a/src/main/java/tools/jackson/databind/ext/javatime/deser/JSR310DeserializerBase.java b/src/main/java/tools/jackson/databind/ext/javatime/deser/JSR310DeserializerBase.java index 23ca16f384..50ecd08869 100644 --- a/src/main/java/tools/jackson/databind/ext/javatime/deser/JSR310DeserializerBase.java +++ b/src/main/java/tools/jackson/databind/ext/javatime/deser/JSR310DeserializerBase.java @@ -229,8 +229,8 @@ public Object handleUnexpectedToken(Class instClass, JsonToken t, protected DateTimeException _peelDTE(DateTimeException e) { while (true) { Throwable t = e.getCause(); - if (t != null && t instanceof DateTimeException) { - e = (DateTimeException) t; + if (t != null && t instanceof DateTimeException dte) { + e = dte; continue; } break; diff --git a/src/main/java/tools/jackson/databind/introspect/AnnotationIntrospectorPair.java b/src/main/java/tools/jackson/databind/introspect/AnnotationIntrospectorPair.java index 402dc97f90..10b4dc91a4 100644 --- a/src/main/java/tools/jackson/databind/introspect/AnnotationIntrospectorPair.java +++ b/src/main/java/tools/jackson/databind/introspect/AnnotationIntrospectorPair.java @@ -724,8 +724,8 @@ protected boolean _isExplicitClassOrOb(Object maybeCls, Class implicit) { if ((maybeCls == null) || (maybeCls == implicit)) { return false; } - if (maybeCls instanceof Class) { - return !ClassUtil.isBogusClass((Class) maybeCls); + if (maybeCls instanceof Class class1) { + return !ClassUtil.isBogusClass(class1); } return true; } @@ -734,7 +734,7 @@ protected Object _explicitClassOrOb(Object maybeCls, Class implicit) { if ((maybeCls == null) || (maybeCls == implicit)) { return null; } - if ((maybeCls instanceof Class) && ClassUtil.isBogusClass((Class) maybeCls)) { + if (maybeCls instanceof Class class1 && ClassUtil.isBogusClass(class1)) { return null; } return maybeCls; diff --git a/src/main/java/tools/jackson/databind/introspect/EnumNamingStrategyFactory.java b/src/main/java/tools/jackson/databind/introspect/EnumNamingStrategyFactory.java index 85a2b18105..0331c990cc 100644 --- a/src/main/java/tools/jackson/databind/introspect/EnumNamingStrategyFactory.java +++ b/src/main/java/tools/jackson/databind/introspect/EnumNamingStrategyFactory.java @@ -29,8 +29,8 @@ public static EnumNamingStrategy createEnumNamingStrategyInstance(Object namingD if (namingDef == null) { return defaultNamingStrategy; } - if (namingDef instanceof EnumNamingStrategy) { - return (EnumNamingStrategy) namingDef; + if (namingDef instanceof EnumNamingStrategy strategy) { + return strategy; } if (!(namingDef instanceof Class)) { throw new IllegalArgumentException(String.format( diff --git a/src/main/java/tools/jackson/databind/jsontype/TypeResolverProvider.java b/src/main/java/tools/jackson/databind/jsontype/TypeResolverProvider.java index 169deaa47b..8f9f3bd11f 100644 --- a/src/main/java/tools/jackson/databind/jsontype/TypeResolverProvider.java +++ b/src/main/java/tools/jackson/databind/jsontype/TypeResolverProvider.java @@ -234,7 +234,7 @@ protected TypeResolverBuilder _findTypeResolver(MapperConfig config, // to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY" if (ann instanceof AnnotatedClass) { JsonTypeInfo.As inclusion = typeInfo.getInclusionType(); - if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) { + if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY) { typeInfo = typeInfo.withInclusionType(JsonTypeInfo.As.PROPERTY); } } diff --git a/src/main/java/tools/jackson/databind/jsontype/impl/ClassNameIdResolver.java b/src/main/java/tools/jackson/databind/jsontype/impl/ClassNameIdResolver.java index 1bacdca13b..f7ad406fc6 100644 --- a/src/main/java/tools/jackson/databind/jsontype/impl/ClassNameIdResolver.java +++ b/src/main/java/tools/jackson/databind/jsontype/impl/ClassNameIdResolver.java @@ -73,8 +73,8 @@ public JavaType typeFromId(DatabindContext ctxt, String id) throws JacksonExcept protected JavaType _typeFromId(DatabindContext ctxt, String id) throws JacksonException { DeserializationContext deserializationContext = null; - if (ctxt instanceof DeserializationContext) { - deserializationContext = (DeserializationContext) ctxt; + if (ctxt instanceof DeserializationContext dContext) { + deserializationContext = dContext; } if ((_allowedSubtypes != null) && (deserializationContext != null) && deserializationContext.isEnabled( @@ -108,12 +108,12 @@ protected String _idFrom(DatabindContext ctxt, Object value, Class cls) // Enum sets and maps are problematic since we MUST know type of // contained enums, to be able to deserialize. // In addition, EnumSet is not a concrete type either - if (value instanceof EnumSet) { // Regular- and JumboEnumSet... - Class enumClass = ClassUtil.findEnumType((EnumSet) value); + if (value instanceof EnumSet enumSet) { // Regular- and JumboEnumSet... + Class enumClass = ClassUtil.findEnumType(enumSet); // not optimal: but EnumSet is not a customizable type so this is sort of ok str = ctxt.getTypeFactory().constructCollectionType(EnumSet.class, enumClass).toCanonical(); - } else if (value instanceof EnumMap) { - Class enumClass = ClassUtil.findEnumType((EnumMap) value); + } else if (value instanceof EnumMap enumMap) { + Class enumClass = ClassUtil.findEnumType(enumMap); Class valueClass = Object.class; // not optimal: but EnumMap is not a customizable type so this is sort of ok str = ctxt.getTypeFactory().constructMapType(EnumMap.class, enumClass, valueClass).toCanonical(); diff --git a/src/main/java/tools/jackson/databind/jsontype/impl/TypeDeserializerBase.java b/src/main/java/tools/jackson/databind/jsontype/impl/TypeDeserializerBase.java index bfc103cce6..0d4dd6a870 100644 --- a/src/main/java/tools/jackson/databind/jsontype/impl/TypeDeserializerBase.java +++ b/src/main/java/tools/jackson/databind/jsontype/impl/TypeDeserializerBase.java @@ -246,7 +246,7 @@ protected Object _deserializeWithNativeTypeId(JsonParser p, DeserializationConte "No (native) type id found when one was expected for polymorphic type handling"); } } else { - String typeIdStr = (typeId instanceof String) ? (String) typeId : String.valueOf(typeId); + String typeIdStr = (typeId instanceof String string) ? string : String.valueOf(typeId); deser = _findDeserializer(ctxt, typeIdStr); } return deser.deserialize(p, ctxt); diff --git a/src/main/java/tools/jackson/databind/node/ArrayNode.java b/src/main/java/tools/jackson/databind/node/ArrayNode.java index 17154a9f83..a9405f5f5d 100644 --- a/src/main/java/tools/jackson/databind/node/ArrayNode.java +++ b/src/main/java/tools/jackson/databind/node/ArrayNode.java @@ -93,8 +93,8 @@ protected ObjectNode _withObject(JsonPointer origPtr, } JsonNode n = _at(currentPtr); // If there's a path, follow it - if ((n != null) && (n instanceof BaseJsonNode)) { - ObjectNode found = ((BaseJsonNode) n)._withObject(origPtr, currentPtr.tail(), + if (n instanceof BaseJsonNode baseNode) { + ObjectNode found = baseNode._withObject(origPtr, currentPtr.tail(), overwriteMode, preferIndex); if (found != null) { return found; @@ -116,8 +116,8 @@ protected ArrayNode _withArray(JsonPointer origPtr, } JsonNode n = _at(currentPtr); // If there's a path, follow it - if ((n != null) && (n instanceof BaseJsonNode)) { - ArrayNode found = ((BaseJsonNode) n)._withArray(origPtr, currentPtr.tail(), + if (n instanceof BaseJsonNode baseNode) { + ArrayNode found = baseNode._withArray(origPtr, currentPtr.tail(), overwriteMode, preferIndex); if (found != null) { return found; @@ -296,22 +296,21 @@ public Collection elements() { @Override public boolean equals(Comparator comparator, JsonNode o) { - if (!(o instanceof ArrayNode)) { - return false; - } - ArrayNode other = (ArrayNode) o; - final int len = _children.size(); - if (other.size() != len) { - return false; - } - List l1 = _children; - List l2 = other._children; - for (int i = 0; i < len; ++i) { - if (!l1.get(i).equals(comparator, l2.get(i))) { + if (o instanceof ArrayNode other) { + final int len = _children.size(); + if (other.size() != len) { return false; } + List l1 = _children; + List l2 = other._children; + for (int i = 0; i < len; ++i) { + if (!l1.get(i).equals(comparator, l2.get(i))) { + return false; + } + } + return true; } - return true; + return false; } /* @@ -1163,8 +1162,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof ArrayNode) { - return _children.equals(((ArrayNode) o)._children); + if (o instanceof ArrayNode arrayNode) { + return _children.equals(arrayNode._children); } return false; } diff --git a/src/main/java/tools/jackson/databind/node/BaseJsonNode.java b/src/main/java/tools/jackson/databind/node/BaseJsonNode.java index da82eb5fef..5a927e67e3 100644 --- a/src/main/java/tools/jackson/databind/node/BaseJsonNode.java +++ b/src/main/java/tools/jackson/databind/node/BaseJsonNode.java @@ -485,8 +485,8 @@ public ObjectNode withObject(JsonPointer ptr, { // Degenerate case of using with "empty" path; ok if ObjectNode if (ptr.matches()) { - if (this instanceof ObjectNode) { - return (ObjectNode) this; + if (this instanceof ObjectNode objectNode) { + return objectNode; } _reportWrongNodeType("Can only call `withObject()` with empty JSON Pointer on `ObjectNode`, not %s", ClassUtil.nameOf(getClass())); @@ -551,8 +551,8 @@ public ArrayNode withArray(JsonPointer ptr, { // Degenerate case of using with "empty" path; ok if ArrayNode if (ptr.matches()) { - if (this instanceof ArrayNode) { - return (ArrayNode) this; + if (this instanceof ArrayNode arrayNode) { + return arrayNode; } _reportWrongNodeType("Can only call `withArray()` with empty JSON Pointer on `ArrayNode`, not %s", ClassUtil.nameOf(getClass())); diff --git a/src/main/java/tools/jackson/databind/node/BigIntegerNode.java b/src/main/java/tools/jackson/databind/node/BigIntegerNode.java index 6f8cdd6f41..34aced0b57 100644 --- a/src/main/java/tools/jackson/databind/node/BigIntegerNode.java +++ b/src/main/java/tools/jackson/databind/node/BigIntegerNode.java @@ -366,8 +366,7 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof BigIntegerNode) { - BigIntegerNode otherNode = (BigIntegerNode) o; + if (o instanceof BigIntegerNode otherNode) { return Objects.equals(otherNode._value, _value); } return false; diff --git a/src/main/java/tools/jackson/databind/node/BinaryNode.java b/src/main/java/tools/jackson/databind/node/BinaryNode.java index ce8be58f48..392b30e85d 100644 --- a/src/main/java/tools/jackson/databind/node/BinaryNode.java +++ b/src/main/java/tools/jackson/databind/node/BinaryNode.java @@ -130,8 +130,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof BinaryNode) { - byte[] otherData = ((BinaryNode) o)._data; + if (o instanceof BinaryNode binaryNode) { + byte[] otherData = binaryNode._data; return Arrays.equals(_data, otherData); } return false; diff --git a/src/main/java/tools/jackson/databind/node/BooleanNode.java b/src/main/java/tools/jackson/databind/node/BooleanNode.java index 09e252a7b7..c5ee6b9d94 100644 --- a/src/main/java/tools/jackson/databind/node/BooleanNode.java +++ b/src/main/java/tools/jackson/databind/node/BooleanNode.java @@ -137,9 +137,9 @@ public boolean equals(Object o) */ if (o == this) return true; if (o == null) return false; - if (!(o instanceof BooleanNode)) { - return false; + if (o instanceof BooleanNode otherNode) { + return (_value == otherNode._value); } - return (_value == ((BooleanNode) o)._value); + return false; } } diff --git a/src/main/java/tools/jackson/databind/node/DecimalNode.java b/src/main/java/tools/jackson/databind/node/DecimalNode.java index 1d9544443e..a2ab1f834d 100644 --- a/src/main/java/tools/jackson/databind/node/DecimalNode.java +++ b/src/main/java/tools/jackson/databind/node/DecimalNode.java @@ -270,8 +270,7 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof DecimalNode) { - DecimalNode otherNode = (DecimalNode) o; + if (o instanceof DecimalNode otherNode) { return otherNode._value.equals(_value); } return false; diff --git a/src/main/java/tools/jackson/databind/node/DoubleNode.java b/src/main/java/tools/jackson/databind/node/DoubleNode.java index bab9aa704f..73ef713866 100644 --- a/src/main/java/tools/jackson/databind/node/DoubleNode.java +++ b/src/main/java/tools/jackson/databind/node/DoubleNode.java @@ -220,10 +220,10 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof DoubleNode) { + if (o instanceof DoubleNode otherNode) { // We must account for NaNs: NaN does not equal NaN, therefore we have // to use Double.compare(). - final double otherValue = ((DoubleNode) o)._value; + final double otherValue = otherNode._value; return Double.compare(_value, otherValue) == 0; } return false; diff --git a/src/main/java/tools/jackson/databind/node/FloatNode.java b/src/main/java/tools/jackson/databind/node/FloatNode.java index 49abaf8581..adb8b5c695 100644 --- a/src/main/java/tools/jackson/databind/node/FloatNode.java +++ b/src/main/java/tools/jackson/databind/node/FloatNode.java @@ -189,10 +189,10 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof FloatNode) { + if (o instanceof FloatNode otherNode) { // We must account for NaNs: NaN does not equal NaN, therefore we have // to use Float.compare(). - final float otherValue = ((FloatNode) o)._value; + final float otherValue = otherNode._value; return Float.compare(_value, otherValue) == 0; } return false; diff --git a/src/main/java/tools/jackson/databind/node/IntNode.java b/src/main/java/tools/jackson/databind/node/IntNode.java index e626c38035..e6f36d2ca2 100644 --- a/src/main/java/tools/jackson/databind/node/IntNode.java +++ b/src/main/java/tools/jackson/databind/node/IntNode.java @@ -250,8 +250,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof IntNode) { - return ((IntNode) o)._value == _value; + if (o instanceof IntNode otherNode) { + return otherNode._value == _value; } return false; } diff --git a/src/main/java/tools/jackson/databind/node/LongNode.java b/src/main/java/tools/jackson/databind/node/LongNode.java index e268adb717..4f3919adcc 100644 --- a/src/main/java/tools/jackson/databind/node/LongNode.java +++ b/src/main/java/tools/jackson/databind/node/LongNode.java @@ -240,8 +240,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof LongNode) { - return ((LongNode) o)._value == _value; + if (o instanceof LongNode otherNode) { + return otherNode._value == _value; } return false; } diff --git a/src/main/java/tools/jackson/databind/node/ObjectNode.java b/src/main/java/tools/jackson/databind/node/ObjectNode.java index d26cf271d7..1c00dad726 100644 --- a/src/main/java/tools/jackson/databind/node/ObjectNode.java +++ b/src/main/java/tools/jackson/databind/node/ObjectNode.java @@ -138,8 +138,8 @@ protected ObjectNode _withObject(JsonPointer origPtr, JsonNode n = _at(currentPtr); // If there's a path, follow it - if ((n != null) && (n instanceof BaseJsonNode)) { - ObjectNode found = ((BaseJsonNode) n)._withObject(origPtr, currentPtr.tail(), + if (n instanceof BaseJsonNode baseNode) { + ObjectNode found = baseNode._withObject(origPtr, currentPtr.tail(), overwriteMode, preferIndex); if (found != null) { return found; @@ -163,8 +163,8 @@ protected ArrayNode _withArray(JsonPointer origPtr, JsonNode n = _at(currentPtr); // If there's a path, follow it - if ((n != null) && (n instanceof BaseJsonNode)) { - ArrayNode found = ((BaseJsonNode) n)._withArray(origPtr, currentPtr.tail(), + if (n instanceof BaseJsonNode baseNode) { + ArrayNode found = baseNode._withArray(origPtr, currentPtr.tail(), overwriteMode, preferIndex); if (found != null) { return found; @@ -328,25 +328,24 @@ public void forEachEntry(BiConsumer action) { @Override public boolean equals(Comparator comparator, JsonNode o) { - if (!(o instanceof ObjectNode)) { - return false; - } - ObjectNode other = (ObjectNode) o; - Map m1 = _children; - Map m2 = other._children; - - final int len = m1.size(); - if (m2.size() != len) { - return false; - } + if (o instanceof ObjectNode other) { + Map m1 = _children; + Map m2 = other._children; - for (Map.Entry entry : m1.entrySet()) { - JsonNode v2 = m2.get(entry.getKey()); - if ((v2 == null) || !entry.getValue().equals(comparator, v2)) { + final int len = m1.size(); + if (m2.size() != len) { return false; } + + for (Map.Entry entry : m1.entrySet()) { + JsonNode v2 = m2.get(entry.getKey()); + if ((v2 == null) || !entry.getValue().equals(comparator, v2)) { + return false; + } + } + return true; } - return true; + return false; } /* @@ -1030,8 +1029,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof ObjectNode) { - return _childrenEqual((ObjectNode) o); + if (o instanceof ObjectNode objectNode) { + return _childrenEqual(objectNode); } return false; } diff --git a/src/main/java/tools/jackson/databind/node/POJONode.java b/src/main/java/tools/jackson/databind/node/POJONode.java index 5e49de6b5e..856c3a5a4f 100644 --- a/src/main/java/tools/jackson/databind/node/POJONode.java +++ b/src/main/java/tools/jackson/databind/node/POJONode.java @@ -650,8 +650,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof POJONode) { - return _pojoEquals((POJONode) o); + if (o instanceof POJONode pojoNode) { + return _pojoEquals(pojoNode); } return false; } diff --git a/src/main/java/tools/jackson/databind/node/ShortNode.java b/src/main/java/tools/jackson/databind/node/ShortNode.java index d8e4e7a32f..0818e542c7 100644 --- a/src/main/java/tools/jackson/databind/node/ShortNode.java +++ b/src/main/java/tools/jackson/databind/node/ShortNode.java @@ -220,8 +220,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof ShortNode) { - return ((ShortNode) o)._value == _value; + if (o instanceof ShortNode other) { + return other._value == _value; } return false; } diff --git a/src/main/java/tools/jackson/databind/node/StringNode.java b/src/main/java/tools/jackson/databind/node/StringNode.java index b75e5c92f9..62816b4892 100644 --- a/src/main/java/tools/jackson/databind/node/StringNode.java +++ b/src/main/java/tools/jackson/databind/node/StringNode.java @@ -428,8 +428,8 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; - if (o instanceof StringNode) { - return Objects.equals(((StringNode) o)._value, _value); + if (o instanceof StringNode other) { + return Objects.equals(other._value, _value); } return false; } diff --git a/src/main/java/tools/jackson/databind/node/TreeBuildingGenerator.java b/src/main/java/tools/jackson/databind/node/TreeBuildingGenerator.java index 769deba5e6..dd7ce8fe9d 100644 --- a/src/main/java/tools/jackson/databind/node/TreeBuildingGenerator.java +++ b/src/main/java/tools/jackson/databind/node/TreeBuildingGenerator.java @@ -451,8 +451,8 @@ public JsonGenerator writeTree(TreeNode node) if (node == null) { return writeNull(); } - if (node instanceof JsonNode) { - _tokenWriteContext.writeNode((JsonNode) node); + if (node instanceof JsonNode jsonNode) { + _tokenWriteContext.writeNode(jsonNode); } else { _tokenWriteContext.writePOJO(node); } diff --git a/src/main/java/tools/jackson/databind/node/TreeTraversingParser.java b/src/main/java/tools/jackson/databind/node/TreeTraversingParser.java index 2f5116c1e5..f803ac90ea 100644 --- a/src/main/java/tools/jackson/databind/node/TreeTraversingParser.java +++ b/src/main/java/tools/jackson/databind/node/TreeTraversingParser.java @@ -419,8 +419,8 @@ public Object getEmbeddedObject() public boolean isNaN() { if (!_closed) { JsonNode n = currentNode(); - if (n instanceof NumericNode) { - return ((NumericNode) n).isNaN(); + if (n instanceof NumericNode numericNode) { + return numericNode.isNaN(); } } return false; @@ -441,8 +441,8 @@ public byte[] getBinaryValue(Base64Variant b64variant) if (n != null) { // [databind#2096]: although `binaryValue()` works for real binary node // and embedded "POJO" node, coercion from `StringNode` may require variant, so: - if (n instanceof StringNode) { - return ((StringNode) n).getBinaryValue(b64variant); + if (n instanceof StringNode stringNode) { + return stringNode.getBinaryValue(b64variant); } return n.binaryValue(); } diff --git a/src/main/java/tools/jackson/databind/ser/AnyGetterWriter.java b/src/main/java/tools/jackson/databind/ser/AnyGetterWriter.java index 49d7ff9851..b24a191d40 100644 --- a/src/main/java/tools/jackson/databind/ser/AnyGetterWriter.java +++ b/src/main/java/tools/jackson/databind/ser/AnyGetterWriter.java @@ -40,8 +40,8 @@ public AnyGetterWriter(BeanPropertyWriter parent, BeanProperty property, _accessor = accessor; _property = property; _serializer = (ValueSerializer) serializer; - if (serializer instanceof MapSerializer) { - _mapSerializer = (MapSerializer) serializer; + if (serializer instanceof MapSerializer mapSer) { + _mapSerializer = mapSer; } } @@ -58,8 +58,8 @@ public void resolve(SerializationContext provider) // 05-Sep-2013, tatu: I _think_ this can be considered a primary property... ValueSerializer ser = provider.handlePrimaryContextualization(_serializer, _property); _serializer = (ValueSerializer) ser; - if (ser instanceof MapSerializer) { - _mapSerializer = (MapSerializer) ser; + if (ser instanceof MapSerializer mapSer) { + _mapSerializer = mapSer; } } diff --git a/src/main/java/tools/jackson/databind/ser/BasicSerializerFactory.java b/src/main/java/tools/jackson/databind/ser/BasicSerializerFactory.java index 5fd8b13184..059d42b4ec 100644 --- a/src/main/java/tools/jackson/databind/ser/BasicSerializerFactory.java +++ b/src/main/java/tools/jackson/databind/ser/BasicSerializerFactory.java @@ -487,8 +487,8 @@ protected ValueSerializer buildContainerSerializer(SerializationContext ctxt, * But we do need to check class annotations. */ ValueSerializer keySerializer = _findKeySerializer(ctxt, beanDescRef.getClassInfo()); - if (mlt instanceof MapType) { - return buildMapSerializer(ctxt, (MapType) mlt, + if (mlt instanceof MapType mapType) { + return buildMapSerializer(ctxt, mapType, beanDescRef, formatOverrides, staticTyping, keySerializer, elementTypeSerializer, elementValueSerializer); } @@ -517,8 +517,8 @@ protected ValueSerializer buildContainerSerializer(SerializationContext ctxt, } if (type.isCollectionLikeType()) { CollectionLikeType clt = (CollectionLikeType) type; - if (clt instanceof CollectionType) { - return buildCollectionSerializer(ctxt, (CollectionType) clt, + if (clt instanceof CollectionType collectionType) { + return buildCollectionSerializer(ctxt, collectionType, beanDescRef, formatOverrides, staticTyping, elementTypeSerializer, elementValueSerializer); } diff --git a/src/main/java/tools/jackson/databind/ser/SerializationContextExt.java b/src/main/java/tools/jackson/databind/ser/SerializationContextExt.java index 7763c3dbf0..8c11eb9373 100644 --- a/src/main/java/tools/jackson/databind/ser/SerializationContextExt.java +++ b/src/main/java/tools/jackson/databind/ser/SerializationContextExt.java @@ -71,8 +71,8 @@ public ValueSerializer serializerInstance(Annotated annotated, Object se } ValueSerializer ser; - if (serDef instanceof ValueSerializer) { - ser = (ValueSerializer) serDef; + if (serDef instanceof ValueSerializer serializer) { + ser = serializer; } else { // Alas, there's no way to force return type of "either class // X or Y" -- need to throw an exception after the fact diff --git a/src/main/java/tools/jackson/databind/ser/bean/BeanSerializerBase.java b/src/main/java/tools/jackson/databind/ser/bean/BeanSerializerBase.java index ef2f7e64d7..97db4aef78 100644 --- a/src/main/java/tools/jackson/databind/ser/bean/BeanSerializerBase.java +++ b/src/main/java/tools/jackson/databind/ser/bean/BeanSerializerBase.java @@ -341,8 +341,8 @@ public void resolve(SerializationContext provider) // also, any-getter may need to be resolved for (int i = 0; i < _props.length; i++) { BeanPropertyWriter prop = _props[i]; - if (prop instanceof AnyGetterWriter) { - ((AnyGetterWriter) prop).resolve(provider); + if (prop instanceof AnyGetterWriter anyGetterWriter) { + anyGetterWriter.resolve(provider); } } } diff --git a/src/main/java/tools/jackson/databind/ser/bean/UnwrappingBeanPropertyWriter.java b/src/main/java/tools/jackson/databind/ser/bean/UnwrappingBeanPropertyWriter.java index e60ae6d557..aab21ef008 100644 --- a/src/main/java/tools/jackson/databind/ser/bean/UnwrappingBeanPropertyWriter.java +++ b/src/main/java/tools/jackson/databind/ser/bean/UnwrappingBeanPropertyWriter.java @@ -133,8 +133,8 @@ public void assignSerializer(ValueSerializer ser) if (ser.isUnwrappingSerializer() // as per [databind#2060], need to also check this, in case someone writes // custom implementation that does not extend standard implementation: - && (ser instanceof UnwrappingBeanSerializer)) { - t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializer) ser)._nameTransformer); + && ser instanceof UnwrappingBeanSerializer unwrappingBeanSerializer) { + t = NameTransformer.chainedTransformer(t, unwrappingBeanSerializer._nameTransformer); } ser = ser.unwrappingSerializer(t); } @@ -207,8 +207,8 @@ protected ValueSerializer _findAndAddDynamic(PropertySerializerMap map, if (serializer.isUnwrappingSerializer() // as per [databind#2060], need to also check this, in case someone writes // custom implementation that does not extend standard implementation: - && (serializer instanceof UnwrappingBeanSerializer)) { - t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializer) serializer)._nameTransformer); + && serializer instanceof UnwrappingBeanSerializer unwrappingBeanSerializer) { + t = NameTransformer.chainedTransformer(t, unwrappingBeanSerializer._nameTransformer); } serializer = serializer.unwrappingSerializer(t); diff --git a/src/main/java/tools/jackson/databind/ser/jackson/JacksonSerializableSerializer.java b/src/main/java/tools/jackson/databind/ser/jackson/JacksonSerializableSerializer.java index 6494ede958..79eeac574f 100644 --- a/src/main/java/tools/jackson/databind/ser/jackson/JacksonSerializableSerializer.java +++ b/src/main/java/tools/jackson/databind/ser/jackson/JacksonSerializableSerializer.java @@ -27,8 +27,8 @@ public class JacksonSerializableSerializer @Override public boolean isEmpty(SerializationContext serializers, JacksonSerializable value) { - if (value instanceof JacksonSerializable.Base) { - return ((JacksonSerializable.Base) value).isEmpty(serializers); + if (value instanceof JacksonSerializable.Base base) { + return base.isEmpty(serializers); } return false; } diff --git a/src/main/java/tools/jackson/databind/ser/jdk/DateTimeSerializerBase.java b/src/main/java/tools/jackson/databind/ser/jdk/DateTimeSerializerBase.java index ed50bfd50b..8ceb7223a0 100644 --- a/src/main/java/tools/jackson/databind/ser/jdk/DateTimeSerializerBase.java +++ b/src/main/java/tools/jackson/databind/ser/jdk/DateTimeSerializerBase.java @@ -95,8 +95,7 @@ public ValueSerializer createContextual(SerializationContext serializers, DateFormat df0 = serializers.getConfig().getDateFormat(); // Jackson's own `StdDateFormat` is quite easy to deal with... - if (df0 instanceof StdDateFormat) { - StdDateFormat std = (StdDateFormat) df0; + if (df0 instanceof StdDateFormat std) { if (format.hasLocale()) { std = std.withLocale(format.getLocale()); } diff --git a/src/main/java/tools/jackson/databind/ser/jdk/MapProperty.java b/src/main/java/tools/jackson/databind/ser/jdk/MapProperty.java index 9679e1323a..49a0ce774d 100644 --- a/src/main/java/tools/jackson/databind/ser/jdk/MapProperty.java +++ b/src/main/java/tools/jackson/databind/ser/jdk/MapProperty.java @@ -49,8 +49,8 @@ public void reset(Object key, Object value, @Override public String getName() { - if (_key instanceof String) { - return (String) _key; + if (_key instanceof String string) { + return string; } return String.valueOf(_key); } diff --git a/src/main/java/tools/jackson/databind/ser/jdk/NumberSerializer.java b/src/main/java/tools/jackson/databind/ser/jdk/NumberSerializer.java index 7b3b6e0712..b3b75a0ce5 100644 --- a/src/main/java/tools/jackson/databind/ser/jdk/NumberSerializer.java +++ b/src/main/java/tools/jackson/databind/ser/jdk/NumberSerializer.java @@ -69,10 +69,10 @@ public ValueSerializer createContextual(SerializationContext prov, public void serialize(Number value, JsonGenerator g, SerializationContext provider) throws JacksonException { // should mostly come in as one of these two: - if (value instanceof BigDecimal) { - g.writeNumber((BigDecimal) value); - } else if (value instanceof BigInteger) { - g.writeNumber((BigInteger) value); + if (value instanceof BigDecimal bd) { + g.writeNumber(bd); + } else if (value instanceof BigInteger bi) { + g.writeNumber(bi); // These should not occur, as more specific methods should have been called; but // just in case let's cover all bases: diff --git a/src/main/java/tools/jackson/databind/ser/std/SimpleBeanPropertyFilter.java b/src/main/java/tools/jackson/databind/ser/std/SimpleBeanPropertyFilter.java index 68f977e480..1d6b706a3d 100644 --- a/src/main/java/tools/jackson/databind/ser/std/SimpleBeanPropertyFilter.java +++ b/src/main/java/tools/jackson/databind/ser/std/SimpleBeanPropertyFilter.java @@ -122,8 +122,8 @@ public void serializeAsProperty(Object pojo, JsonGenerator g, writer.serializeAsProperty(pojo, g, provider); } else if (!g.canOmitProperties()) { writer.serializeAsOmittedProperty(pojo, g, provider); - } else if (writer instanceof AnyGetterWriter) { - ((AnyGetterWriter) writer).getAndFilter(pojo, g, provider, this); + } else if (writer instanceof AnyGetterWriter anyGetterWriter) { + anyGetterWriter.getAndFilter(pojo, g, provider, this); } } diff --git a/src/main/java/tools/jackson/databind/type/CollectionLikeType.java b/src/main/java/tools/jackson/databind/type/CollectionLikeType.java index 689378a169..7ac868872b 100644 --- a/src/main/java/tools/jackson/databind/type/CollectionLikeType.java +++ b/src/main/java/tools/jackson/databind/type/CollectionLikeType.java @@ -55,8 +55,8 @@ public static CollectionLikeType construct(Class rawType, TypeBindings bindin public static CollectionLikeType upgradeFrom(JavaType baseType, JavaType elementType) { // 19-Oct-2015, tatu: Not sure if and how other types could be used as base; // will cross that bridge if and when need be - if (baseType instanceof TypeBase) { - return new CollectionLikeType((TypeBase) baseType, elementType); + if (baseType instanceof TypeBase base) { + return new CollectionLikeType(base, elementType); } throw new IllegalArgumentException("Cannot upgrade from an instance of "+baseType.getClass()); } diff --git a/src/main/java/tools/jackson/databind/type/IterationType.java b/src/main/java/tools/jackson/databind/type/IterationType.java index 48a36d94d6..a10c1bfdea 100644 --- a/src/main/java/tools/jackson/databind/type/IterationType.java +++ b/src/main/java/tools/jackson/databind/type/IterationType.java @@ -47,8 +47,8 @@ public static IterationType upgradeFrom(JavaType baseType, JavaType iteratedType Objects.requireNonNull(iteratedType); // 19-Oct-2015, tatu: Not sure if and how other types could be used as base; // will cross that bridge if and when need be - if (baseType instanceof TypeBase) { - return new IterationType((TypeBase) baseType, iteratedType); + if (baseType instanceof TypeBase base) { + return new IterationType(base, iteratedType); } throw new IllegalArgumentException("Cannot upgrade from an instance of "+baseType.getClass()); } diff --git a/src/main/java/tools/jackson/databind/type/MapLikeType.java b/src/main/java/tools/jackson/databind/type/MapLikeType.java index 900a64b84e..20c1960e1e 100644 --- a/src/main/java/tools/jackson/databind/type/MapLikeType.java +++ b/src/main/java/tools/jackson/databind/type/MapLikeType.java @@ -58,8 +58,8 @@ public static MapLikeType upgradeFrom(JavaType baseType, JavaType keyT, // 19-Oct-2015, tatu: Not sure if and how other types could be used as // base; // will cross that bridge if and when need be - if (baseType instanceof TypeBase) { - return new MapLikeType((TypeBase) baseType, keyT, valueT); + if (baseType instanceof TypeBase base) { + return new MapLikeType(base, keyT, valueT); } throw new IllegalArgumentException( "Cannot upgrade from an instance of " + baseType.getClass()); @@ -117,11 +117,11 @@ public JavaType withHandlersFrom(JavaType src) { JavaType type = super.withHandlersFrom(src); JavaType srcKeyType = src.getKeyType(); // "withKeyType()" not part of JavaType, hence must verify: - if (type instanceof MapLikeType) { + if (type instanceof MapLikeType mapLikeType) { if (srcKeyType != null) { JavaType ct = _keyType.withHandlersFrom(srcKeyType); if (ct != _keyType) { - type = ((MapLikeType) type).withKeyType(ct); + type = mapLikeType.withKeyType(ct); } } } diff --git a/src/main/java/tools/jackson/databind/type/ReferenceType.java b/src/main/java/tools/jackson/databind/type/ReferenceType.java index c12470395e..f5075cb9ea 100644 --- a/src/main/java/tools/jackson/databind/type/ReferenceType.java +++ b/src/main/java/tools/jackson/databind/type/ReferenceType.java @@ -63,8 +63,8 @@ public static ReferenceType upgradeFrom(JavaType baseType, JavaType refdType) { } // 19-Oct-2015, tatu: Not sure if and how other types could be used as base; // will cross that bridge if and when need be - if (baseType instanceof TypeBase) { - return new ReferenceType((TypeBase) baseType, refdType); + if (baseType instanceof TypeBase base) { + return new ReferenceType(base, refdType); } throw new IllegalArgumentException("Cannot upgrade from an instance of "+baseType.getClass()); } diff --git a/src/main/java/tools/jackson/databind/type/TypeBindings.java b/src/main/java/tools/jackson/databind/type/TypeBindings.java index cbf57f6b4d..122c48b376 100644 --- a/src/main/java/tools/jackson/databind/type/TypeBindings.java +++ b/src/main/java/tools/jackson/databind/type/TypeBindings.java @@ -241,8 +241,7 @@ public JavaType findBoundType(String name) for (int i = 0, len = _names.length; i < len; ++i) { if (name.equals(_names[i])) { JavaType t = _types[i]; - if (t instanceof ResolvedRecursiveType) { - ResolvedRecursiveType rrt = (ResolvedRecursiveType) t; + if (t instanceof ResolvedRecursiveType rrt) { JavaType t2 = rrt.getSelfReferencedType(); if (t2 != null) { t = t2; diff --git a/src/main/java/tools/jackson/databind/type/TypeFactory.java b/src/main/java/tools/jackson/databind/type/TypeFactory.java index 9ce3118e8c..71aa4737c9 100644 --- a/src/main/java/tools/jackson/databind/type/TypeFactory.java +++ b/src/main/java/tools/jackson/databind/type/TypeFactory.java @@ -286,8 +286,8 @@ public static JavaType unknownType() { * default instance is used for determination. */ public static Class rawClass(Type t) { - if (t instanceof Class) { - return (Class) t; + if (t instanceof Class class1) { + return class1; } else if (t instanceof JavaType jt) { return jt.getRawClass(); } else if (t instanceof GenericArrayType gat) { @@ -597,8 +597,8 @@ private String _resolveTypePlaceholders(JavaType sourceType, JavaType actualType private boolean _verifyAndResolvePlaceholders(JavaType exp, JavaType act) { // See if we have an actual type placeholder to resolve; if yes, replace - if (act instanceof PlaceholderForType) { - ((PlaceholderForType) act).actualType(exp); + if (act instanceof PlaceholderForType placeholderForType) { + placeholderForType.actualType(exp); return true; } // if not, try to verify compatibility. But note that we cannot @@ -862,8 +862,8 @@ public CollectionLikeType constructCollectionLikeType(Class collectionClass, public CollectionLikeType constructCollectionLikeType(Class collectionClass, JavaType elementType) { JavaType type = _fromClass(null, collectionClass, TypeBindings.createIfNeeded(collectionClass, elementType)); - if (type instanceof CollectionLikeType) { - return (CollectionLikeType) type; + if (type instanceof CollectionLikeType collectionLikeType) { + return collectionLikeType; } return CollectionLikeType.upgradeFrom(type, elementType); } @@ -948,8 +948,8 @@ public MapLikeType constructMapLikeType(Class mapClass, JavaType keyType, Jav // a valid use case here JavaType type = _fromClass(null, mapClass, TypeBindings.createIfNeeded(mapClass, new JavaType[] { keyType, valueType })); - if (type instanceof MapLikeType) { - return (MapLikeType) type; + if (type instanceof MapLikeType mapLikeType) { + return mapLikeType; } return MapLikeType.upgradeFrom(type, keyType, valueType); } @@ -1281,14 +1281,14 @@ else if (srcType instanceof JavaType jt) { // [databind#116] // no need to modify further if we already had JavaType return jt; } - else if (srcType instanceof GenericArrayType) { - resultType = _fromArrayType(context, (GenericArrayType) srcType, bindings); + else if (srcType instanceof GenericArrayType genericArrayType) { + resultType = _fromArrayType(context, genericArrayType, bindings); } - else if (srcType instanceof TypeVariable) { - resultType = _fromVariable(context, (TypeVariable) srcType, bindings); + else if (srcType instanceof TypeVariable typeVariable) { + resultType = _fromVariable(context, typeVariable, bindings); } - else if (srcType instanceof WildcardType) { - resultType = _fromWildcard(context, (WildcardType) srcType, bindings); + else if (srcType instanceof WildcardType wildcardType) { + resultType = _fromWildcard(context, wildcardType, bindings); } else { // sanity check throw new IllegalArgumentException("Unrecognized Type: "+((srcType == null) ? "[null]" : srcType.toString())); diff --git a/src/main/java/tools/jackson/databind/util/ClassUtil.java b/src/main/java/tools/jackson/databind/util/ClassUtil.java index 13ec2f737a..c7f5208d61 100644 --- a/src/main/java/tools/jackson/databind/util/ClassUtil.java +++ b/src/main/java/tools/jackson/databind/util/ClassUtil.java @@ -274,8 +274,8 @@ public static void verifyMustOverride(Class expType, Object instance, * and if so, (re)throw it; otherwise just return */ public static Throwable throwIfError(Throwable t) { - if (t instanceof Error) { - throw (Error) t; + if (t instanceof Error error) { + throw error; } return t; } @@ -285,8 +285,8 @@ public static Throwable throwIfError(Throwable t) { * and if so, (re)throw it; otherwise just return */ public static Throwable throwIfRTE(Throwable t) { - if (t instanceof RuntimeException) { - throw (RuntimeException) t; + if (t instanceof RuntimeException rte) { + throw rte; } return t; } @@ -296,8 +296,8 @@ public static Throwable throwIfRTE(Throwable t) { * and if so, (re)throw it; otherwise just return */ public static Throwable throwIfJacksonE(Throwable t) throws JacksonException { - if (t instanceof JacksonException) { - throw (JacksonException) t; + if (t instanceof JacksonException jacksonException) { + throw jacksonException; } return t; } @@ -402,8 +402,8 @@ public static void closeOnFailAndThrowAsJacksonE(JsonGenerator g, Exception fail } throwIfJacksonE(fail); throwIfRTE(fail); - if (fail instanceof IOException) { - throw JacksonIOException.construct((IOException) fail, g); + if (fail instanceof IOException ioException) { + throw JacksonIOException.construct(ioException, g); } throw new RuntimeException(fail); } @@ -436,8 +436,8 @@ public static void closeOnFailAndThrowAsJacksonE(JsonGenerator g, } throwIfJacksonE(fail); throwIfRTE(fail); - if (fail instanceof IOException) { - throw JacksonIOException.construct((IOException) fail, g); + if (fail instanceof IOException ioException) { + throw JacksonIOException.construct(ioException, g); } throw new RuntimeException(fail); } @@ -561,8 +561,8 @@ public static String getClassDescription(Object classOrInstance) if (classOrInstance == null) { return "unknown"; } - Class cls = (classOrInstance instanceof Class) ? - (Class) classOrInstance : classOrInstance.getClass(); + Class cls = (classOrInstance instanceof Class clsInstance) ? + clsInstance : classOrInstance.getClass(); return nameOf(cls); } @@ -605,7 +605,7 @@ public static String classNameOf(Object inst) { if (inst == null) { return "[null]"; } - Class raw = (inst instanceof Class) ? (Class) inst : inst.getClass(); + Class raw = (inst instanceof Class clsInstance) ? clsInstance : inst.getClass(); return nameOf(raw); } @@ -702,8 +702,8 @@ public static String apostrophed(String text) { * in message when wrapping exceptions. */ public static String exceptionMessage(Throwable t) { - if (t instanceof JacksonException) { - return ((JacksonException) t).getOriginalMessage(); + if (t instanceof JacksonException jacksonException) { + return jacksonException.getOriginalMessage(); } if (t instanceof InvocationTargetException && t.getCause() != null) { return t.getCause().getMessage(); diff --git a/src/main/java/tools/jackson/databind/util/ExceptionUtil.java b/src/main/java/tools/jackson/databind/util/ExceptionUtil.java index 0e7e4769fc..2f2e2ce0d0 100644 --- a/src/main/java/tools/jackson/databind/util/ExceptionUtil.java +++ b/src/main/java/tools/jackson/databind/util/ExceptionUtil.java @@ -27,11 +27,11 @@ private ExceptionUtil() {} */ public static void rethrowIfFatal(Throwable throwable) throws Error, RuntimeException { if (isFatal(throwable)) { - if (throwable instanceof Error) { - throw (Error) throwable; + if (throwable instanceof Error error) { + throw error; } - if (throwable instanceof RuntimeException) { - throw (RuntimeException) throwable; + if (throwable instanceof RuntimeException runtimeException) { + throw runtimeException; } throw new RuntimeException(throwable); } diff --git a/src/main/java/tools/jackson/databind/util/RawValue.java b/src/main/java/tools/jackson/databind/util/RawValue.java index 776d712a8a..21476728c4 100644 --- a/src/main/java/tools/jackson/databind/util/RawValue.java +++ b/src/main/java/tools/jackson/databind/util/RawValue.java @@ -57,8 +57,8 @@ public Object rawValue() { @Override public void serialize(JsonGenerator gen, SerializationContext serializers) throws JacksonException { - if (_value instanceof JacksonSerializable) { - ((JacksonSerializable) _value).serialize(gen, serializers); + if (_value instanceof JacksonSerializable jacksonSerializable) { + jacksonSerializable.serialize(gen, serializers); } else { _serialize(gen); } @@ -68,8 +68,8 @@ public void serialize(JsonGenerator gen, SerializationContext serializers) throw public void serializeWithType(JsonGenerator gen, SerializationContext serializers, TypeSerializer typeSer) throws JacksonException { - if (_value instanceof JacksonSerializable) { - ((JacksonSerializable) _value).serializeWithType(gen, serializers, typeSer); + if (_value instanceof JacksonSerializable jacksonSerializable) { + jacksonSerializable.serializeWithType(gen, serializers, typeSer); } else if (_value instanceof SerializableString) { /* Since these are not really to be deserialized (with or without type info), * just re-route as regular write, which will create one... hopefully it works @@ -90,8 +90,8 @@ public void serialize(JsonGenerator gen) throws JacksonException protected void _serialize(JsonGenerator gen) throws JacksonException { - if (_value instanceof SerializableString) { - gen.writeRawValue((SerializableString) _value); + if (_value instanceof SerializableString serializableString) { + gen.writeRawValue(serializableString); } else { gen.writeRawValue(String.valueOf(_value)); } @@ -100,13 +100,13 @@ protected void _serialize(JsonGenerator gen) throws JacksonException @Override public boolean equals(Object o) { if (o == this) return true; - if (!(o instanceof RawValue)) return false; - RawValue other = (RawValue) o; - - if (_value == other._value) { - return true; + if (o instanceof RawValue other) { + if (_value == other._value) { + return true; + } + return (_value != null) && _value.equals(other._value); } - return (_value != null) && _value.equals(other._value); + return false; } @Override diff --git a/src/main/java/tools/jackson/databind/util/TokenBuffer.java b/src/main/java/tools/jackson/databind/util/TokenBuffer.java index d7b2726381..b7bf7fe1f9 100644 --- a/src/main/java/tools/jackson/databind/util/TokenBuffer.java +++ b/src/main/java/tools/jackson/databind/util/TokenBuffer.java @@ -193,8 +193,8 @@ protected TokenBuffer(JsonParser p, ObjectReadContext ctxt) _hasNativeTypeIds = p.canReadTypeId(); _hasNativeObjectIds = p.canReadObjectId(); _mayHaveNativeIds = _hasNativeTypeIds || _hasNativeObjectIds; - if (ctxt instanceof DeserializationContext) { - _forceBigDecimal = ((DeserializationContext) ctxt).isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); + if (ctxt instanceof DeserializationContext deserializationContext) { + _forceBigDecimal = deserializationContext.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); } else { _forceBigDecimal = false; } @@ -470,8 +470,8 @@ public void serialize(JsonGenerator gen) throws JacksonException { // 13-Dec-2010, tatu: Maybe we should start using different type tokens to reduce casting? Object ob = segment.get(ptr); - if (ob instanceof SerializableString) { - gen.writeName((SerializableString) ob); + if (ob instanceof SerializableString str) { + gen.writeName(str); } else { gen.writeName((String) ob); } @@ -480,8 +480,8 @@ public void serialize(JsonGenerator gen) throws JacksonException case VALUE_STRING: { Object ob = segment.get(ptr); - if (ob instanceof SerializableString) { - gen.writeString((SerializableString) ob); + if (ob instanceof SerializableString str) { + gen.writeString(str); } else { gen.writeString((String) ob); } @@ -490,14 +490,14 @@ public void serialize(JsonGenerator gen) throws JacksonException case VALUE_NUMBER_INT: { Object n = segment.get(ptr); - if (n instanceof Integer) { - gen.writeNumber((Integer) n); - } else if (n instanceof BigInteger) { - gen.writeNumber((BigInteger) n); - } else if (n instanceof Long) { - gen.writeNumber((Long) n); - } else if (n instanceof Short) { - gen.writeNumber((Short) n); + if (n instanceof Integer i) { + gen.writeNumber(i); + } else if (n instanceof BigInteger bi) { + gen.writeNumber(bi); + } else if (n instanceof Long l) { + gen.writeNumber(l); + } else if (n instanceof Short s) { + gen.writeNumber(s); } else { gen.writeNumber(((Number) n).intValue()); } @@ -506,16 +506,16 @@ public void serialize(JsonGenerator gen) throws JacksonException case VALUE_NUMBER_FLOAT: { Object n = segment.get(ptr); - if (n instanceof Double) { - gen.writeNumber((Double) n); - } else if (n instanceof BigDecimal) { - gen.writeNumber((BigDecimal) n); - } else if (n instanceof Float) { - gen.writeNumber((Float) n); + if (n instanceof Double d) { + gen.writeNumber(d); + } else if (n instanceof BigDecimal bd) { + gen.writeNumber(bd); + } else if (n instanceof Float f) { + gen.writeNumber(f); } else if (n == null) { gen.writeNull(); - } else if (n instanceof String) { - gen.writeNumber((String) n); + } else if (n instanceof String s) { + gen.writeNumber(s); } else { throw new StreamWriteException(gen, String.format( "Unrecognized value type for VALUE_NUMBER_FLOAT: %s, cannot serialize", @@ -538,8 +538,8 @@ public void serialize(JsonGenerator gen) throws JacksonException // 01-Sep-2016, tatu: as per [databind#1361], should use `writeEmbeddedObject()`; // however, may need to consider alternatives for some well-known types // first - if (value instanceof RawValue) { - ((RawValue) value).serialize(gen); + if (value instanceof RawValue rawValue) { + rawValue.serialize(gen); } else if (value instanceof JacksonSerializable) { gen.writePOJO(value); } else { @@ -1662,7 +1662,7 @@ public JsonToken nextToken() // Property name? Need to update context if (_currToken == JsonToken.PROPERTY_NAME) { Object ob = _currentObject(); - String name = (ob instanceof String) ? ((String) ob) : ob.toString(); + String name = (ob instanceof String string) ? string : ob.toString(); _parsingContext.setCurrentName(name); } else if (_currToken == JsonToken.START_OBJECT) { _parsingContext = _parsingContext.createChildObjectContext(); @@ -1691,7 +1691,7 @@ public String nextName() _segmentPtr = ptr; _updateToken(JsonToken.PROPERTY_NAME); Object ob = _segment.get(ptr); // inlined _currentObject(); - String name = (ob instanceof String) ? ((String) ob) : ob.toString(); + String name = (ob instanceof String string) ? string : ob.toString(); _parsingContext.setCurrentName(name); return name; } @@ -1756,8 +1756,8 @@ public String getString() if (_currToken == JsonToken.VALUE_STRING || _currToken == JsonToken.PROPERTY_NAME) { Object ob = _currentObject(); - if (ob instanceof String) { - return (String) ob; + if (ob instanceof String string) { + return string; } return ClassUtil.nullOrToString(ob); } @@ -1805,11 +1805,11 @@ public boolean isNaN() { // can only occur for floating-point numbers if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) { Object value = _currentObject(); - if (value instanceof Double) { - return NumberOutput.notFinite((Double) value); + if (value instanceof Double d) { + return NumberOutput.notFinite(d); } - if (value instanceof Float) { - return NumberOutput.notFinite((Float) value); + if (value instanceof Float f) { + return NumberOutput.notFinite(f); } } return false; @@ -1819,10 +1819,9 @@ public boolean isNaN() { public BigInteger getBigIntegerValue() { Number n = _numberValue(NR_BIGINT, true); - if (n instanceof BigInteger) { - return (BigInteger) n; - } else if (n instanceof BigDecimal) { - final BigDecimal bd = (BigDecimal) n; + if (n instanceof BigInteger bi) { + return bi; + } else if (n instanceof BigDecimal bd) { streamReadConstraints().validateBigIntegerScale(bd.scale()); return bd.toBigInteger(); } @@ -1834,14 +1833,14 @@ public BigInteger getBigIntegerValue() public BigDecimal getDecimalValue() { Number n = _numberValue(NR_BIGDECIMAL, true); - if (n instanceof BigDecimal) { - return (BigDecimal) n; + if (n instanceof BigDecimal bd) { + return bd; } else if (n instanceof Integer) { return BigDecimal.valueOf(n.intValue()); } else if (n instanceof Long) { return BigDecimal.valueOf(n.longValue()); - } else if (n instanceof BigInteger) { - return new BigDecimal((BigInteger) n); + } else if (n instanceof BigInteger bi) { + return new BigDecimal(bi); } // float or double return BigDecimal.valueOf(n.doubleValue()); @@ -1884,8 +1883,7 @@ public NumberType getNumberType() return null; } final Object value = _currentObject(); - if (value instanceof Number) { - Number n = (Number) value; + if (value instanceof Number n) { if (n instanceof Integer) return NumberType.INT; if (n instanceof Long) return NumberType.LONG; if (n instanceof Double) return NumberType.DOUBLE; @@ -1932,16 +1930,15 @@ private Number _numberValue(final int targetNumType, final boolean preferBigNumb throw _constructNotNumericType(_currToken, targetNumType); } Object value = _currentObject(); - if (value instanceof Number) { - return (Number) value; + if (value instanceof Number number) { + return number; } // Difficult to really support numbers-as-Strings; but let's try. // NOTE: no access to DeserializationConfig, unfortunately, so cannot // try to determine Double/BigDecimal preference... // 12-Jan-2021, tatu: Is this really needed, and for what? CSV, XML? - if (value instanceof String) { - String str = (String) value; + if (value instanceof String str) { final int len = str.length(); if (_currToken == JsonToken.VALUE_NUMBER_INT) { // 08-Dec-2023, tatu: Note -- deferred numbers' validity (wrt input token) @@ -1995,8 +1992,7 @@ protected int _convertNumberToInt(Number n) throws InputCoercionException } return result; } - if (n instanceof BigInteger) { - BigInteger big = (BigInteger) n; + if (n instanceof BigInteger big) { if (BI_MIN_INT.compareTo(big) > 0 || BI_MAX_INT.compareTo(big) < 0) { _reportOverflowInt(); @@ -2008,8 +2004,7 @@ protected int _convertNumberToInt(Number n) throws InputCoercionException _reportOverflowInt(); } return (int) d; - } else if (n instanceof BigDecimal) { - BigDecimal big = (BigDecimal) n; + } else if (n instanceof BigDecimal big) { if (BD_MIN_INT.compareTo(big) > 0 || BD_MAX_INT.compareTo(big) < 0) { _reportOverflowInt(); @@ -2022,8 +2017,7 @@ protected int _convertNumberToInt(Number n) throws InputCoercionException protected long _convertNumberToLong(Number n) throws InputCoercionException { - if (n instanceof BigInteger) { - BigInteger big = (BigInteger) n; + if (n instanceof BigInteger big) { if (BI_MIN_LONG.compareTo(big) > 0 || BI_MAX_LONG.compareTo(big) < 0) { _reportOverflowLong(); @@ -2035,8 +2029,7 @@ protected long _convertNumberToLong(Number n) throws InputCoercionException _reportOverflowLong(); } return (long) d; - } else if (n instanceof BigDecimal) { - BigDecimal big = (BigDecimal) n; + } else if (n instanceof BigDecimal big) { if (BD_MIN_LONG.compareTo(big) > 0 || BD_MAX_LONG.compareTo(big) < 0) { _reportOverflowLong(); diff --git a/src/main/java/tools/jackson/databind/util/TokenBufferReadContext.java b/src/main/java/tools/jackson/databind/util/TokenBufferReadContext.java index 1ab9a789ab..aafdd65fb4 100644 --- a/src/main/java/tools/jackson/databind/util/TokenBufferReadContext.java +++ b/src/main/java/tools/jackson/databind/util/TokenBufferReadContext.java @@ -35,8 +35,7 @@ protected TokenBufferReadContext(TokenStreamContext base, ContentReference conte _parent = base.getParent(); _currentName = base.currentName(); _currentValue = base.currentValue(); - if (base instanceof JsonReadContext) { - JsonReadContext rc = (JsonReadContext) base; + if (base instanceof JsonReadContext rc) { _startLocation = rc.startLocation(contentRef); } else { _startLocation = TokenStreamLocation.NA; @@ -111,8 +110,8 @@ public TokenBufferReadContext parentOrCopy() { // 30-Apr-2017, tatu: This is bit awkward since part on ancestor stack is of different // type (usually `JsonReadContext`)... and so for unbalanced buffers (with extra // END_OBJECT / END_ARRAY), we may need to create - if (_parent instanceof TokenBufferReadContext) { - return (TokenBufferReadContext) _parent; + if (_parent instanceof TokenBufferReadContext tokenBufferReadContext) { + return tokenBufferReadContext; } if (_parent == null) { // unlikely, but just in case let's support return new TokenBufferReadContext(); diff --git a/src/main/java/tools/jackson/databind/util/internal/LinkedDeque.java b/src/main/java/tools/jackson/databind/util/internal/LinkedDeque.java index 60b6def8a6..070b48c8a8 100644 --- a/src/main/java/tools/jackson/databind/util/internal/LinkedDeque.java +++ b/src/main/java/tools/jackson/databind/util/internal/LinkedDeque.java @@ -190,7 +190,7 @@ public void clear() { @Override public boolean contains(Object o) { - return (o instanceof Linked) && contains((Linked) o); + return (o instanceof Linked linked) && contains(linked); } // A fast-path containment check diff --git a/src/main/java/tools/jackson/databind/util/internal/PrivateMaxEntriesMap.java b/src/main/java/tools/jackson/databind/util/internal/PrivateMaxEntriesMap.java index 9fb15d7e78..c24a82b41c 100644 --- a/src/main/java/tools/jackson/databind/util/internal/PrivateMaxEntriesMap.java +++ b/src/main/java/tools/jackson/databind/util/internal/PrivateMaxEntriesMap.java @@ -1034,12 +1034,11 @@ public Iterator> iterator() { @Override public boolean contains(Object obj) { - if (!(obj instanceof Entry)) { - return false; + if (obj instanceof Entry entry) { + Node node = map.data.get(entry.getKey()); + return (node != null) && (node.getValue().equals(entry.getValue())); } - Entry entry = (Entry) obj; - Node node = map.data.get(entry.getKey()); - return (node != null) && (node.getValue().equals(entry.getValue())); + return false; } @Override @@ -1049,11 +1048,10 @@ public boolean add(Entry entry) { @Override public boolean remove(Object obj) { - if (!(obj instanceof Entry)) { - return false; + if (obj instanceof Entry entry) { + return map.remove(entry.getKey(), entry.getValue()); } - Entry entry = (Entry) obj; - return map.remove(entry.getKey(), entry.getValue()); + return false; } } diff --git a/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java b/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java index 5064bdb8ec..17ce65b27a 100644 --- a/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java +++ b/src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java @@ -285,11 +285,10 @@ public Point(int x, int y) { @Override public boolean equals(Object o) { - if (!(o instanceof Point)) { - return false; + if (o instanceof Point other) { + return (other.x == x) && (other.y == y); } - Point other = (Point) o; - return (other.x == x) && (other.y == y); + return false; } @Override