From 2bf42320dee38b873be9ec8a1c27018ba4d76a91 Mon Sep 17 00:00:00 2001 From: Dirk Olmes Date: Tue, 5 Nov 2024 09:26:46 +0100 Subject: [PATCH 1/2] chore(sonar): Fix instanceof check and cast with pattern matching in engine-dmn/engine. (#85) --- .../impl/DefaultDmnEngineConfiguration.java | 4 ++-- .../impl/el/VariableContextScriptBindings.java | 8 ++++---- .../evaluation/ExpressionEvaluationHandler.java | 5 ++--- .../AbstractCollectNumberHitPolicyHandler.java | 12 ++++++------ .../metrics/DmnEngineMetricCollectorWrapper.java | 4 ++-- .../impl/type/BooleanDataTypeTransformer.java | 8 ++++---- .../impl/type/DateDataTypeTransformer.java | 16 ++++++++-------- .../impl/type/DoubleDataTypeTransformer.java | 8 ++++---- .../impl/type/IntegerDataTypeTransformer.java | 8 ++++---- .../impl/type/LongDataTypeTransformer.java | 8 ++++---- 10 files changed, 40 insertions(+), 41 deletions(-) diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/DefaultDmnEngineConfiguration.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/DefaultDmnEngineConfiguration.java index 8ea7c208e03..a6eb7f45f3e 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/DefaultDmnEngineConfiguration.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/DefaultDmnEngineConfiguration.java @@ -168,8 +168,8 @@ protected void initDecisionEvaluationListener() { protected Collection getDefaultDmnDecisionEvaluationListeners() { List defaultListeners = new ArrayList<>(); - if (engineMetricCollector instanceof DmnDecisionEvaluationListener) { - defaultListeners.add((DmnDecisionEvaluationListener) engineMetricCollector); + if (engineMetricCollector instanceof DmnDecisionEvaluationListener listener) { + defaultListeners.add(listener); } else { defaultListeners.add(new DmnEngineMetricCollectorWrapper(engineMetricCollector)); } diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/el/VariableContextScriptBindings.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/el/VariableContextScriptBindings.java index 925ee916d46..bf999f1a73f 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/el/VariableContextScriptBindings.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/el/VariableContextScriptBindings.java @@ -54,8 +54,8 @@ public boolean containsKey(Object key) { if(wrappedBindings.containsKey(key)) { return true; } - if (key instanceof String) { - return variableContext.containsVariable((String) key); + if (key instanceof String keyString) { + return variableContext.containsVariable(keyString); } else { return false; @@ -72,8 +72,8 @@ public Object get(Object key) { result = wrappedBindings.get(key); } else { - if (key instanceof String) { - TypedValue resolvedValue = variableContext.resolve((String) key); + if (key instanceof String keyString) { + TypedValue resolvedValue = variableContext.resolve(keyString); result = unpack(resolvedValue); } } diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/evaluation/ExpressionEvaluationHandler.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/evaluation/ExpressionEvaluationHandler.java index 5e3cc0ee57a..f143feb42c5 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/evaluation/ExpressionEvaluationHandler.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/evaluation/ExpressionEvaluationHandler.java @@ -77,15 +77,14 @@ protected Object evaluateScriptExpression(String expressionLanguage, VariableCon bindings.put("variableContext", variableContext); try { - if (scriptEngine instanceof Compilable) { + if (scriptEngine instanceof Compilable compilableScriptEngine) { CompiledScript compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if (compiledScript == null) { synchronized (cachedCompiledScriptSupport) { compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); - if(compiledScript == null) { - Compilable compilableScriptEngine = (Compilable) scriptEngine; + if (compiledScript == null) { compiledScript = compilableScriptEngine.compile(expressionText); cachedCompiledScriptSupport.cacheCompiledScript(compiledScript); diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/hitpolicy/AbstractCollectNumberHitPolicyHandler.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/hitpolicy/AbstractCollectNumberHitPolicyHandler.java index 049b2a75f5e..71d762a9ef7 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/hitpolicy/AbstractCollectNumberHitPolicyHandler.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/hitpolicy/AbstractCollectNumberHitPolicyHandler.java @@ -135,8 +135,8 @@ protected List convertValuesToInteger(List typedValues) thr // check if it is an integer Object value = typedValue.getValue(); - if (value instanceof Integer) { - intValues.add((Integer) value); + if (value instanceof Integer integerValue) { + intValues.add(integerValue); } else { throw new IllegalArgumentException(); @@ -162,8 +162,8 @@ protected List convertValuesToLong(List typedValues) throws Il // check if it is a long or a string of a number Object value = typedValue.getValue(); - if (value instanceof Long) { - longValues.add((Long) value); + if (value instanceof Long longValue) { + longValues.add(longValue); } else { Long longValue = Long.valueOf(value.toString()); @@ -191,8 +191,8 @@ protected List convertValuesToDouble(List typedValues) throw // check if it is a double or a string of a decimal number Object value = typedValue.getValue(); - if (value instanceof Double) { - doubleValues.add((Double) value); + if (value instanceof Double doubleValue) { + doubleValues.add(doubleValue); } else { Double doubleValue = Double.valueOf(value.toString()); diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/metrics/DmnEngineMetricCollectorWrapper.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/metrics/DmnEngineMetricCollectorWrapper.java index cb26c19ccb3..40b0b30b963 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/metrics/DmnEngineMetricCollectorWrapper.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/metrics/DmnEngineMetricCollectorWrapper.java @@ -45,8 +45,8 @@ public void notify(DmnDecisionEvaluationEvent evaluationEvent) { } protected void notifyCollector(DmnDecisionLogicEvaluationEvent evaluationEvent) { - if (evaluationEvent instanceof DmnDecisionTableEvaluationEvent) { - collector.notify((DmnDecisionTableEvaluationEvent) evaluationEvent); + if (evaluationEvent instanceof DmnDecisionTableEvaluationEvent dmnEvent) { + collector.notify(dmnEvent); } // ignore other evaluation events since the collector is implemented as decision table evaluation listener } diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/BooleanDataTypeTransformer.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/BooleanDataTypeTransformer.java index e47b5cc17f3..73130f79103 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/BooleanDataTypeTransformer.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/BooleanDataTypeTransformer.java @@ -30,11 +30,11 @@ public class BooleanDataTypeTransformer implements DmnDataTypeTransformer { @Override public TypedValue transform(Object value) throws IllegalArgumentException { - if (value instanceof Boolean) { - return Variables.booleanValue((Boolean) value); + if (value instanceof Boolean booleanValue) { + return Variables.booleanValue(booleanValue); - } else if (value instanceof String) { - boolean booleanValue = transformString((String) value); + } else if (value instanceof String stringValue) { + boolean booleanValue = transformString(stringValue); return Variables.booleanValue(booleanValue); } else { diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DateDataTypeTransformer.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DateDataTypeTransformer.java index 7c11fa90cdf..a0cc8208ea0 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DateDataTypeTransformer.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DateDataTypeTransformer.java @@ -49,22 +49,22 @@ public class DateDataTypeTransformer implements DmnDataTypeTransformer { @Override public TypedValue transform(Object value) throws IllegalArgumentException { - if (value instanceof Date) { - return Variables.dateValue((Date) value); + if (value instanceof Date dateValue) { + return Variables.dateValue(dateValue); - } else if (value instanceof String) { - Date date = transformString((String) value); + } else if (value instanceof String stringValue) { + Date date = transformString(stringValue); return Variables.dateValue(date); - } if (value instanceof ZonedDateTime) { - Instant instant = ((ZonedDateTime) value).toInstant(); + } if (value instanceof ZonedDateTime zonedDateTimeValue) { + Instant instant = (zonedDateTimeValue).toInstant(); Date date = Date.from(instant); return Variables.dateValue(date); - } else if (value instanceof LocalDateTime) { + } else if (value instanceof LocalDateTime localDateTimeValue) { ZoneId defaultTimeZone = ZoneId.systemDefault(); - Instant instant = ((LocalDateTime) value) + Instant instant = localDateTimeValue .atZone(defaultTimeZone) .toInstant(); diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DoubleDataTypeTransformer.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DoubleDataTypeTransformer.java index c02d88101ee..61e9d34345b 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DoubleDataTypeTransformer.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/DoubleDataTypeTransformer.java @@ -30,12 +30,12 @@ public class DoubleDataTypeTransformer implements DmnDataTypeTransformer { @Override public TypedValue transform(Object value) throws IllegalArgumentException { - if (value instanceof Number) { - double doubleValue = transformNumber((Number) value); + if (value instanceof Number numberValue) { + double doubleValue = transformNumber(numberValue); return Variables.doubleValue(doubleValue); - } else if (value instanceof String) { - double doubleValue = transformString((String) value); + } else if (value instanceof String stringValue) { + double doubleValue = transformString(stringValue); return Variables.doubleValue(doubleValue); } else { diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/IntegerDataTypeTransformer.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/IntegerDataTypeTransformer.java index 300ae45bcf3..e93e3f605e4 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/IntegerDataTypeTransformer.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/IntegerDataTypeTransformer.java @@ -30,12 +30,12 @@ public class IntegerDataTypeTransformer implements DmnDataTypeTransformer { @Override public TypedValue transform(Object value) throws IllegalArgumentException { - if (value instanceof Number) { - int intValue = transformNumber((Number) value); + if (value instanceof Number numberValue) { + int intValue = transformNumber(numberValue); return Variables.integerValue(intValue); - } else if (value instanceof String) { - int intValue = transformString((String) value); + } else if (value instanceof String stringValue) { + int intValue = transformString(stringValue); return Variables.integerValue(intValue); } else { diff --git a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/LongDataTypeTransformer.java b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/LongDataTypeTransformer.java index a9e7d196f52..9c13ce931a0 100644 --- a/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/LongDataTypeTransformer.java +++ b/engine-dmn/engine/src/main/java/org/operaton/bpm/dmn/engine/impl/type/LongDataTypeTransformer.java @@ -31,12 +31,12 @@ public class LongDataTypeTransformer implements DmnDataTypeTransformer { @Override public TypedValue transform(Object value) throws IllegalArgumentException { - if (value instanceof Number) { - long longValue = transformNumber((Number) value); + if (value instanceof Number numberValue) { + long longValue = transformNumber(numberValue); return Variables.longValue(longValue); - } else if (value instanceof String) { - long longValue = transformString((String) value); + } else if (value instanceof String stringValue) { + long longValue = transformString(stringValue); return Variables.longValue(longValue); } else { From 82f1cbdc906f3ebf25f8c98484077289dc98d93c Mon Sep 17 00:00:00 2001 From: Dirk Olmes Date: Fri, 8 Nov 2024 06:58:08 +0100 Subject: [PATCH 2/2] chore(sonar): Fix instanceof check and cast with pattern matching in juel. (#85) --- .../operaton/bpm/impl/juel/AstIdentifier.java | 3 +- .../org/operaton/bpm/impl/juel/Bindings.java | 3 +- .../bpm/impl/juel/BooleanOperations.java | 28 +-- .../bpm/impl/juel/NumberOperations.java | 38 ++--- .../bpm/impl/juel/TypeConverterImpl.java | 160 +++++++++--------- 5 files changed, 115 insertions(+), 117 deletions(-) diff --git a/juel/src/main/java/org/operaton/bpm/impl/juel/AstIdentifier.java b/juel/src/main/java/org/operaton/bpm/impl/juel/AstIdentifier.java index a26dd381fb3..4ca85fb534f 100644 --- a/juel/src/main/java/org/operaton/bpm/impl/juel/AstIdentifier.java +++ b/juel/src/main/java/org/operaton/bpm/impl/juel/AstIdentifier.java @@ -116,8 +116,7 @@ protected Method getMethod(Bindings bindings, ELContext context, Class return if (value == null) { throw new MethodNotFoundException(LocalMessages.get("error.identifier.method.notfound", name)); } - if (value instanceof Method) { - Method method = (Method)value; + if (value instanceof Method method) { if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) { throw new MethodNotFoundException(LocalMessages.get("error.identifier.method.notfound", name)); } diff --git a/juel/src/main/java/org/operaton/bpm/impl/juel/Bindings.java b/juel/src/main/java/org/operaton/bpm/impl/juel/Bindings.java index 2c93a651403..a4ba088e9b4 100644 --- a/juel/src/main/java/org/operaton/bpm/impl/juel/Bindings.java +++ b/juel/src/main/java/org/operaton/bpm/impl/juel/Bindings.java @@ -139,8 +139,7 @@ public T convert(Object value, Class type) { @Override public boolean equals(Object obj) { - if (obj instanceof Bindings) { - Bindings other = (Bindings)obj; + if (obj instanceof Bindings other) { return Arrays.equals(functions, other.functions) && Arrays.equals(variables, other.variables) && converter.equals(other.converter); diff --git a/juel/src/main/java/org/operaton/bpm/impl/juel/BooleanOperations.java b/juel/src/main/java/org/operaton/bpm/impl/juel/BooleanOperations.java index 32073c2280a..bf433034f36 100644 --- a/juel/src/main/java/org/operaton/bpm/impl/juel/BooleanOperations.java +++ b/juel/src/main/java/org/operaton/bpm/impl/juel/BooleanOperations.java @@ -56,11 +56,11 @@ private static final boolean lt0(TypeConverter converter, Object o1, Object o2) if (t1 == String.class || t2 == String.class) { return converter.convert(o1, String.class).compareTo(converter.convert(o2, String.class)) < 0; } - if (o1 instanceof Comparable) { - return ((Comparable)o1).compareTo(o2) < 0; + if (o1 instanceof Comparable comparable) { + return comparable.compareTo(o2) < 0; } - if (o2 instanceof Comparable) { - return ((Comparable)o2).compareTo(o1) > 0; + if (o2 instanceof Comparable comparable) { + return comparable.compareTo(o1) > 0; } throw new ELException(LocalMessages.get("error.compare.types", o1.getClass(), o2.getClass())); } @@ -84,11 +84,11 @@ private static final boolean gt0(TypeConverter converter, Object o1, Object o2) if (t1 == String.class || t2 == String.class) { return converter.convert(o1, String.class).compareTo(converter.convert(o2, String.class)) > 0; } - if (o1 instanceof Comparable) { - return ((Comparable)o1).compareTo(o2) > 0; + if (o1 instanceof Comparable comparable) { + return comparable.compareTo(o2) > 0; } - if (o2 instanceof Comparable) { - return ((Comparable)o2).compareTo(o1) < 0; + if (o2 instanceof Comparable comparable) { + return comparable.compareTo(o1) < 0; } throw new ELException(LocalMessages.get("error.compare.types", o1.getClass(), o2.getClass())); } @@ -177,14 +177,14 @@ public static final boolean empty(TypeConverter converter, Object o) { if (o == null || "".equals(o)) { return true; } - if (o instanceof Object[]) { - return ((Object[])o).length == 0; + if (o instanceof Object[] array) { + return array.length == 0; } - if (o instanceof Map) { - return ((Map)o).isEmpty(); + if (o instanceof Map map) { + return map.isEmpty(); } - if (o instanceof Collection) { - return ((Collection)o).isEmpty(); + if (o instanceof Collection collection) { + return collection.isEmpty(); } return false; } diff --git a/juel/src/main/java/org/operaton/bpm/impl/juel/NumberOperations.java b/juel/src/main/java/org/operaton/bpm/impl/juel/NumberOperations.java index 9894b2b3ad5..66215b68cba 100644 --- a/juel/src/main/java/org/operaton/bpm/impl/juel/NumberOperations.java +++ b/juel/src/main/java/org/operaton/bpm/impl/juel/NumberOperations.java @@ -41,7 +41,7 @@ private final static boolean isDotEe(String value) { } private final static boolean isDotEe(Object value) { - return value instanceof String && isDotEe((String)value); + return value instanceof String stringValue && isDotEe(stringValue); } private static final boolean isFloatOrDouble(Object value) { @@ -144,35 +144,35 @@ public static final Number neg(TypeConverter converter, Object value) { if (value == null) { return LONG_ZERO; } - if (value instanceof BigDecimal) { - return ((BigDecimal)value).negate(); + if (value instanceof BigDecimal bigDecimal) { + return bigDecimal.negate(); } - if (value instanceof BigInteger) { - return ((BigInteger)value).negate(); + if (value instanceof BigInteger bigInteger) { + return bigInteger.negate(); } - if (value instanceof Double) { - return -((Double) value).doubleValue(); + if (value instanceof Double doubleValue) { + return -doubleValue.doubleValue(); } - if (value instanceof Float) { - return -((Float) value).floatValue(); + if (value instanceof Float floatValue) { + return -floatValue.floatValue(); } - if (value instanceof String) { - if (isDotEe((String)value)) { + if (value instanceof String stringValue) { + if (isDotEe(stringValue)) { return -converter.convert(value, Double.class).doubleValue(); } return -converter.convert(value, Long.class).longValue(); } - if (value instanceof Long) { - return -((Long) value).longValue(); + if (value instanceof Long longValue) { + return -longValue.longValue(); } - if (value instanceof Integer) { - return -((Integer) value).intValue(); + if (value instanceof Integer integerValue) { + return -integerValue.intValue(); } - if (value instanceof Short) { - return (short) -((Short) value).shortValue(); + if (value instanceof Short shortValue) { + return (short) -shortValue.shortValue(); } - if (value instanceof Byte) { - return (byte) -((Byte) value).byteValue(); + if (value instanceof Byte byteValue) { + return (byte) -byteValue.byteValue(); } throw new ELException(LocalMessages.get("error.negate", value.getClass())); } diff --git a/juel/src/main/java/org/operaton/bpm/impl/juel/TypeConverterImpl.java b/juel/src/main/java/org/operaton/bpm/impl/juel/TypeConverterImpl.java index 9c140b49fcd..391e5226632 100644 --- a/juel/src/main/java/org/operaton/bpm/impl/juel/TypeConverterImpl.java +++ b/juel/src/main/java/org/operaton/bpm/impl/juel/TypeConverterImpl.java @@ -44,14 +44,14 @@ protected Character coerceToCharacter(Object value) { if (value == null || "".equals(value)) { return (char) 0; } - if (value instanceof Character) { - return (Character) value; + if (value instanceof Character character) { + return character; } - if (value instanceof Number) { - return (char) ((Number) value).shortValue(); + if (value instanceof Number number) { + return (char) number.shortValue(); } - if (value instanceof String) { - return ((String) value).charAt(0); + if (value instanceof String string) { + return string.charAt(0); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Character.class)); } @@ -60,24 +60,24 @@ protected BigDecimal coerceToBigDecimal(Object value) { if (value == null || "".equals(value)) { return BigDecimal.valueOf(0l); } - if (value instanceof BigDecimal) { - return (BigDecimal) value; + if (value instanceof BigDecimal bigDecimal) { + return bigDecimal; } - if (value instanceof BigInteger) { - return new BigDecimal((BigInteger) value); + if (value instanceof BigInteger bigInteger) { + return new BigDecimal(bigInteger); } - if (value instanceof Number) { - return new BigDecimal(((Number) value).doubleValue()); + if (value instanceof Number number) { + return BigDecimal.valueOf(number.doubleValue()); } - if (value instanceof String) { + if (value instanceof String string) { try { - return new BigDecimal((String) value); + return new BigDecimal(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, BigDecimal.class)); } } - if (value instanceof Character) { - return new BigDecimal((short) ((Character) value).charValue()); + if (value instanceof Character character) { + return new BigDecimal((short) character.charValue()); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), BigDecimal.class)); } @@ -86,24 +86,24 @@ protected BigInteger coerceToBigInteger(Object value) { if (value == null || "".equals(value)) { return BigInteger.valueOf(0l); } - if (value instanceof BigInteger) { - return (BigInteger) value; + if (value instanceof BigInteger bigInteger) { + return bigInteger; } - if (value instanceof BigDecimal) { - return ((BigDecimal) value).toBigInteger(); + if (value instanceof BigDecimal bigDecimal) { + return bigDecimal.toBigInteger(); } - if (value instanceof Number) { - return BigInteger.valueOf(((Number) value).longValue()); + if (value instanceof Number number) { + return BigInteger.valueOf(number.longValue()); } - if (value instanceof String) { + if (value instanceof String string) { try { - return new BigInteger((String) value); + return new BigInteger(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, BigInteger.class)); } } - if (value instanceof Character) { - return BigInteger.valueOf((short) ((Character) value).charValue()); + if (value instanceof Character character) { + return BigInteger.valueOf((short) character.charValue()); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), BigInteger.class)); } @@ -112,21 +112,21 @@ protected Double coerceToDouble(Object value) { if (value == null || "".equals(value)) { return (double) 0; } - if (value instanceof Double) { - return (Double) value; + if (value instanceof Double doubleValue) { + return doubleValue; } - if (value instanceof Number) { - return ((Number) value).doubleValue(); + if (value instanceof Number number) { + return number.doubleValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Double.valueOf((String) value); + return Double.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Double.class)); } } - if (value instanceof Character) { - return (double) (short) ((Character) value).charValue(); + if (value instanceof Character character) { + return (double) (short) character.charValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Double.class)); } @@ -135,21 +135,21 @@ protected Float coerceToFloat(Object value) { if (value == null || "".equals(value)) { return (float) 0; } - if (value instanceof Float) { - return (Float) value; + if (value instanceof Float floatValue) { + return floatValue; } - if (value instanceof Number) { - return ((Number) value).floatValue(); + if (value instanceof Number number) { + return number.floatValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Float.valueOf((String) value); + return Float.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Float.class)); } } - if (value instanceof Character) { - return (float) (short) ((Character) value).charValue(); + if (value instanceof Character character) { + return (float) (short) character.charValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Float.class)); } @@ -158,21 +158,21 @@ protected Long coerceToLong(Object value) { if (value == null || "".equals(value)) { return 0l; } - if (value instanceof Long) { - return (Long) value; + if (value instanceof Long longValue) { + return longValue; } - if (value instanceof Number) { - return ((Number) value).longValue(); + if (value instanceof Number number) { + return number.longValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Long.valueOf((String) value); + return Long.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Long.class)); } } - if (value instanceof Character) { - return (long) (short) ((Character) value).charValue(); + if (value instanceof Character character) { + return (long) (short) character.charValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Long.class)); } @@ -181,21 +181,21 @@ protected Integer coerceToInteger(Object value) { if (value == null || "".equals(value)) { return 0; } - if (value instanceof Integer) { - return (Integer) value; + if (value instanceof Integer integerValue) { + return integerValue; } - if (value instanceof Number) { - return ((Number) value).intValue(); + if (value instanceof Number number) { + return number.intValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Integer.valueOf((String) value); + return Integer.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Integer.class)); } } - if (value instanceof Character) { - return (int) (short) ((Character) value).charValue(); + if (value instanceof Character character) { + return (int) (short) character.charValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Integer.class)); } @@ -204,21 +204,21 @@ protected Short coerceToShort(Object value) { if (value == null || "".equals(value)) { return (short) 0; } - if (value instanceof Short) { - return (Short) value; + if (value instanceof Short shortValue) { + return shortValue; } - if (value instanceof Number) { - return ((Number) value).shortValue(); + if (value instanceof Number number) { + return number.shortValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Short.valueOf((String) value); + return Short.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Short.class)); } } - if (value instanceof Character) { - return (short) ((Character) value).charValue(); + if (value instanceof Character character) { + return (short) character.charValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Short.class)); } @@ -227,21 +227,21 @@ protected Byte coerceToByte(Object value) { if (value == null || "".equals(value)) { return (byte) 0; } - if (value instanceof Byte) { - return (Byte) value; + if (value instanceof Byte byteValue) { + return byteValue; } - if (value instanceof Number) { - return ((Number) value).byteValue(); + if (value instanceof Number number) { + return number.byteValue(); } - if (value instanceof String) { + if (value instanceof String string) { try { - return Byte.valueOf((String) value); + return Byte.valueOf(string); } catch (NumberFormatException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, Byte.class)); } } - if (value instanceof Character) { - return Short.valueOf((short) ((Character) value).charValue()).byteValue(); + if (value instanceof Character character) { + return Short.valueOf((short) character.charValue()).byteValue(); } throw new ELException(LocalMessages.get("error.coerce.type", value.getClass(), Byte.class)); } @@ -250,11 +250,11 @@ protected String coerceToString(Object value) { if (value == null) { return ""; } - if (value instanceof String) { - return (String) value; + if (value instanceof String string) { + return string; } - if (value instanceof Enum) { - return ((Enum) value).name(); + if (value instanceof Enum enumValue) { + return enumValue.name(); } return value.toString(); } @@ -267,9 +267,9 @@ protected > T coerceToEnum(Object value, Class type) { if (type.isInstance(value)) { return (T) value; } - if (value instanceof String) { + if (value instanceof String string) { try { - return Enum.valueOf(type, (String) value); + return Enum.valueOf(type, string); } catch (IllegalArgumentException e) { throw new ELException(LocalMessages.get("error.coerce.value", value, type)); }