From e29b10125cbf55989055e820b798b7fc4d4483aa Mon Sep 17 00:00:00 2001 From: CBorowski-dev <57234776+CBorowski-dev@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:17:10 +0100 Subject: [PATCH] chore(commons): Fix instanceof check and cast with pattern matching in commons. [#85] * Fix instanceof check and cast with pattern matching in clients. (#85) * Fix instanceof check and cast with pattern matching in commons. (#85) * Fix instanceof check and cast with pattern matching in clients. (#85) --------- Co-authored-by: CBorowski-dev --- .../org/operaton/bpm/engine/variable/Variables.java | 11 +++++------ .../bpm/engine/variable/impl/VariableMapImpl.java | 4 ++-- .../variable/impl/type/AbstractValueTypeImpl.java | 6 +++--- .../engine/variable/impl/type/FileValueTypeImpl.java | 12 ++++++------ .../engine/variable/impl/type/ObjectTypeImpl.java | 4 ++-- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/Variables.java b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/Variables.java index e2b3de2ab24..7a5f6b335f9 100644 --- a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/Variables.java +++ b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/Variables.java @@ -146,8 +146,8 @@ public static VariableMap createVariables() { * it is returned as is. */ public static VariableMap fromMap(Map map) { - if(map instanceof VariableMap) { - return (VariableMap) map; + if(map instanceof VariableMap variableMap) { + return variableMap; } else { return new VariableMapImpl(map); @@ -364,8 +364,8 @@ public static TypedValue untypedValue(Object value) { if (untypedValue instanceof TypedValueBuilder) { untypedValue = ((TypedValueBuilder) untypedValue).create(); } - if (untypedValue instanceof TypedValue) { - return untypedValue(untypedValue, ((TypedValue) untypedValue).isTransient()); + if (untypedValue instanceof TypedValue typedvalue) { + return untypedValue(typedvalue, typedvalue.isTransient()); } else return untypedValue(untypedValue, false); } @@ -379,8 +379,7 @@ public static TypedValue untypedValue(Object value, boolean isTransient) { return untypedNullValue(isTransient); } else if (value instanceof TypedValueBuilder) { return ((TypedValueBuilder) value).setTransient(isTransient).create(); - } else if (value instanceof TypedValue) { - TypedValue transientValue = (TypedValue) value; + } else if (value instanceof TypedValue transientValue) { if (value instanceof NullValueImpl) { transientValue = untypedNullValue(isTransient); } else if (value instanceof FileValue) { diff --git a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/VariableMapImpl.java b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/VariableMapImpl.java index 220ffb5419e..1c644d3b141 100644 --- a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/VariableMapImpl.java +++ b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/VariableMapImpl.java @@ -147,8 +147,8 @@ public Object remove(Object key) { public void putAll(Map m) { if(m != null) { - if(m instanceof VariableMapImpl) { - variables.putAll(((VariableMapImpl)m).variables); + if(m instanceof VariableMapImpl variableMapImpl) { + variables.putAll(variableMapImpl.variables); } else { for (java.util.Map.Entry entry : m.entrySet()) { diff --git a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/AbstractValueTypeImpl.java b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/AbstractValueTypeImpl.java index e77a71637e4..9ad61e93d76 100644 --- a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/AbstractValueTypeImpl.java +++ b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/AbstractValueTypeImpl.java @@ -91,9 +91,9 @@ public boolean equals(Object obj) { protected Boolean isTransient(Map valueInfo) { if (valueInfo != null && valueInfo.containsKey(VALUE_INFO_TRANSIENT)) { - Object isTransient = valueInfo.get(VALUE_INFO_TRANSIENT); - if (isTransient instanceof Boolean) { - return (Boolean) isTransient; + Object valueInfoTransient = valueInfo.get(VALUE_INFO_TRANSIENT); + if (valueInfoTransient instanceof Boolean isTransient) { + return isTransient; } else { throw new IllegalArgumentException("The property 'transient' should have a value of type 'boolean'."); } diff --git a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/FileValueTypeImpl.java b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/FileValueTypeImpl.java index 1bca03eabd5..e61573d6441 100644 --- a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/FileValueTypeImpl.java +++ b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/FileValueTypeImpl.java @@ -53,12 +53,12 @@ public TypedValue createValue(Object value, Map valueInfo) { throw new IllegalArgumentException("Cannot create file without filename! Please set a name into ValueInfo with key " + VALUE_INFO_FILE_NAME); } FileValueBuilder builder = Variables.fileValue(filename.toString()); - if (value instanceof File) { - builder.file((File) value); - } else if (value instanceof InputStream) { - builder.file((InputStream) value); - } else if (value instanceof byte[]) { - builder.file((byte[]) value); + if (value instanceof File file) { + builder.file(file); + } else if (value instanceof InputStream inputStream) { + builder.file(inputStream); + } else if (value instanceof byte[] byteArray) { + builder.file(byteArray); } else { throw new IllegalArgumentException("Provided value is not of File, InputStream or byte[] type."); } diff --git a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/ObjectTypeImpl.java b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/ObjectTypeImpl.java index d6d47e592bf..230d82de293 100644 --- a/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/ObjectTypeImpl.java +++ b/commons/typed-values/src/main/java/org/operaton/bpm/engine/variable/impl/type/ObjectTypeImpl.java @@ -93,8 +93,8 @@ public SerializableValue createValueFromSerialized(String serializedValue, Map valueInfo) { String objectValueTypeName = (String) valueInfo.get(VALUE_INFO_OBJECT_TYPE_NAME); - if (builder instanceof SerializedObjectValueBuilder) { - ((SerializedObjectValueBuilder) builder).objectTypeName(objectValueTypeName); + if (builder instanceof SerializedObjectValueBuilder serializedObjectValueBuilder) { + serializedObjectValueBuilder.objectTypeName(objectValueTypeName); } String serializationDataFormat = (String) valueInfo.get(VALUE_INFO_SERIALIZATION_DATA_FORMAT);