Browse Source

Apply 'instanceof pattern matching' in spring-expression

pull/29659/head
Sam Brannen 2 years ago
parent
commit
f07a4587bb
  1. 6
      spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java
  2. 6
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java
  3. 14
      spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java
  4. 5
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java
  5. 5
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java
  6. 9
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Projection.java
  7. 15
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java
  8. 7
      spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java
  9. 6
      spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java
  10. 2
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -70,14 +70,14 @@ public class FunctionReference extends SpelNodeImpl { @@ -70,14 +70,14 @@ public class FunctionReference extends SpelNodeImpl {
if (value == TypedValue.NULL) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name);
}
if (!(value.getValue() instanceof Method)) {
if (!(value.getValue() instanceof Method function)) {
// Possibly a static Java method registered as a function
throw new SpelEvaluationException(
SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass());
}
try {
return executeFunctionJLRMethod(state, (Method) value.getValue());
return executeFunctionJLRMethod(state, function);
}
catch (SpelEvaluationException ex) {
ex.setPosition(getStartPosition());

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -165,11 +165,11 @@ public class Indexer extends SpelNodeImpl { @@ -165,11 +165,11 @@ public class Indexer extends SpelNodeImpl {
this.indexedType = IndexedType.ARRAY;
return new ArrayIndexingValueRef(state.getTypeConverter(), target, idx, targetDescriptor);
}
else if (target instanceof Collection) {
else if (target instanceof Collection<?> collection) {
if (target instanceof List) {
this.indexedType = IndexedType.LIST;
}
return new CollectionIndexingValueRef((Collection<?>) target, idx, targetDescriptor,
return new CollectionIndexingValueRef(collection, idx, targetDescriptor,
state.getTypeConverter(), state.getConfiguration().isAutoGrowCollections(),
state.getConfiguration().getMaximumAutoGrowSize());
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@ -134,7 +134,7 @@ public class MethodReference extends SpelNodeImpl { @@ -134,7 +134,7 @@ public class MethodReference extends SpelNodeImpl {
// either there was no accessor or it no longer existed
executorToUse = findAccessorForMethod(argumentTypes, value, evaluationContext);
this.cachedExecutor = new CachedMethodExecutor(
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
executorToUse, (value instanceof Class<?> clazz ? clazz : null), targetType, argumentTypes);
try {
return executorToUse.execute(evaluationContext, value, arguments);
}
@ -216,7 +216,7 @@ public class MethodReference extends SpelNodeImpl { @@ -216,7 +216,7 @@ public class MethodReference extends SpelNodeImpl {
String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
String className = FormatHelper.formatClassNameForMessage(
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
targetObject instanceof Class<?> clazz ? clazz : targetObject.getClass());
if (accessException != null) {
throw new SpelEvaluationException(
getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
@ -233,8 +233,8 @@ public class MethodReference extends SpelNodeImpl { @@ -233,8 +233,8 @@ public class MethodReference extends SpelNodeImpl {
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
if (ex.getCause() instanceof InvocationTargetException) {
Throwable rootCause = ex.getCause().getCause();
if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
if (rootCause instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new ExpressionInvocationTargetException(getStartPosition(),
"A problem occurred when trying to execute method '" + this.name +
@ -244,8 +244,8 @@ public class MethodReference extends SpelNodeImpl { @@ -244,8 +244,8 @@ public class MethodReference extends SpelNodeImpl {
private void updateExitTypeDescriptor() {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck != null && executorToCheck.get() instanceof ReflectiveMethodExecutor) {
Method method = ((ReflectiveMethodExecutor) executorToCheck.get()).getMethod();
if (executorToCheck != null && executorToCheck.get() instanceof ReflectiveMethodExecutor reflectiveMethodExecutor) {
Method method = reflectiveMethodExecutor.getMethod();
String descriptor = CodeFlow.toDescriptor(method.getReturnType());
if (this.nullSafe && CodeFlow.isPrimitive(descriptor)) {
this.originalPrimitiveExitTypeDescriptor = descriptor;

5
spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -53,12 +53,11 @@ public class OperatorBetween extends Operator { @@ -53,12 +53,11 @@ public class OperatorBetween extends Operator {
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
Object left = getLeftOperand().getValueInternal(state).getValue();
Object right = getRightOperand().getValueInternal(state).getValue();
if (!(right instanceof List) || ((List<?>) right).size() != 2) {
if (!(right instanceof List<?> list) || list.size() != 2) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(),
SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
}
List<?> list = (List<?>) right;
Object low = list.get(0);
Object high = list.get(1);
TypeComparator comp = state.getTypeComparator();

5
spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,12 +62,11 @@ public class OperatorInstanceof extends Operator { @@ -62,12 +62,11 @@ public class OperatorInstanceof extends Operator {
Object leftValue = left.getValue();
Object rightValue = right.getValue();
BooleanTypedValue result;
if (!(rightValue instanceof Class)) {
if (!(rightValue instanceof Class<?> rightClass)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(),
SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
(rightValue == null ? "null" : rightValue.getClass().getName()));
}
Class<?> rightClass = (Class<?>) rightValue;
if (leftValue == null) {
result = BooleanTypedValue.FALSE; // null is not an instanceof anything
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -70,8 +70,7 @@ public class Projection extends SpelNodeImpl { @@ -70,8 +70,7 @@ public class Projection extends SpelNodeImpl {
// has two fields 'key' and 'value' that refer to the map entries key
// and value, and they can be referenced in the operation
// eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
if (operand instanceof Map) {
Map<?, ?> mapData = (Map<?, ?>) operand;
if (operand instanceof Map<?, ?> mapData) {
List<Object> result = new ArrayList<>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
@ -88,8 +87,8 @@ public class Projection extends SpelNodeImpl { @@ -88,8 +87,8 @@ public class Projection extends SpelNodeImpl {
}
if (operand instanceof Iterable || operandIsArray) {
Iterable<?> data = (operand instanceof Iterable ?
(Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
Iterable<?> data = (operand instanceof Iterable<?> iterable ?
iterable : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<>();
Class<?> arrayElementType = null;

15
spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java

@ -87,8 +87,7 @@ public class Selection extends SpelNodeImpl { @@ -87,8 +87,7 @@ public class Selection extends SpelNodeImpl {
Object operand = op.getValue();
SpelNodeImpl selectionCriteria = this.children[0];
if (operand instanceof Map) {
Map<?, ?> mapdata = (Map<?, ?>) operand;
if (operand instanceof Map<?, ?> mapdata) {
// TODO don't lose generic info for the new map
Map<Object, Object> result = new HashMap<>();
Object lastKey = null;
@ -99,8 +98,8 @@ public class Selection extends SpelNodeImpl { @@ -99,8 +98,8 @@ public class Selection extends SpelNodeImpl {
state.pushActiveContextObject(kvPair);
state.enterScope();
Object val = selectionCriteria.getValueInternal(state).getValue();
if (val instanceof Boolean) {
if ((Boolean) val) {
if (val instanceof Boolean b) {
if (b) {
if (this.variant == FIRST) {
result.put(entry.getKey(), entry.getValue());
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
@ -135,8 +134,8 @@ public class Selection extends SpelNodeImpl { @@ -135,8 +134,8 @@ public class Selection extends SpelNodeImpl {
}
if (operand instanceof Iterable || ObjectUtils.isArray(operand)) {
Iterable<?> data = (operand instanceof Iterable ?
(Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
Iterable<?> data = (operand instanceof Iterable<?> iterable ?
iterable : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<>();
int index = 0;
@ -145,8 +144,8 @@ public class Selection extends SpelNodeImpl { @@ -145,8 +144,8 @@ public class Selection extends SpelNodeImpl {
state.pushActiveContextObject(new TypedValue(element));
state.enterScope("index", index);
Object val = selectionCriteria.getValueInternal(state).getValue();
if (val instanceof Boolean) {
if ((Boolean) val) {
if (val instanceof Boolean b) {
if (b) {
if (this.variant == FIRST) {
return new ValueRef.TypedValueHolderValueRef(new TypedValue(element), this);
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -144,7 +144,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes { @@ -144,7 +144,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes {
if (obj == null) {
return null;
}
return (obj instanceof Class ? ((Class<?>) obj) : obj.getClass());
return (obj instanceof Class<?> clazz ? clazz : obj.getClass());
}
@Override
@ -207,8 +207,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes { @@ -207,8 +207,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes {
protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Member member, SpelNodeImpl[] arguments) {
String[] paramDescriptors = null;
boolean isVarargs = false;
if (member instanceof Constructor) {
Constructor<?> ctor = (Constructor<?>) member;
if (member instanceof Constructor<?> ctor) {
paramDescriptors = CodeFlow.toDescriptors(ctor.getParameterTypes());
isVarargs = ctor.isVarArgs();
}

6
spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java

@ -252,7 +252,7 @@ public final class SpelCompiler implements Opcodes { @@ -252,7 +252,7 @@ public final class SpelCompiler implements Opcodes {
* {@code false} otherwise
*/
public static boolean compile(Expression expression) {
return (expression instanceof SpelExpression && ((SpelExpression) expression).compileExpression());
return (expression instanceof SpelExpression spelExpression && spelExpression.compileExpression());
}
/**
@ -261,8 +261,8 @@ public final class SpelCompiler implements Opcodes { @@ -261,8 +261,8 @@ public final class SpelCompiler implements Opcodes {
* @param expression the expression
*/
public static void revertToInterpreted(Expression expression) {
if (expression instanceof SpelExpression) {
((SpelExpression) expression).revertToInterpreted();
if (expression instanceof SpelExpression spelExpression) {
spelExpression.revertToInterpreted();
}
}

2
spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

@ -114,7 +114,7 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -114,7 +114,7 @@ public class ReflectiveMethodResolver implements MethodResolver {
try {
TypeConverter typeConverter = context.getTypeConverter();
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
Class<?> type = (targetObject instanceof Class<?> clazz ? clazz : targetObject.getClass());
ArrayList<Method> methods = new ArrayList<>(getMethods(type, targetObject));
// If a filter is registered for this type, call it

Loading…
Cancel
Save