From f07a4587bbe8554a9bd3f39d91a293f487f28c1a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 7 Dec 2022 17:09:24 -0500 Subject: [PATCH] Apply 'instanceof pattern matching' in spring-expression --- .../expression/spel/ast/FunctionReference.java | 6 +++--- .../expression/spel/ast/Indexer.java | 6 +++--- .../expression/spel/ast/MethodReference.java | 14 +++++++------- .../expression/spel/ast/OperatorBetween.java | 5 ++--- .../expression/spel/ast/OperatorInstanceof.java | 5 ++--- .../expression/spel/ast/Projection.java | 9 ++++----- .../expression/spel/ast/Selection.java | 15 +++++++-------- .../expression/spel/ast/SpelNodeImpl.java | 7 +++---- .../expression/spel/standard/SpelCompiler.java | 6 +++--- .../spel/support/ReflectiveMethodResolver.java | 2 +- 10 files changed, 35 insertions(+), 40 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java index 041773538647..892f8bab860d 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java @@ -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 TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep 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()); 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 7a9d277ac027..a12c11df85e6 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-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 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException 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()); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java index 1ecb6187001a..8931591e54f6 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java @@ -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 @@ private TypedValue getValueInternal(EvaluationContext evaluationContext, // 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 @@ private MethodExecutor findAccessorForMethod(List argumentTypes, 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 @@ private MethodExecutor findAccessorForMethod(List argumentTypes, 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 @@ private void throwSimpleExceptionIfPossible(Object value, AccessException ex) { 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; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java index e5f94adecea5..12733435a90d 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorBetween.java @@ -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 OperatorBetween(int startPos, int endPos, SpelNodeImpl... operands) { 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(); diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java index 3cd663d873bc..36dfc4c4366b 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorInstanceof.java @@ -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 BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati 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 } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Projection.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Projection.java index 1639a9e4416d..b9a9c2cc0b9d 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Projection.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Projection.java @@ -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 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException // 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 result = new ArrayList<>(); for (Map.Entry entry : mapData.entrySet()) { try { @@ -88,8 +87,8 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException } 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 result = new ArrayList<>(); Class arrayElementType = null; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java index 9daa3b94aa78..72f9aa6f0e88 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Selection.java @@ -87,8 +87,7 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException 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 result = new HashMap<>(); Object lastKey = null; @@ -99,8 +98,8 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException 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 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException } 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 result = new ArrayList<>(); int index = 0; @@ -145,8 +144,8 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException 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); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java index 3976fbf3425d..c3514ab1cdd9 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java @@ -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 Class getObjectClass(@Nullable Object obj) { 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 @@ protected ValueRef getValueRef(ExpressionState state) throws EvaluationException 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(); } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java b/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java index a431d3055a55..695d82c1b36f 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java @@ -252,7 +252,7 @@ public static SpelCompiler getCompiler(@Nullable ClassLoader classLoader) { * {@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 static boolean compile(Expression expression) { * @param expression the expression */ public static void revertToInterpreted(Expression expression) { - if (expression instanceof SpelExpression) { - ((SpelExpression) expression).revertToInterpreted(); + if (expression instanceof SpelExpression spelExpression) { + spelExpression.revertToInterpreted(); } } diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java index 86889dca73e2..b4c634d34812 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java @@ -114,7 +114,7 @@ public MethodExecutor resolve(EvaluationContext context, Object targetObject, St try { TypeConverter typeConverter = context.getTypeConverter(); - Class type = (targetObject instanceof Class ? (Class) targetObject : targetObject.getClass()); + Class type = (targetObject instanceof Class clazz ? clazz : targetObject.getClass()); ArrayList methods = new ArrayList<>(getMethods(type, targetObject)); // If a filter is registered for this type, call it