Browse Source

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`.
pull/27562/head
Sam Brannen 3 years ago
parent
commit
4e6ef82d8f
  1. 5
      spring-expression/src/main/java/org/springframework/expression/TypedValue.java
  2. 11
      spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java
  3. 36
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java
  4. 18
      spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java
  5. 30
      spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java
  6. 14
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java
  7. 8
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java
  8. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java
  9. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java
  10. 14
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java
  11. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java
  12. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java
  13. 52
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java
  14. 9
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java
  15. 16
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java
  16. 14
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java
  17. 6
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java
  18. 8
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java
  19. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java
  20. 20
      spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java
  21. 10
      spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java
  22. 42
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java
  23. 9
      spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

5
spring-expression/src/main/java/org/springframework/expression/TypedValue.java

@ -1,5 +1,5 @@ @@ -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 class TypedValue { @@ -85,10 +85,9 @@ public class TypedValue {
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) ||

11
spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

@ -136,8 +136,8 @@ public class ConstructorReference extends SpelNodeImpl { @@ -136,8 +136,8 @@ public class ConstructorReference extends SpelNodeImpl {
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 @@ public class ConstructorReference extends SpelNodeImpl { @@ -158,9 +158,9 @@ public class ConstructorReference extends SpelNodeImpl {
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 class ConstructorReference extends SpelNodeImpl { @@ -228,14 +228,13 @@ public class ConstructorReference extends SpelNodeImpl {
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) {

36
spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.ReflectionUtils; @@ -50,6 +50,7 @@ import org.springframework.util.ReflectionUtils;
* @author Andy Clement
* @author Phillip Webb
* @author Stephane Nicoll
* @author Sam Brannen
* @since 3.0
*/
// TODO support multidimensional arrays
@ -121,8 +122,7 @@ public class Indexer extends SpelNodeImpl { @@ -121,8 +122,7 @@ public class Indexer extends SpelNodeImpl {
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 @@ public class Indexer extends SpelNodeImpl { @@ -148,13 +148,13 @@ public class Indexer extends SpelNodeImpl {
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 @@ public class Indexer extends SpelNodeImpl { @@ -275,8 +275,7 @@ public class Indexer extends SpelNodeImpl {
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 @@ public class Indexer extends SpelNodeImpl { @@ -306,9 +305,9 @@ public class Indexer extends SpelNodeImpl {
}
}
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 class Indexer extends SpelNodeImpl { @@ -572,19 +571,17 @@ public class Indexer extends SpelNodeImpl {
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 class Indexer extends SpelNodeImpl { @@ -665,8 +662,8 @@ public class Indexer extends SpelNodeImpl {
@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 class Indexer extends SpelNodeImpl { @@ -683,8 +680,7 @@ public class Indexer extends SpelNodeImpl {
@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());

18
spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineList.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.Assert; @@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* 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 @@ public class InlineList extends SpelNodeImpl { @@ -59,8 +60,7 @@ public class InlineList extends SpelNodeImpl {
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 @@ public class InlineList extends SpelNodeImpl { @@ -75,11 +75,11 @@ public class InlineList extends SpelNodeImpl {
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 @@ public class InlineList extends SpelNodeImpl { @@ -164,8 +164,8 @@ public class InlineList extends SpelNodeImpl {
// 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);

30
spring-expression/src/main/java/org/springframework/expression/spel/ast/InlineMap.java

@ -31,6 +31,7 @@ import org.springframework.util.Assert; @@ -31,6 +31,7 @@ import org.springframework.util.Assert;
* 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 @@ public class InlineMap extends SpelNodeImpl { @@ -56,15 +57,13 @@ public class InlineMap extends SpelNodeImpl {
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 @@ public class InlineMap extends SpelNodeImpl { @@ -84,23 +83,23 @@ public class InlineMap extends SpelNodeImpl {
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 class InlineMap extends SpelNodeImpl { @@ -120,8 +119,7 @@ public class InlineMap extends SpelNodeImpl {
// 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 {

14
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.Assert; @@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* @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 class OpDec extends Operator { @@ -60,10 +61,9 @@ public class OpDec extends Operator {
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 @@ public class OpDec extends Operator { @@ -71,8 +71,8 @@ public class OpDec extends Operator {
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());

8
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDivide.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.NumberUtils; @@ -35,6 +35,7 @@ import org.springframework.util.NumberUtils;
* @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 class OpDivide extends Operator { @@ -49,10 +50,7 @@ public class OpDivide extends Operator {
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);

7
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGE.java

@ -1,5 +1,5 @@ @@ -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 class OpGE extends Operator { @@ -50,10 +50,7 @@ public class OpGE extends Operator {
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);

7
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpGT.java

@ -1,5 +1,5 @@ @@ -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 class OpGT extends Operator { @@ -50,10 +50,7 @@ public class OpGT extends Operator {
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);

14
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.Assert; @@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* @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 class OpInc extends Operator { @@ -58,10 +59,9 @@ public class OpInc extends Operator {
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 @@ public class OpInc extends Operator { @@ -69,8 +69,8 @@ public class OpInc extends Operator {
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());

7
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLE.java

@ -1,5 +1,5 @@ @@ -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 class OpLE extends Operator { @@ -50,10 +50,7 @@ public class OpLE extends Operator {
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);

7
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpLT.java

@ -1,5 +1,5 @@ @@ -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 class OpLT extends Operator { @@ -50,10 +50,7 @@ public class OpLT extends Operator {
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);

52
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMinus.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.NumberUtils; @@ -43,6 +43,7 @@ import org.springframework.util.NumberUtils;
* @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 class OpMinus extends Operator { @@ -58,38 +59,38 @@ public class OpMinus extends Operator {
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 @@ public class OpMinus extends Operator { @@ -98,10 +99,7 @@ public class OpMinus extends Operator {
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 @@ public class OpMinus extends Operator { @@ -134,9 +132,7 @@ public class OpMinus extends Operator {
}
}
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)));
}

9
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpModulus.java

@ -1,5 +1,5 @@ @@ -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 class OpModulus extends Operator { @@ -48,10 +48,7 @@ public class OpModulus extends Operator {
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 @@ public class OpModulus extends Operator { @@ -79,7 +76,7 @@ public class OpModulus extends Operator {
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());
}
}

16
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpMultiply.java

@ -1,5 +1,5 @@ @@ -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 class OpMultiply extends Operator { @@ -72,10 +72,7 @@ public class OpMultiply extends Operator {
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 @@ public class OpMultiply extends Operator { @@ -108,13 +105,8 @@ public class OpMultiply extends Operator {
}
}
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);

14
spring-expression/src/main/java/org/springframework/expression/spel/ast/OpPlus.java

@ -1,5 +1,5 @@ @@ -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 @@ public class OpPlus extends Operator { @@ -85,10 +85,7 @@ public class OpPlus extends Operator {
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 @@ public class OpPlus extends Operator { @@ -121,9 +118,9 @@ public class OpPlus extends Operator {
}
}
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 class OpPlus extends Operator { @@ -190,8 +187,7 @@ public class OpPlus extends Operator {
* 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());
}

6
spring-expression/src/main/java/org/springframework/expression/spel/ast/Operator.java

@ -36,6 +36,7 @@ import org.springframework.util.ObjectUtils; @@ -36,6 +36,7 @@ import org.springframework.util.ObjectUtils;
* @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 @@ public abstract class Operator extends SpelNodeImpl { @@ -261,10 +262,7 @@ public abstract class Operator extends SpelNodeImpl {
* @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);

8
spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.expression.spel.support.BooleanTypedValue; @@ -36,6 +36,7 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
*
* @author Andy Clement
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
*/
public class OperatorMatches extends Operator {
@ -69,13 +70,12 @@ public class OperatorMatches extends Operator { @@ -69,13 +70,12 @@ public class OperatorMatches extends Operator {
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 @@ public class OperatorMatches extends Operator { @@ -111,7 +111,7 @@ public class OperatorMatches extends Operator {
private final CharSequence value;
private AccessCount access;
private final AccessCount access;
public MatcherInput(CharSequence value, AccessCount access) {
this.value = value;

7
spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorPower.java

@ -1,5 +1,5 @@ @@ -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 class OperatorPower extends Operator { @@ -47,10 +47,7 @@ public class OperatorPower extends Operator {
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()));

20
spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java

@ -1,5 +1,5 @@ @@ -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 @@ import org.springframework.util.ReflectionUtils; @@ -46,6 +46,7 @@ import org.springframework.util.ReflectionUtils;
* @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 class PropertyOrFieldReference extends SpelNodeImpl { @@ -91,8 +92,7 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
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 @@ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -196,8 +196,8 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
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 @@ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -330,9 +330,8 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
@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 class PropertyOrFieldReference extends SpelNodeImpl { @@ -404,9 +403,8 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
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;
}

10
spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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<Token> tokens = new ArrayList<>();
private final List<Token> tokens = new ArrayList<>();
public Tokenizer(String inputData) {

42
spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java

@ -1,5 +1,5 @@ @@ -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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -123,7 +123,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -160,7 +160,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
@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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -231,7 +231,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -269,7 +269,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
}
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -346,7 +346,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
@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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -533,18 +533,18 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -561,7 +561,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 @@ public class ReflectivePropertyAccessor implements PropertyAccessor { @@ -600,7 +600,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
private final String property;
private boolean targetIsClass;
private final boolean targetIsClass;
public PropertyCacheKey(Class<?> clazz, String name, boolean targetIsClass) {
this.clazz = clazz;
@ -613,10 +613,9 @@ public class ReflectivePropertyAccessor implements PropertyAccessor { @@ -613,10 +613,9 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -676,13 +675,12 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -697,8 +695,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
@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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -739,8 +736,8 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
@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 class ReflectivePropertyAccessor implements PropertyAccessor { @@ -769,8 +766,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
}
}
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(),

9
spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

@ -1,5 +1,5 @@ @@ -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 @@ public class StandardTypeComparator implements TypeComparator { @@ -62,10 +62,7 @@ public class StandardTypeComparator implements TypeComparator {
}
// 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 @@ public class StandardTypeComparator implements TypeComparator { @@ -95,7 +92,7 @@ public class StandardTypeComparator implements TypeComparator {
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());
}
}

Loading…
Cancel
Save