Skip to content

Commit 4eed2ce

Browse files
committed
Polishing
1 parent 98f1287 commit 4eed2ce

File tree

4 files changed

+38
-43
lines changed

4 files changed

+38
-43
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,29 +24,26 @@
2424
import org.springframework.util.ClassUtils;
2525

2626
/**
27-
* Utility methods (formatters etc) used during parsing and evaluation.
27+
* Utility methods (formatters, etc) used during parsing and evaluation.
2828
*
2929
* @author Andy Clement
30+
* @author Sam Brannen
3031
*/
3132
abstract class FormatHelper {
3233

3334
/**
3435
* Produce a readable representation for a given method name with specified arguments.
3536
* @param name the name of the method
3637
* @param argumentTypes the types of the arguments to the method
37-
* @return a nicely formatted representation, e.g. {@code foo(String,int)}
38+
* @return a nicely formatted representation — for example, {@code foo(java.lang.String,int)}
3839
*/
39-
public static String formatMethodForMessage(String name, List<TypeDescriptor> argumentTypes) {
40+
static String formatMethodForMessage(String name, List<TypeDescriptor> argumentTypes) {
4041
StringJoiner sj = new StringJoiner(",", "(", ")");
4142
for (TypeDescriptor typeDescriptor : argumentTypes) {
42-
if (typeDescriptor != null) {
43-
sj.add(formatClassNameForMessage(typeDescriptor.getType()));
44-
}
45-
else {
46-
sj.add(formatClassNameForMessage(null));
47-
}
43+
String className = (typeDescriptor != null ? formatClassNameForMessage(typeDescriptor.getType()) : "null");
44+
sj.add(className);
4845
}
49-
return name + sj.toString();
46+
return name + sj;
5047
}
5148

5249
/**
@@ -56,7 +53,7 @@ public static String formatMethodForMessage(String name, List<TypeDescriptor> ar
5653
* @return a formatted String suitable for message inclusion
5754
* @see ClassUtils#getQualifiedName(Class)
5855
*/
59-
public static String formatClassNameForMessage(@Nullable Class<?> clazz) {
56+
static String formatClassNameForMessage(@Nullable Class<?> clazz) {
6057
return (clazz != null ? ClassUtils.getQualifiedName(clazz) : "null");
6158
}
6259

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes,
231231
* if the cause was a RuntimeException, throw the RuntimeException directly.
232232
*/
233233
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
234-
if (ex.getCause() instanceof InvocationTargetException) {
235-
Throwable rootCause = ex.getCause().getCause();
234+
if (ex.getCause() instanceof InvocationTargetException cause) {
235+
Throwable rootCause = cause.getCause();
236236
if (rootCause instanceof RuntimeException runtimeException) {
237237
throw runtimeException;
238238
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,10 +28,11 @@
2828
import org.springframework.util.Assert;
2929

3030
/**
31-
* Represents a reference to a type, for example
32-
* {@code "T(String)" or "T(com.somewhere.Foo)"}.
31+
* Represents a reference to a type, for example {@code "T(String)"} or
32+
* {@code "T(com.example.Foo)"}.
3333
*
3434
* @author Andy Clement
35+
* @author Sam Brannen
3536
*/
3637
public class TypeReference extends SpelNodeImpl {
3738

@@ -74,22 +75,19 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
7475
}
7576

7677
private Class<?> makeArrayIfNecessary(Class<?> clazz) {
77-
if (this.dimensions != 0) {
78-
for (int i = 0; i < this.dimensions; i++) {
79-
Object array = Array.newInstance(clazz, 0);
80-
clazz = array.getClass();
81-
}
78+
if (this.dimensions < 1) {
79+
return clazz;
8280
}
83-
return clazz;
81+
int[] dims = new int[this.dimensions];
82+
Object array = Array.newInstance(clazz, dims);
83+
return array.getClass();
8484
}
8585

8686
@Override
8787
public String toStringAST() {
8888
StringBuilder sb = new StringBuilder("T(");
8989
sb.append(getChild(0).toStringAST());
90-
for (int d = 0; d < this.dimensions; d++) {
91-
sb.append("[]");
92-
}
90+
sb.append("[]".repeat(this.dimensions));
9391
sb.append(')');
9492
return sb.toString();
9593
}

spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ protected Method[] getMethods(Class<?> type) {
869869
Expression expression = parser.parseExpression("parseInt('-FF', 16)");
870870

871871
Integer result = expression.getValue(context, "", Integer.class);
872-
assertThat(result.intValue()).isEqualTo(-255);
872+
assertThat(result).isEqualTo(-255);
873873
}
874874

875875
@Test
@@ -880,25 +880,25 @@ void array() {
880880
Object result = null;
881881

882882
expression = parser.parseExpression("new java.lang.Long[0].class");
883-
result = expression.getValue(context, "");
884-
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
883+
result = expression.getValue(context);
884+
assertThat(result).asString().isEqualTo("class [Ljava.lang.Long;");
885885

886886
expression = parser.parseExpression("T(java.lang.Long[])");
887-
result = expression.getValue(context, "");
888-
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
887+
result = expression.getValue(context);
888+
assertThat(result).asString().isEqualTo("class [Ljava.lang.Long;");
889889

890890
expression = parser.parseExpression("T(java.lang.String[][][])");
891-
result = expression.getValue(context, "");
892-
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [[[Ljava.lang.String;");
891+
result = expression.getValue(context);
892+
assertThat(result).asString().isEqualTo("class [[[Ljava.lang.String;");
893893
assertThat(((SpelExpression) expression).toStringAST()).isEqualTo("T(java.lang.String[][][])");
894894

895895
expression = parser.parseExpression("new int[0].class");
896-
result = expression.getValue(context, "");
897-
assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [I");
896+
result = expression.getValue(context);
897+
assertThat(result).asString().isEqualTo("class [I");
898898

899899
expression = parser.parseExpression("T(int[][])");
900-
result = expression.getValue(context, "");
901-
assertThat(result.toString()).isEqualTo("class [[I");
900+
result = expression.getValue(context);
901+
assertThat(result).asString().isEqualTo("class [[I");
902902
}
903903

904904
@Test
@@ -1587,12 +1587,12 @@ void SPR10417() {
15871587
// #this should be the element from list1
15881588
Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
15891589
Object result = ex.getValue(context);
1590-
assertThat(result.toString()).isEqualTo("[x]");
1590+
assertThat(result).asString().isEqualTo("[x]");
15911591

15921592
// toString() should be called on the element from list1
15931593
ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
15941594
result = ex.getValue(context);
1595-
assertThat(result.toString()).isEqualTo("[x]");
1595+
assertThat(result).asString().isEqualTo("[x]");
15961596

15971597
List list3 = new ArrayList();
15981598
list3.add(1);
@@ -1604,11 +1604,11 @@ void SPR10417() {
16041604
context.setVariable("list3", list3);
16051605
ex = parser.parseExpression("#list3.?[#this > 2]");
16061606
result = ex.getValue(context);
1607-
assertThat(result.toString()).isEqualTo("[3, 4]");
1607+
assertThat(result).asString().isEqualTo("[3, 4]");
16081608

16091609
ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
16101610
result = ex.getValue(context);
1611-
assertThat(result.toString()).isEqualTo("[1, 2, 3, 4]");
1611+
assertThat(result).asString().isEqualTo("[1, 2, 3, 4]");
16121612
}
16131613

16141614
@Test
@@ -1628,11 +1628,11 @@ void SPR10417_maps() {
16281628
// #this should be the element from list1
16291629
Expression ex = parser.parseExpression("#map1.?[#map2.containsKey(#this.getKey())]");
16301630
Object result = ex.getValue(context);
1631-
assertThat(result.toString()).isEqualTo("{X=66}");
1631+
assertThat(result).asString().isEqualTo("{X=66}");
16321632

16331633
ex = parser.parseExpression("#map1.?[#map2.containsKey(key)]");
16341634
result = ex.getValue(context);
1635-
assertThat(result.toString()).isEqualTo("{X=66}");
1635+
assertThat(result).asString().isEqualTo("{X=66}");
16361636
}
16371637

16381638
@Test

0 commit comments

Comments
 (0)