Skip to content

Commit

Permalink
Apply 'instanceof pattern matching' in spring-expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 7, 2022
1 parent 114d6a9 commit f07a458
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 40 deletions.
@@ -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.
Expand Down Expand Up @@ -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());
Expand Down
@@ -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.
Expand Down Expand Up @@ -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());
}
Expand Down
@@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ private MethodExecutor findAccessorForMethod(List<TypeDescriptor> 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);
Expand All @@ -233,8 +233,8 @@ private MethodExecutor findAccessorForMethod(List<TypeDescriptor> 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 +
Expand All @@ -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;
Expand Down
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down
@@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
@@ -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.
Expand Down Expand Up @@ -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<Object> result = new ArrayList<>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
Expand All @@ -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<Object> result = new ArrayList<>();
Class<?> arrayElementType = null;
Expand Down
Expand Up @@ -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<Object, Object> result = new HashMap<>();
Object lastKey = null;
Expand All @@ -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);
Expand Down Expand Up @@ -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<Object> result = new ArrayList<>();
int index = 0;
Expand All @@ -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);
}
Expand Down
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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());
}

/**
Expand All @@ -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();
}
}

Expand Down
Expand Up @@ -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<Method> methods = new ArrayList<>(getMethods(type, targetObject));

// If a filter is registered for this type, call it
Expand Down

0 comments on commit f07a458

Please sign in to comment.