Skip to content

Commit f28318b

Browse files
authored
use instanceof that avoids explicit cast (#5390)
1 parent ed70776 commit f28318b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+153
-170
lines changed

src/main/java/tools/jackson/databind/DatabindException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public static JacksonException wrapWithPath(DeserializationContext ctxt,
9898

9999
// Copied from JacksonException.wrapWithPath()
100100
JacksonException jme;
101-
if (src instanceof JacksonException) {
102-
jme = (JacksonException) src;
101+
if (src instanceof JacksonException je) {
102+
jme = je;
103103
} else {
104104
// [databind#2128]: try to avoid duplication
105105
String msg = _exceptionMessage(src);
@@ -120,8 +120,8 @@ public static JacksonException wrapWithPath(SerializationContext ctxt,
120120

121121
// Copied from JacksonException.wrapWithPath()
122122
JacksonException jme;
123-
if (src instanceof JacksonException) {
124-
jme = (JacksonException) src;
123+
if (src instanceof JacksonException je) {
124+
jme = je;
125125
} else {
126126
String msg = _exceptionMessage(src);
127127
if (msg == null || msg.isEmpty()) {

src/main/java/tools/jackson/databind/DeserializationContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ public <T> T readValue(JsonParser p, TypeReference<T> refType) throws JacksonExc
374374

375375
@Override
376376
public <T> T readValue(JsonParser p, ResolvedType type) throws JacksonException {
377-
if (!(type instanceof JavaType)) {
378-
throw new UnsupportedOperationException(
379-
"Only support `JavaType` implementation of `ResolvedType`, not: "+type.getClass().getName());
377+
if (type instanceof JavaType jt) {
378+
return readValue(p, jt);
380379
}
381-
return readValue(p, (JavaType) type);
380+
throw new UnsupportedOperationException(
381+
"Only support `JavaType` implementation of `ResolvedType`, not: "+type.getClass().getName());
382382
}
383383

384384
@SuppressWarnings("unchecked")
@@ -869,8 +869,8 @@ public final KeyDeserializer findKeyDeserializer(JavaType keyType,
869869
kd = null;
870870
}
871871
// Second: contextualize?
872-
if (kd instanceof ContextualKeyDeserializer) {
873-
kd = ((ContextualKeyDeserializer) kd).createContextual(this, prop);
872+
if (kd instanceof ContextualKeyDeserializer ckd) {
873+
kd = ckd.createContextual(this, prop);
874874
}
875875
return kd;
876876
}

src/main/java/tools/jackson/databind/InjectableValues.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,26 @@ public Object findInjectableValue(DeserializationContext ctxt,
8585
BeanProperty forProperty, Object beanInstance,
8686
Boolean optional, Boolean useInput)
8787
{
88-
if (!(valueId instanceof String)) {
88+
if (valueId instanceof String key) {
89+
Object ob = _values.get(key);
90+
if (ob == null && !_values.containsKey(key)) {
91+
if (Boolean.FALSE.equals(optional)
92+
|| ((optional == null)
93+
&& ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
94+
throw ctxt.missingInjectableValueException(
95+
String.format("No injectable value with id '%s' found (for property '%s')",
96+
key, forProperty.getName()),
97+
valueId, forProperty, beanInstance);
98+
}
99+
}
100+
return ob;
101+
} else {
89102
throw ctxt.missingInjectableValueException(
90103
String.format(
91104
"Unsupported injectable value id type (%s), expecting String",
92105
ClassUtil.classNameOf(valueId)),
93106
valueId, forProperty, beanInstance);
94107
}
95-
String key = (String) valueId;
96-
Object ob = _values.get(key);
97-
if (ob == null && !_values.containsKey(key)) {
98-
if (Boolean.FALSE.equals(optional)
99-
|| ((optional == null)
100-
&& ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
101-
throw ctxt.missingInjectableValueException(
102-
String.format("No injectable value with id '%s' found (for property '%s')",
103-
key, forProperty.getName()),
104-
valueId, forProperty, beanInstance);
105-
}
106-
}
107-
return ob;
108108
}
109109
}
110110
}

src/main/java/tools/jackson/databind/ObjectMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,8 @@ public <T> T treeToValue(TreeNode n, Class<T> valueType)
12781278
// 20-Apr-2016, tatu: Another thing: for VALUE_EMBEDDED_OBJECT, assume similar
12791279
// short-cut coercion
12801280
if (tt == JsonToken.VALUE_EMBEDDED_OBJECT) {
1281-
if (n instanceof POJONode) {
1282-
Object ob = ((POJONode) n).getPojo();
1281+
if (n instanceof POJONode pNode) {
1282+
Object ob = pNode.getPojo();
12831283
if ((ob == null) || valueType.isInstance(ob)) {
12841284
return (T) ob;
12851285
}
@@ -1312,8 +1312,8 @@ public <T> T treeToValue(TreeNode n, JavaType valueType)
13121312
}
13131313
final JsonToken tt = n.asToken();
13141314
if (tt == JsonToken.VALUE_EMBEDDED_OBJECT) {
1315-
if (n instanceof POJONode) {
1316-
Object ob = ((POJONode) n).getPojo();
1315+
if (n instanceof POJONode pNode) {
1316+
Object ob = pNode.getPojo();
13171317
if ((ob == null) || valueType.isTypeOrSuperTypeOf(ob.getClass())) {
13181318
return (T) ob;
13191319
}

src/main/java/tools/jackson/databind/ObjectWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,9 @@ public Prefetch forRootType(ObjectWriter parent, JavaType newType) {
13281328
try {
13291329
ValueSerializer<Object> ser = ctxt.findTypedValueSerializer(newType, true);
13301330
// Important: for polymorphic types, "unwrap"...
1331-
if (ser instanceof TypeWrappedSerializer) {
1331+
if (ser instanceof TypeWrappedSerializer typeWrappedSerializer) {
13321332
return new Prefetch(newType, null,
1333-
((TypeWrappedSerializer) ser).typeSerializer());
1333+
typeWrappedSerializer.typeSerializer());
13341334
}
13351335
return new Prefetch(newType, ser, null);
13361336
} catch (JacksonException e) {

src/main/java/tools/jackson/databind/SerializationConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ public SerializationConfig withDefaultPrettyPrinter(PrettyPrinter pp) {
474474

475475
public PrettyPrinter constructDefaultPrettyPrinter() {
476476
PrettyPrinter pp = _defaultPrettyPrinter;
477-
if (pp instanceof Instantiatable<?>) {
478-
pp = (PrettyPrinter) ((Instantiatable<?>) pp).createInstance();
477+
if (pp instanceof Instantiatable<?> pInstantiatable) {
478+
pp = (PrettyPrinter) pInstantiatable.createInstance();
479479
}
480480
return pp;
481481
}

src/main/java/tools/jackson/databind/cfg/BaseSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ public ConstructorDetector getConstructorDetector() {
413413

414414
private DateFormat _force(DateFormat df, TimeZone tz)
415415
{
416-
if (df instanceof StdDateFormat) {
417-
return ((StdDateFormat) df).withTimeZone(tz);
416+
if (df instanceof StdDateFormat sdf) {
417+
return sdf.withTimeZone(tz);
418418
}
419419
// we don't know if original format might be shared; better create a clone:
420420
df = (DateFormat) df.clone();

src/main/java/tools/jackson/databind/cfg/GeneratorSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public PrettyPrinter getPrettyPrinter() {
128128
PrettyPrinter pp = prettyPrinter;
129129
if (pp != null) {
130130
// Important! Must return actual instance to use, NOT just blueprint
131-
if (pp instanceof Instantiatable<?>) {
132-
pp = (PrettyPrinter) ((Instantiatable<?>) pp).createInstance();
131+
if (pp instanceof Instantiatable<?> pInstantiatable) {
132+
pp = (PrettyPrinter) pInstantiatable.createInstance();
133133
}
134134
return pp;
135135
}

src/main/java/tools/jackson/databind/deser/BasicDeserializerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
277277

278278
ValueInstantiator inst;
279279

280-
if (instDef instanceof ValueInstantiator) {
281-
return (ValueInstantiator) instDef;
280+
if (instDef instanceof ValueInstantiator valueInstantiator) {
281+
return valueInstantiator;
282282
}
283283
if (!(instDef instanceof Class)) {
284284
throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type "

src/main/java/tools/jackson/databind/deser/BeanDeserializerFactory.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ public ValueDeserializer<Object> buildThrowableDeserializer(DeserializationConte
435435
if (am != null) { // should never be null
436436
SettableBeanProperty causeCreatorProp = builder.findProperty(PropertyName.construct("cause"));
437437
// [databind#4827] : Consider case where sub-classed `Exception` has `JsonCreator` with `cause` parameter
438-
if (causeCreatorProp instanceof CreatorProperty) {
438+
if (causeCreatorProp instanceof CreatorProperty ccCreatorProperty) {
439439
// Set fallback-setter as null, so `fixAccess()` does not happen during build
440-
((CreatorProperty) causeCreatorProp).setFallbackSetter(null);
440+
ccCreatorProperty.setFallbackSetter(null);
441441
} else {
442442
// [databind#3497]: must consider possible PropertyNamingStrategy
443443
String name = "cause";
@@ -466,8 +466,8 @@ public ValueDeserializer<Object> buildThrowableDeserializer(DeserializationConte
466466

467467
// At this point it ought to be a BeanDeserializer; if not, must assume
468468
// it's some other thing that can handle deserialization ok...
469-
if (deserializer instanceof BeanDeserializer) {
470-
deserializer = ThrowableDeserializer.construct(ctxt, (BeanDeserializer) deserializer);
469+
if (deserializer instanceof BeanDeserializer beanDeserializer) {
470+
deserializer = ThrowableDeserializer.construct(ctxt, beanDeserializer);
471471
}
472472

473473
// may have modifier(s) that wants to modify or replace serializer we just built:
@@ -627,8 +627,8 @@ protected void addBeanProps(DeserializationContext ctxt,
627627
CreatorProperty cprop = null;
628628

629629
for (SettableBeanProperty cp : creatorProps) {
630-
if (name.equals(cp.getName()) && (cp instanceof CreatorProperty)) {
631-
cprop = (CreatorProperty) cp;
630+
if (name.equals(cp.getName()) && (cp instanceof CreatorProperty ccCreatorProperty)) {
631+
cprop = ccCreatorProperty;
632632
break;
633633
}
634634
}
@@ -822,9 +822,8 @@ protected SettableAnyProperty constructAnySetter(DeserializationContext ctxt,
822822
final boolean isParameter = mutator instanceof AnnotatedParameter;
823823
int parameterIndex = -1;
824824

825-
if (mutator instanceof AnnotatedMethod) {
825+
if (mutator instanceof AnnotatedMethod am) {
826826
// we know it's a 2-arg method, second arg is the value
827-
AnnotatedMethod am = (AnnotatedMethod) mutator;
828827
keyType = am.getParameterType(0);
829828
valueType = am.getParameterType(1);
830829
// Need to resolve for possible generic types (like Maps, Collections)
@@ -908,9 +907,8 @@ protected SettableAnyProperty constructAnySetter(DeserializationContext ctxt,
908907
if (keyDeser == null) {
909908
keyDeser = ctxt.findKeyDeserializer(keyType, prop);
910909
} else {
911-
if (keyDeser instanceof ContextualKeyDeserializer) {
912-
keyDeser = ((ContextualKeyDeserializer) keyDeser)
913-
.createContextual(ctxt, prop);
910+
if (keyDeser instanceof ContextualKeyDeserializer ckd) {
911+
keyDeser = ckd.createContextual(ctxt, prop);
914912
}
915913
}
916914
ValueDeserializer<Object> deser = findContentDeserializerFromAnnotation(ctxt, mutator);

0 commit comments

Comments
 (0)