From 4e6ef82d8f3ae7c587e0f1900ca263fd5d7b7f02 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Oct 2021 19:46:21 +0200 Subject: [PATCH] Apply "instanceof pattern matching" in spring-expression This commit also applies additional clean-up tasks such as the following. - final fields This commit also makes use of java.lang.String.repeat(int) in OptMultiply. This has only been applied to `src/main/java`. --- .../expression/TypedValue.java | 5 +- .../spel/ast/ConstructorReference.java | 11 ++-- .../expression/spel/ast/Indexer.java | 36 ++++++------- .../expression/spel/ast/InlineList.java | 18 +++---- .../expression/spel/ast/InlineMap.java | 30 +++++------ .../expression/spel/ast/OpDec.java | 14 ++--- .../expression/spel/ast/OpDivide.java | 8 ++- .../expression/spel/ast/OpGE.java | 7 +-- .../expression/spel/ast/OpGT.java | 7 +-- .../expression/spel/ast/OpInc.java | 14 ++--- .../expression/spel/ast/OpLE.java | 7 +-- .../expression/spel/ast/OpLT.java | 7 +-- .../expression/spel/ast/OpMinus.java | 52 +++++++++---------- .../expression/spel/ast/OpModulus.java | 9 ++-- .../expression/spel/ast/OpMultiply.java | 16 ++---- .../expression/spel/ast/OpPlus.java | 14 ++--- .../expression/spel/ast/Operator.java | 6 +-- .../expression/spel/ast/OperatorMatches.java | 8 +-- .../expression/spel/ast/OperatorPower.java | 7 +-- .../spel/ast/PropertyOrFieldReference.java | 20 ++++--- .../expression/spel/standard/Tokenizer.java | 10 ++-- .../support/ReflectivePropertyAccessor.java | 42 +++++++-------- .../spel/support/StandardTypeComparator.java | 9 ++-- 23 files changed, 151 insertions(+), 206 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/TypedValue.java b/spring-expression/src/main/java/org/springframework/expression/TypedValue.java index d2d470b72d70..a4f2427cc0ad 100644 --- a/spring-expression/src/main/java/org/springframework/expression/TypedValue.java +++ b/spring-expression/src/main/java/org/springframework/expression/TypedValue.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,10 +85,9 @@ public boolean equals(@Nullable Object other) { if (this == other) { return true; } - if (!(other instanceof TypedValue)) { + if (!(other instanceof TypedValue otherTv)) { return false; } - TypedValue otherTv = (TypedValue) other; // Avoid TypeDescriptor initialization if not necessary return (ObjectUtils.nullSafeEquals(this.value, otherTv.value) && ((this.typeDescriptor == null && otherTv.typeDescriptor == null) || diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java index d42a375410f6..9841cee9ba98 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java @@ -136,8 +136,8 @@ private TypedValue createNewInstance(ExpressionState state) throws EvaluationExc if (ex.getCause() instanceof InvocationTargetException) { // User exception was the root cause - exit now Throwable rootCause = ex.getCause().getCause(); - if (rootCause instanceof RuntimeException) { - throw (RuntimeException) rootCause; + if (rootCause instanceof RuntimeException runtimeException) { + throw runtimeException; } else { String typeName = (String) this.children[0].getValueInternal(state).getValue(); @@ -158,9 +158,9 @@ private TypedValue createNewInstance(ExpressionState state) throws EvaluationExc executorToUse = findExecutorForConstructor(typeName, argumentTypes, state); try { this.cachedExecutor = executorToUse; - if (executorToUse instanceof ReflectiveConstructorExecutor) { + if (executorToUse instanceof ReflectiveConstructorExecutor reflectiveConstructorExecutor) { this.exitTypeDescriptor = CodeFlow.toDescriptor( - ((ReflectiveConstructorExecutor) executorToUse).getConstructor().getDeclaringClass()); + reflectiveConstructorExecutor.getConstructor().getDeclaringClass()); } return executorToUse.execute(state.getEvaluationContext(), arguments); @@ -228,14 +228,13 @@ public String toStringAST() { private TypedValue createArray(ExpressionState state) throws EvaluationException { // First child gives us the array type which will either be a primitive or reference type Object intendedArrayType = getChild(0).getValue(state); - if (!(intendedArrayType instanceof String)) { + if (!(intendedArrayType instanceof String type)) { throw new SpelEvaluationException(getChild(0).getStartPosition(), SpelMessage.TYPE_NAME_EXPECTED_FOR_ARRAY_CONSTRUCTION, FormatHelper.formatClassNameForMessage( intendedArrayType != null ? intendedArrayType.getClass() : null)); } - String type = (String) intendedArrayType; Class componentType; TypeCode arrayTypeCode = TypeCode.forName(type); if (arrayTypeCode == TypeCode.OBJECT) { diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java index 04b502816169..7a9d277ac027 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ * @author Andy Clement * @author Phillip Webb * @author Stephane Nicoll + * @author Sam Brannen * @since 3.0 */ // TODO support multidimensional arrays @@ -121,8 +122,7 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException Object index; // This first part of the if clause prevents a 'double dereference' of the property (SPR-5847) - if (target instanceof Map && (this.children[0] instanceof PropertyOrFieldReference)) { - PropertyOrFieldReference reference = (PropertyOrFieldReference) this.children[0]; + if (target instanceof Map && (this.children[0] instanceof PropertyOrFieldReference reference)) { index = reference.getName(); indexValue = new TypedValue(index); } @@ -148,13 +148,13 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException Assert.state(targetDescriptor != null, "No type descriptor"); // Indexing into a Map - if (target instanceof Map) { + if (target instanceof Map map) { Object key = index; if (targetDescriptor.getMapKeyTypeDescriptor() != null) { key = state.convertValue(key, targetDescriptor.getMapKeyTypeDescriptor()); } this.indexedType = IndexedType.MAP; - return new MapIndexingValueRef(state.getTypeConverter(), (Map) target, key, targetDescriptor); + return new MapIndexingValueRef(state.getTypeConverter(), map, key, targetDescriptor); } // If the object is something that looks indexable by an integer, @@ -275,8 +275,7 @@ else if (this.indexedType == IndexedType.MAP) { mv.visitTypeInsn(CHECKCAST, "java/util/Map"); // Special case when the key is an unquoted string literal that will be parsed as // a property/field reference - if ((this.children[0] instanceof PropertyOrFieldReference)) { - PropertyOrFieldReference reference = (PropertyOrFieldReference) this.children[0]; + if ((this.children[0] instanceof PropertyOrFieldReference reference)) { String mapKeyName = reference.getName(); mv.visitLdcInsn(mapKeyName); } @@ -306,9 +305,9 @@ else if (this.indexedType == IndexedType.OBJECT) { } } - if (member instanceof Method) { + if (member instanceof Method method) { mv.visitMethodInsn((isStatic? INVOKESTATIC : INVOKEVIRTUAL), classDesc, member.getName(), - CodeFlow.createSignatureDescriptor((Method) member), false); + CodeFlow.createSignatureDescriptor(method), false); } else { mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, member.getName(), @@ -572,19 +571,17 @@ public TypedValue getValue() { targetObjectRuntimeClass, this.evaluationContext.getPropertyAccessors()); for (PropertyAccessor accessor : accessorsToTry) { if (accessor.canRead(this.evaluationContext, this.targetObject, this.name)) { - if (accessor instanceof ReflectivePropertyAccessor) { - accessor = ((ReflectivePropertyAccessor) accessor).createOptimalAccessor( + if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) { + accessor = reflectivePropertyAccessor.createOptimalAccessor( this.evaluationContext, this.targetObject, this.name); } Indexer.this.cachedReadAccessor = accessor; Indexer.this.cachedReadName = this.name; Indexer.this.cachedReadTargetType = targetObjectRuntimeClass; - if (accessor instanceof ReflectivePropertyAccessor.OptimalPropertyAccessor) { - ReflectivePropertyAccessor.OptimalPropertyAccessor optimalAccessor = - (ReflectivePropertyAccessor.OptimalPropertyAccessor) accessor; + if (accessor instanceof ReflectivePropertyAccessor.OptimalPropertyAccessor optimalAccessor) { Member member = optimalAccessor.member; - Indexer.this.exitTypeDescriptor = CodeFlow.toDescriptor(member instanceof Method ? - ((Method) member).getReturnType() : ((Field) member).getType()); + Indexer.this.exitTypeDescriptor = CodeFlow.toDescriptor(member instanceof Method method ? + method.getReturnType() : ((Field) member).getType()); } return accessor.read(this.evaluationContext, this.targetObject, this.name); } @@ -665,8 +662,8 @@ public CollectionIndexingValueRef(Collection collection, int index, TypeDescript @Override public TypedValue getValue() { growCollectionIfNecessary(); - if (this.collection instanceof List) { - Object o = ((List) this.collection).get(this.index); + if (this.collection instanceof List list) { + Object o = list.get(this.index); exitTypeDescriptor = CodeFlow.toDescriptor(Object.class); return new TypedValue(o, this.collectionEntryDescriptor.elementTypeDescriptor(o)); } @@ -683,8 +680,7 @@ public TypedValue getValue() { @Override public void setValue(@Nullable Object newValue) { growCollectionIfNecessary(); - if (this.collection instanceof List) { - List list = (List) this.collection; + if (this.collection instanceof List list) { if (this.collectionEntryDescriptor.getElementTypeDescriptor() != null) { newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue), this.collectionEntryDescriptor.getElementTypeDescriptor()); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java index 4fa298eb5a3d..8abf63ec6ddf 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * Represent a list in an expression, e.g. '{1,2,3}' * * @author Andy Clement + * @author Sam Brannen * @since 3.0.4 */ public class InlineList extends SpelNodeImpl { @@ -59,8 +60,7 @@ private void checkIfConstant() { for (int c = 0, max = getChildCount(); c < max; c++) { SpelNode child = getChild(c); if (!(child instanceof Literal)) { - if (child instanceof InlineList) { - InlineList inlineList = (InlineList) child; + if (child instanceof InlineList inlineList) { if (!inlineList.isConstant()) { isConstant = false; } @@ -75,11 +75,11 @@ private void checkIfConstant() { int childcount = getChildCount(); for (int c = 0; c < childcount; c++) { SpelNode child = getChild(c); - if ((child instanceof Literal)) { - constantList.add(((Literal) child).getLiteralValue().getValue()); + if (child instanceof Literal literal) { + constantList.add(literal.getLiteralValue().getValue()); } - else if (child instanceof InlineList) { - constantList.add(((InlineList) child).getConstantValue()); + else if (child instanceof InlineList inlineList) { + constantList.add(inlineList.getConstantValue()); } } this.constant = new TypedValue(Collections.unmodifiableList(constantList)); @@ -164,8 +164,8 @@ void generateClinitCode(String clazzname, String constantFieldName, MethodVisito // The children might be further lists if they are not constants. In this // situation do not call back into generateCode() because it will register another clinit adder. // Instead, directly build the list here: - if (this.children[c] instanceof InlineList) { - ((InlineList)this.children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true); + if (this.children[c] instanceof InlineList inlineList) { + inlineList.generateClinitCode(clazzname, constantFieldName, mv, codeflow, true); } else { this.children[c].generateCode(mv, codeflow); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java index c70759b03fe1..7f63c356885b 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java @@ -31,6 +31,7 @@ * Represent a map in an expression, e.g. '{name:'foo',age:12}' * * @author Andy Clement + * @author Sam Brannen * @since 4.1 */ public class InlineMap extends SpelNodeImpl { @@ -56,15 +57,13 @@ private void checkIfConstant() { for (int c = 0, max = getChildCount(); c < max; c++) { SpelNode child = getChild(c); if (!(child instanceof Literal)) { - if (child instanceof InlineList) { - InlineList inlineList = (InlineList) child; + if (child instanceof InlineList inlineList) { if (!inlineList.isConstant()) { isConstant = false; break; } } - else if (child instanceof InlineMap) { - InlineMap inlineMap = (InlineMap) child; + else if (child instanceof InlineMap inlineMap) { if (!inlineMap.isConstant()) { isConstant = false; break; @@ -84,23 +83,23 @@ else if (!(c % 2 == 0 && child instanceof PropertyOrFieldReference)) { SpelNode valueChild = getChild(c); Object key = null; Object value = null; - if (keyChild instanceof Literal) { - key = ((Literal) keyChild).getLiteralValue().getValue(); + if (keyChild instanceof Literal literal) { + key = literal.getLiteralValue().getValue(); } - else if (keyChild instanceof PropertyOrFieldReference) { - key = ((PropertyOrFieldReference) keyChild).getName(); + else if (keyChild instanceof PropertyOrFieldReference propertyOrFieldReference) { + key = propertyOrFieldReference.getName(); } else { return; } - if (valueChild instanceof Literal) { - value = ((Literal) valueChild).getLiteralValue().getValue(); + if (valueChild instanceof Literal literal) { + value = literal.getLiteralValue().getValue(); } - else if (valueChild instanceof InlineList) { - value = ((InlineList) valueChild).getConstantValue(); + else if (valueChild instanceof InlineList inlineList) { + value = inlineList.getConstantValue(); } - else if (valueChild instanceof InlineMap) { - value = ((InlineMap) valueChild).getConstantValue(); + else if (valueChild instanceof InlineMap inlineMap) { + value = inlineMap.getConstantValue(); } constantMap.put(key, value); } @@ -120,8 +119,7 @@ public TypedValue getValueInternal(ExpressionState expressionState) throws Evalu // TODO allow for key being PropertyOrFieldReference like Indexer on maps SpelNode keyChild = getChild(c++); Object key = null; - if (keyChild instanceof PropertyOrFieldReference) { - PropertyOrFieldReference reference = (PropertyOrFieldReference) keyChild; + if (keyChild instanceof PropertyOrFieldReference reference) { key = reference.getName(); } else { diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java index 9bab5a1e381b..d61e8d641062 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Giovanni Dall'Oglio Risso + * @author Sam Brannen * @since 3.2 */ public class OpDec extends Operator { @@ -60,10 +61,9 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep TypedValue returnValue = operandTypedValue; TypedValue newValue = null; - if (operandValue instanceof Number) { - Number op1 = (Number) operandValue; - if (op1 instanceof BigDecimal) { - newValue = new TypedValue(((BigDecimal) op1).subtract(BigDecimal.ONE), operandTypedValue.getTypeDescriptor()); + if (operandValue instanceof Number op1) { + if (op1 instanceof BigDecimal bigDecimal) { + newValue = new TypedValue(bigDecimal.subtract(BigDecimal.ONE), operandTypedValue.getTypeDescriptor()); } else if (op1 instanceof Double) { newValue = new TypedValue(op1.doubleValue() - 1.0d, operandTypedValue.getTypeDescriptor()); @@ -71,8 +71,8 @@ else if (op1 instanceof Double) { else if (op1 instanceof Float) { newValue = new TypedValue(op1.floatValue() - 1.0f, operandTypedValue.getTypeDescriptor()); } - else if (op1 instanceof BigInteger) { - newValue = new TypedValue(((BigInteger) op1).subtract(BigInteger.ONE), operandTypedValue.getTypeDescriptor()); + else if (op1 instanceof BigInteger bigInteger) { + newValue = new TypedValue(bigInteger.subtract(BigInteger.ONE), operandTypedValue.getTypeDescriptor()); } else if (op1 instanceof Long) { newValue = new TypedValue(op1.longValue() - 1L, operandTypedValue.getTypeDescriptor()); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java index 02eae2f48c85..f555d75f3209 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Giovanni Dall'Oglio Risso + * @author Sam Brannen * @since 3.0 */ public class OpDivide extends Operator { @@ -49,10 +50,7 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); - if (leftOperand instanceof Number && rightOperand instanceof Number) { - Number leftNumber = (Number) leftOperand; - Number rightNumber = (Number) rightOperand; - + if (leftOperand instanceof Number leftNumber && rightOperand instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java index 708e1b89c593..8dea989339f8 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,10 +50,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java index 3a4b2fd70889..7823bf888a57 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,10 +50,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java index ee19b59988ca..f6dc184c0f5e 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Giovanni Dall'Oglio Risso + * @author Sam Brannen * @since 3.2 */ public class OpInc extends Operator { @@ -58,10 +59,9 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep TypedValue returnValue = typedValue; TypedValue newValue = null; - if (value instanceof Number) { - Number op1 = (Number) value; - if (op1 instanceof BigDecimal) { - newValue = new TypedValue(((BigDecimal) op1).add(BigDecimal.ONE), typedValue.getTypeDescriptor()); + if (value instanceof Number op1) { + if (op1 instanceof BigDecimal bigDecimal) { + newValue = new TypedValue(bigDecimal.add(BigDecimal.ONE), typedValue.getTypeDescriptor()); } else if (op1 instanceof Double) { newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor()); @@ -69,8 +69,8 @@ else if (op1 instanceof Double) { else if (op1 instanceof Float) { newValue = new TypedValue(op1.floatValue() + 1.0f, typedValue.getTypeDescriptor()); } - else if (op1 instanceof BigInteger) { - newValue = new TypedValue(((BigInteger) op1).add(BigInteger.ONE), typedValue.getTypeDescriptor()); + else if (op1 instanceof BigInteger bigInteger) { + newValue = new TypedValue(bigInteger.add(BigInteger.ONE), typedValue.getTypeDescriptor()); } else if (op1 instanceof Long) { newValue = new TypedValue(op1.longValue() + 1L, typedValue.getTypeDescriptor()); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java index adbc29916405..1fa2e2025308 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,10 +50,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java index 8101dee8b31f..fc6d91705c8d 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,10 +50,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java index 54d6b689d8e1..ddbc9c7d6f67 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Giovanni Dall'Oglio Risso + * @author Sam Brannen * @since 3.0 */ public class OpMinus extends Operator { @@ -58,38 +59,38 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep if (this.children.length < 2) { // if only one operand, then this is unary minus Object operand = leftOp.getValueInternal(state).getValue(); - if (operand instanceof Number) { - if (operand instanceof BigDecimal) { - return new TypedValue(((BigDecimal) operand).negate()); + if (operand instanceof Number number) { + if (number instanceof BigDecimal bigDecimal) { + return new TypedValue(bigDecimal.negate()); } - else if (operand instanceof Double) { + else if (number instanceof BigInteger bigInteger) { + return new TypedValue(bigInteger.negate()); + } + else if (number instanceof Double) { this.exitTypeDescriptor = "D"; - return new TypedValue(0 - ((Number) operand).doubleValue()); + return new TypedValue(0 - number.doubleValue()); } - else if (operand instanceof Float) { + else if (number instanceof Float) { this.exitTypeDescriptor = "F"; - return new TypedValue(0 - ((Number) operand).floatValue()); - } - else if (operand instanceof BigInteger) { - return new TypedValue(((BigInteger) operand).negate()); + return new TypedValue(0 - number.floatValue()); } - else if (operand instanceof Long) { + else if (number instanceof Long) { this.exitTypeDescriptor = "J"; - return new TypedValue(0 - ((Number) operand).longValue()); + return new TypedValue(0 - number.longValue()); } - else if (operand instanceof Integer) { + else if (number instanceof Integer) { this.exitTypeDescriptor = "I"; - return new TypedValue(0 - ((Number) operand).intValue()); + return new TypedValue(0 - number.intValue()); } - else if (operand instanceof Short) { - return new TypedValue(0 - ((Number) operand).shortValue()); + else if (number instanceof Short) { + return new TypedValue(0 - number.shortValue()); } - else if (operand instanceof Byte) { - return new TypedValue(0 - ((Number) operand).byteValue()); + else if (number instanceof Byte) { + return new TypedValue(0 - number.byteValue()); } else { - // Unknown Number subtypes -> best guess is double subtraction - return new TypedValue(0 - ((Number) operand).doubleValue()); + // Unknown Number subtype -> best guess is double subtraction + return new TypedValue(0 - number.doubleValue()); } } return state.operate(Operation.SUBTRACT, operand, null); @@ -98,10 +99,7 @@ else if (operand instanceof Byte) { Object left = leftOp.getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); @@ -134,9 +132,7 @@ else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNume } } - if (left instanceof String && right instanceof Integer && ((String) left).length() == 1) { - String theString = (String) left; - Integer theInteger = (Integer) right; + if (left instanceof String theString && right instanceof Integer theInteger && theString.length() == 1) { // Implements character - int (ie. b - 1 = a) return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger))); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java index eadd9a960d1f..51385a3603ea 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,10 +48,7 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); - if (leftOperand instanceof Number && rightOperand instanceof Number) { - Number leftNumber = (Number) leftOperand; - Number rightNumber = (Number) rightOperand; - + if (leftOperand instanceof Number leftNumber && rightOperand instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); @@ -79,7 +76,7 @@ else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNume return new TypedValue(leftNumber.intValue() % rightNumber.intValue()); } else { - // Unknown Number subtypes -> best guess is double division + // Unknown Number subtype -> best guess is double division return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue()); } } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java index fbf28317b32b..83c406e01d09 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,10 +72,7 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep Object leftOperand = getLeftOperand().getValueInternal(state).getValue(); Object rightOperand = getRightOperand().getValueInternal(state).getValue(); - if (leftOperand instanceof Number && rightOperand instanceof Number) { - Number leftNumber = (Number) leftOperand; - Number rightNumber = (Number) rightOperand; - + if (leftOperand instanceof Number leftNumber && rightOperand instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); @@ -108,13 +105,8 @@ else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNume } } - if (leftOperand instanceof String && rightOperand instanceof Integer) { - int repeats = (Integer) rightOperand; - StringBuilder result = new StringBuilder(); - for (int i = 0; i < repeats; i++) { - result.append(leftOperand); - } - return new TypedValue(result.toString()); + if (leftOperand instanceof String text && rightOperand instanceof Integer repeats) { + return new TypedValue(text.repeat(repeats)); } return state.operate(Operation.MULTIPLY, leftOperand, rightOperand); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java index d128f56b2deb..098f96dca123 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,10 +85,7 @@ else if (operandOne instanceof Integer) { TypedValue operandTwoValue = getRightOperand().getValueInternal(state); Object rightOperand = operandTwoValue.getValue(); - if (leftOperand instanceof Number && rightOperand instanceof Number) { - Number leftNumber = (Number) leftOperand; - Number rightNumber = (Number) rightOperand; - + if (leftOperand instanceof Number leftNumber && rightOperand instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); @@ -121,9 +118,9 @@ else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNume } } - if (leftOperand instanceof String && rightOperand instanceof String) { + if (leftOperand instanceof String leftString && rightOperand instanceof String rightString) { this.exitTypeDescriptor = "Ljava/lang/String"; - return new TypedValue((String) leftOperand + rightOperand); + return new TypedValue(leftString + rightString); } if (leftOperand instanceof String) { @@ -190,8 +187,7 @@ public boolean isCompilable() { * them all to the same (on stack) StringBuilder. */ private void walk(MethodVisitor mv, CodeFlow cf, @Nullable SpelNodeImpl operand) { - if (operand instanceof OpPlus) { - OpPlus plus = (OpPlus)operand; + if (operand instanceof OpPlus plus) { walk(mv, cf, plus.getLeftOperand()); walk(mv, cf, plus.getRightOperand()); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java index ec31559e379f..11150a7c9bf6 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java @@ -36,6 +36,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Giovanni Dall'Oglio Risso + * @author Sam Brannen * @since 3.0 */ public abstract class Operator extends SpelNodeImpl { @@ -261,10 +262,7 @@ else if (targetType == 'I') { * @param right the right-hand operand value */ public static boolean equalityCheck(EvaluationContext context, @Nullable Object left, @Nullable Object right) { - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java index 22795a928796..6c9bfbaf3ed5 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ * * @author Andy Clement * @author Juergen Hoeller + * @author Sam Brannen * @since 3.0 */ public class OperatorMatches extends Operator { @@ -69,13 +70,12 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati throw new SpelEvaluationException(leftOp.getStartPosition(), SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, (Object) null); } - if (!(right instanceof String)) { + if (!(right instanceof String rightString)) { throw new SpelEvaluationException(rightOp.getStartPosition(), SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right); } try { - String rightString = (String) right; Pattern pattern = this.patternCache.get(rightString); if (pattern == null) { pattern = Pattern.compile(rightString); @@ -111,7 +111,7 @@ private static class MatcherInput implements CharSequence { private final CharSequence value; - private AccessCount access; + private final AccessCount access; public MatcherInput(CharSequence value, AccessCount access) { this.value = value; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java index 2a256ea4371e..cd90af1effff 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,10 +47,7 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep Object leftOperand = leftOp.getValueInternal(state).getValue(); Object rightOperand = rightOp.getValueInternal(state).getValue(); - if (leftOperand instanceof Number && rightOperand instanceof Number) { - Number leftNumber = (Number) leftOperand; - Number rightNumber = (Number) rightOperand; - + if (leftOperand instanceof Number leftNumber && rightOperand instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); return new TypedValue(leftBigDecimal.pow(rightNumber.intValue())); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java index 476fb4c9b7a2..769376c9fa50 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Clark Duplichien + * @author Sam Brannen * @since 3.0 */ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -91,8 +92,7 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(), state.getConfiguration().isAutoGrowNullReferences()); PropertyAccessor accessorToUse = this.cachedReadAccessor; - if (accessorToUse instanceof CompilablePropertyAccessor) { - CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse; + if (accessorToUse instanceof CompilablePropertyAccessor accessor) { setExitTypeDescriptor(CodeFlow.toDescriptor(accessor.getPropertyType())); } return tv; @@ -196,8 +196,8 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval try { for (PropertyAccessor accessor : accessorsToTry) { if (accessor.canRead(evalContext, contextObject.getValue(), name)) { - if (accessor instanceof ReflectivePropertyAccessor) { - accessor = ((ReflectivePropertyAccessor) accessor).createOptimalAccessor( + if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) { + accessor = reflectivePropertyAccessor.createOptimalAccessor( evalContext, contextObject.getValue(), name); } this.cachedReadAccessor = accessor; @@ -330,9 +330,8 @@ else if (clazz.isAssignableFrom(targetType)) { @Override public boolean isCompilable() { - PropertyAccessor accessorToUse = this.cachedReadAccessor; - return (accessorToUse instanceof CompilablePropertyAccessor && - ((CompilablePropertyAccessor) accessorToUse).isCompilable()); + return (this.cachedReadAccessor instanceof CompilablePropertyAccessor compilablePropertyAccessor && + compilablePropertyAccessor.isCompilable()); } @Override @@ -404,9 +403,8 @@ public AccessorLValue(PropertyOrFieldReference propertyOrFieldReference, TypedVa public TypedValue getValue() { TypedValue value = this.ref.getValueInternal(this.contextObject, this.evalContext, this.autoGrowNullReferences); - PropertyAccessor accessorToUse = this.ref.cachedReadAccessor; - if (accessorToUse instanceof CompilablePropertyAccessor) { - this.ref.setExitTypeDescriptor(CodeFlow.toDescriptor(((CompilablePropertyAccessor) accessorToUse).getPropertyType())); + if (this.ref.cachedReadAccessor instanceof CompilablePropertyAccessor compilablePropertyAccessor) { + this.ref.setExitTypeDescriptor(CodeFlow.toDescriptor(compilablePropertyAccessor.getPropertyType())); } return value; } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java b/spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java index 5034c960ec61..2726fa53fb3c 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,15 +65,15 @@ class Tokenizer { } - private String expressionString; + private final String expressionString; - private char[] charsToProcess; + private final char[] charsToProcess; private int pos; - private int max; + private final int max; - private List tokens = new ArrayList<>(); + private final List tokens = new ArrayList<>(); public Tokenizer(String inputData) { diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java index 5fd48cdad88a..fae62e1d3791 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -123,7 +123,7 @@ public boolean canRead(EvaluationContext context, @Nullable Object target, Strin return false; } - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); if (type.isArray() && name.equals("length")) { return true; } @@ -160,7 +160,7 @@ public boolean canRead(EvaluationContext context, @Nullable Object target, Strin @Override public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException { Assert.state(target != null, "Target must not be null"); - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); if (type.isArray() && name.equals("length")) { if (target instanceof Class) { @@ -231,7 +231,7 @@ public boolean canWrite(EvaluationContext context, @Nullable Object target, Stri return false; } - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class); if (this.writerCache.containsKey(cacheKey)) { return true; @@ -269,7 +269,7 @@ public void write(EvaluationContext context, @Nullable Object target, String nam } Assert.state(target != null, "Target must not be null"); - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); Object possiblyConvertedNewValue = newValue; TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name); @@ -346,7 +346,7 @@ public Member getLastReadInvokerPair() { @Nullable private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); if (type.isArray() && name.equals("length")) { return TypeDescriptor.valueOf(Integer.TYPE); @@ -533,18 +533,18 @@ public PropertyAccessor createOptimalAccessor(EvaluationContext context, @Nullab if (target == null) { return this; } - Class clazz = (target instanceof Class ? (Class) target : target.getClass()); - if (clazz.isArray()) { + Class type = (target instanceof Class clazz ? clazz : target.getClass()); + if (type.isArray()) { return this; } - PropertyCacheKey cacheKey = new PropertyCacheKey(clazz, name, target instanceof Class); + PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class); InvokerPair invocationTarget = this.readerCache.get(cacheKey); if (invocationTarget == null || invocationTarget.member instanceof Method) { Method method = (Method) (invocationTarget != null ? invocationTarget.member : null); if (method == null) { - method = findGetterForProperty(name, clazz, target); + method = findGetterForProperty(name, type, target); if (method != null) { TypeDescriptor typeDescriptor = new TypeDescriptor(new MethodParameter(method, -1)); method = ClassUtils.getInterfaceMethodIfPossible(method); @@ -561,7 +561,7 @@ public PropertyAccessor createOptimalAccessor(EvaluationContext context, @Nullab if (invocationTarget == null || invocationTarget.member instanceof Field) { Field field = (invocationTarget != null ? (Field) invocationTarget.member : null); if (field == null) { - field = findField(name, clazz, target instanceof Class); + field = findField(name, type, target instanceof Class); if (field != null) { invocationTarget = new InvokerPair(field, new TypeDescriptor(field)); ReflectionUtils.makeAccessible(field); @@ -600,7 +600,7 @@ private static final class PropertyCacheKey implements Comparable clazz, String name, boolean targetIsClass) { this.clazz = clazz; @@ -613,10 +613,9 @@ public boolean equals(@Nullable Object other) { if (this == other) { return true; } - if (!(other instanceof PropertyCacheKey)) { + if (!(other instanceof PropertyCacheKey otherKey)) { return false; } - PropertyCacheKey otherKey = (PropertyCacheKey) other; return (this.clazz == otherKey.clazz && this.property.equals(otherKey.property) && this.targetIsClass == otherKey.targetIsClass); } @@ -676,13 +675,12 @@ public boolean canRead(EvaluationContext context, @Nullable Object target, Strin if (target == null) { return false; } - Class type = (target instanceof Class ? (Class) target : target.getClass()); + Class type = (target instanceof Class clazz ? clazz : target.getClass()); if (type.isArray()) { return false; } - if (this.member instanceof Method) { - Method method = (Method) this.member; + if (this.member instanceof Method method) { String getterName = "get" + StringUtils.capitalize(name); if (getterName.equals(method.getName())) { return true; @@ -697,8 +695,7 @@ public boolean canRead(EvaluationContext context, @Nullable Object target, Strin @Override public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException { - if (this.member instanceof Method) { - Method method = (Method) this.member; + if (this.member instanceof Method method) { try { ReflectionUtils.makeAccessible(method); Object value = method.invoke(target); @@ -739,8 +736,8 @@ public boolean isCompilable() { @Override public Class getPropertyType() { - if (this.member instanceof Method) { - return ((Method) this.member).getReturnType(); + if (this.member instanceof Method method) { + return method.getReturnType(); } else { return ((Field) this.member).getType(); @@ -769,8 +766,7 @@ public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) { } } - if (this.member instanceof Method) { - Method method = (Method) this.member; + if (this.member instanceof Method method) { boolean isInterface = method.getDeclaringClass().isInterface(); int opcode = (isStatic ? INVOKESTATIC : isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL); mv.visitMethodInsn(opcode, classDesc, method.getName(), diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java index 8e48aa878c8f..fc15f6b3ec0e 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,10 +62,7 @@ else if (right == null) { } // Basic number comparisons - if (left instanceof Number && right instanceof Number) { - Number leftNumber = (Number) left; - Number rightNumber = (Number) right; - + if (left instanceof Number leftNumber && right instanceof Number rightNumber) { if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) { BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); @@ -95,7 +92,7 @@ else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { return Byte.compare(leftNumber.byteValue(), rightNumber.byteValue()); } else { - // Unknown Number subtypes -> best guess is double multiplication + // Unknown Number subtype -> best guess is double multiplication return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue()); } }