Skip to content

Commit ade139f

Browse files
committed
Polishing
(cherry picked from commit 45fc449)
1 parent 653f35a commit ade139f

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public boolean isCompilable() {
5858

5959
String leftDesc = left.exitTypeDescriptor;
6060
String rightDesc = right.exitTypeDescriptor;
61-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
62-
this.leftActualDescriptor, this.rightActualDescriptor);
61+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
62+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
6363
return (!dc.areNumbers || dc.areCompatible);
6464
}
6565

@@ -73,41 +73,39 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
7373
boolean leftPrim = CodeFlow.isPrimitive(leftDesc);
7474
boolean rightPrim = CodeFlow.isPrimitive(rightDesc);
7575

76-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
77-
this.leftActualDescriptor, this.rightActualDescriptor);
76+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
77+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
7878

7979
if (dc.areNumbers && dc.areCompatible) {
8080
char targetType = dc.compatibleType;
81-
8281
getLeftOperand().generateCode(mv, cf);
8382
if (!leftPrim) {
8483
CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
8584
}
86-
8785
cf.enterCompilationScope();
8886
getRightOperand().generateCode(mv, cf);
8987
cf.exitCompilationScope();
9088
if (!rightPrim) {
9189
CodeFlow.insertUnboxInsns(mv, targetType, rightDesc);
9290
}
9391
// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
94-
if (targetType=='D') {
92+
if (targetType == 'D') {
9593
mv.visitInsn(DCMPL);
9694
mv.visitJumpInsn(IFNE, elseTarget);
9795
}
98-
else if (targetType=='F') {
96+
else if (targetType == 'F') {
9997
mv.visitInsn(FCMPL);
10098
mv.visitJumpInsn(IFNE, elseTarget);
10199
}
102-
else if (targetType=='J') {
100+
else if (targetType == 'J') {
103101
mv.visitInsn(LCMP);
104102
mv.visitJumpInsn(IFNE, elseTarget);
105103
}
106-
else if (targetType=='I' || targetType=='Z') {
104+
else if (targetType == 'I' || targetType == 'Z') {
107105
mv.visitJumpInsn(IF_ICMPNE, elseTarget);
108106
}
109107
else {
110-
throw new IllegalStateException("Unexpected descriptor "+leftDesc);
108+
throw new IllegalStateException("Unexpected descriptor " + leftDesc);
111109
}
112110
}
113111
else {
@@ -120,27 +118,27 @@ else if (targetType=='I' || targetType=='Z') {
120118
CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0));
121119
}
122120
Label leftNotNull = new Label();
123-
mv.visitInsn(DUP_X1); // Dup right on the top of the stack
124-
mv.visitJumpInsn(IFNONNULL,leftNotNull);
121+
mv.visitInsn(DUP_X1); // dup right on the top of the stack
122+
mv.visitJumpInsn(IFNONNULL, leftNotNull);
125123
// Right is null!
126124
mv.visitInsn(SWAP);
127-
mv.visitInsn(POP); // remove it
125+
mv.visitInsn(POP); // remove it
128126
Label rightNotNull = new Label();
129127
mv.visitJumpInsn(IFNONNULL, rightNotNull);
130128
// Left is null too
131129
mv.visitInsn(ICONST_1);
132130
mv.visitJumpInsn(GOTO, endOfIf);
133131
mv.visitLabel(rightNotNull);
134132
mv.visitInsn(ICONST_0);
135-
mv.visitJumpInsn(GOTO,endOfIf);
133+
mv.visitJumpInsn(GOTO, endOfIf);
136134
mv.visitLabel(leftNotNull);
137135
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
138136
mv.visitLabel(endOfIf);
139137
cf.pushDescriptor("Z");
140138
return;
141139
}
142140
mv.visitInsn(ICONST_1);
143-
mv.visitJumpInsn(GOTO,endOfIf);
141+
mv.visitJumpInsn(GOTO, endOfIf);
144142
mv.visitLabel(elseTarget);
145143
mv.visitInsn(ICONST_0);
146144
mv.visitLabel(endOfIf);

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -36,12 +36,13 @@ public OpNE(int pos, SpelNodeImpl... operands) {
3636
this.exitTypeDescriptor = "Z";
3737
}
3838

39+
3940
@Override
4041
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
4142
Object left = getLeftOperand().getValueInternal(state).getValue();
4243
Object right = getRightOperand().getValueInternal(state).getValue();
43-
leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
44-
rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
44+
this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
45+
this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
4546
return BooleanTypedValue.forValue(!equalityCheck(state, left, right));
4647
}
4748

@@ -57,7 +58,8 @@ public boolean isCompilable() {
5758

5859
String leftDesc = left.exitTypeDescriptor;
5960
String rightDesc = right.exitTypeDescriptor;
60-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc, leftActualDescriptor, rightActualDescriptor);
61+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
62+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
6163
return (!dc.areNumbers || dc.areCompatible);
6264
}
6365

@@ -70,16 +72,15 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
7072
boolean leftPrim = CodeFlow.isPrimitive(leftDesc);
7173
boolean rightPrim = CodeFlow.isPrimitive(rightDesc);
7274

73-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc, leftActualDescriptor, rightActualDescriptor);
75+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
76+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
7477

7578
if (dc.areNumbers && dc.areCompatible) {
7679
char targetType = dc.compatibleType;
77-
7880
getLeftOperand().generateCode(mv, cf);
7981
if (!leftPrim) {
8082
CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
8183
}
82-
8384
cf.enterCompilationScope();
8485
getRightOperand().generateCode(mv, cf);
8586
cf.exitCompilationScope();
@@ -103,7 +104,7 @@ else if (targetType == 'I' || targetType == 'Z') {
103104
mv.visitJumpInsn(IF_ICMPEQ, elseTarget);
104105
}
105106
else {
106-
throw new IllegalStateException("Unexpected descriptor "+leftDesc);
107+
throw new IllegalStateException("Unexpected descriptor " + leftDesc);
107108
}
108109
}
109110
else {

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -84,6 +84,7 @@ public String toStringAST() {
8484
return sb.toString();
8585
}
8686

87+
8788
protected boolean isCompilableOperatorUsingNumerics() {
8889
SpelNodeImpl left = getLeftOperand();
8990
SpelNodeImpl right= getRightOperand();
@@ -94,8 +95,8 @@ protected boolean isCompilableOperatorUsingNumerics() {
9495
// Supported operand types for equals (at the moment)
9596
String leftDesc = left.exitTypeDescriptor;
9697
String rightDesc = right.exitTypeDescriptor;
97-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
98-
this.leftActualDescriptor, this.rightActualDescriptor);
98+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
99+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
99100
return (dc.areNumbers && dc.areCompatible);
100101
}
101102

@@ -109,9 +110,9 @@ protected void generateComparisonCode(MethodVisitor mv, CodeFlow cf, int compIns
109110

110111
boolean unboxLeft = !CodeFlow.isPrimitive(leftDesc);
111112
boolean unboxRight = !CodeFlow.isPrimitive(rightDesc);
112-
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
113-
this.leftActualDescriptor, this.rightActualDescriptor);
114-
char targetType = dc.compatibleType;//CodeFlow.toPrimitiveTargetDesc(leftDesc);
113+
DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
114+
leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
115+
char targetType = dc.compatibleType; // CodeFlow.toPrimitiveTargetDesc(leftDesc);
115116

116117
getLeftOperand().generateCode(mv, cf);
117118
if (unboxLeft) {
@@ -128,23 +129,23 @@ protected void generateComparisonCode(MethodVisitor mv, CodeFlow cf, int compIns
128129
// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
129130
Label elseTarget = new Label();
130131
Label endOfIf = new Label();
131-
if (targetType=='D') {
132+
if (targetType == 'D') {
132133
mv.visitInsn(DCMPG);
133134
mv.visitJumpInsn(compInstruction1, elseTarget);
134135
}
135-
else if (targetType=='F') {
136+
else if (targetType == 'F') {
136137
mv.visitInsn(FCMPG);
137138
mv.visitJumpInsn(compInstruction1, elseTarget);
138139
}
139-
else if (targetType=='J') {
140+
else if (targetType == 'J') {
140141
mv.visitInsn(LCMP);
141142
mv.visitJumpInsn(compInstruction1, elseTarget);
142143
}
143-
else if (targetType=='I') {
144+
else if (targetType == 'I') {
144145
mv.visitJumpInsn(compInstruction2, elseTarget);
145146
}
146147
else {
147-
throw new IllegalStateException("Unexpected descriptor "+leftDesc);
148+
throw new IllegalStateException("Unexpected descriptor " + leftDesc);
148149
}
149150

150151
// Other numbers are not yet supported (isCompilable will not have returned true)
@@ -215,34 +216,35 @@ else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
215216

216217

217218
/**
218-
* A descriptor comparison encapsulates the result of comparing descriptor for two operands and
219-
* describes at what level they are compatible.
219+
* A descriptor comparison encapsulates the result of comparing descriptor
220+
* for two operands and describes at what level they are compatible.
220221
*/
221222
protected static class DescriptorComparison {
222223

223224
static DescriptorComparison NOT_NUMBERS = new DescriptorComparison(false, false, ' ');
224225

225226
static DescriptorComparison INCOMPATIBLE_NUMBERS = new DescriptorComparison(true, false, ' ');
226227

227-
final boolean areNumbers; // Were the two compared descriptor both for numbers?
228+
final boolean areNumbers; // Were the two compared descriptor both for numbers?
228229

229-
final boolean areCompatible; // If they were numbers, were they compatible?
230+
final boolean areCompatible; // If they were numbers, were they compatible?
231+
232+
final char compatibleType; // When compatible, what is the descriptor of the common type
230233

231-
final char compatibleType; // When compatible, what is the descriptor of the common type
232-
233234
private DescriptorComparison(boolean areNumbers, boolean areCompatible, char compatibleType) {
234235
this.areNumbers = areNumbers;
235236
this.areCompatible = areCompatible;
236237
this.compatibleType = compatibleType;
237238
}
238239

239240
/**
240-
* Returns an object that indicates whether the input descriptors are compatible. A declared descriptor
241-
* is what could statically be determined (e.g. from looking at the return value of a property accessor
242-
* method) whilst an actual descriptor is the type of an actual object that was returned, which may differ.
243-
* For generic types with unbound type variables the declared descriptor discovered may be 'Object' but
244-
* from the actual descriptor it is possible to observe that the objects are really numeric values (e.g.
245-
* ints).
241+
* Return an object that indicates whether the input descriptors are compatible.
242+
* <p>A declared descriptor is what could statically be determined (e.g. from looking
243+
* at the return value of a property accessor method) whilst an actual descriptor
244+
* is the type of an actual object that was returned, which may differ.
245+
* <p>For generic types with unbound type variables, the declared descriptor
246+
* discovered may be 'Object' but from the actual descriptor it is possible to
247+
* observe that the objects are really numeric values (e.g. ints).
246248
* @param leftDeclaredDescriptor the statically determinable left descriptor
247249
* @param rightDeclaredDescriptor the statically determinable right descriptor
248250
* @param leftActualDescriptor the dynamic/runtime left object descriptor

0 commit comments

Comments
 (0)