1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.expression.spel.ast;
18
19 import org.springframework.asm.Label;
20 import org.springframework.asm.MethodVisitor;
21 import org.springframework.expression.EvaluationException;
22 import org.springframework.expression.spel.CodeFlow;
23 import org.springframework.expression.spel.ExpressionState;
24 import org.springframework.expression.spel.SpelEvaluationException;
25 import org.springframework.expression.spel.SpelMessage;
26 import org.springframework.expression.spel.support.BooleanTypedValue;
27
28
29
30
31
32
33
34
35
36 public class OperatorNot extends SpelNodeImpl {
37
38 public OperatorNot(int pos, SpelNodeImpl operand) {
39 super(pos, operand);
40 this.exitTypeDescriptor = "Z";
41 }
42
43
44 @Override
45 public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
46 try {
47 Boolean value = this.children[0].getValue(state, Boolean.class);
48 if (value == null) {
49 throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
50 }
51 return BooleanTypedValue.forValue(!value);
52 }
53 catch (SpelEvaluationException ex) {
54 ex.setPosition(getChild(0).getStartPosition());
55 throw ex;
56 }
57 }
58
59 @Override
60 public String toStringAST() {
61 return "!" + getChild(0).toStringAST();
62 }
63
64 @Override
65 public boolean isCompilable() {
66 SpelNodeImpl child = this.children[0];
67 return (child.isCompilable() && CodeFlow.isBooleanCompatible(child.exitTypeDescriptor));
68 }
69
70 @Override
71 public void generateCode(MethodVisitor mv, CodeFlow cf) {
72 this.children[0].generateCode(mv, cf);
73 cf.unboxBooleanIfNecessary(mv);
74 Label elseTarget = new Label();
75 Label endOfIf = new Label();
76 mv.visitJumpInsn(IFNE,elseTarget);
77 mv.visitInsn(ICONST_1);
78 mv.visitJumpInsn(GOTO,endOfIf);
79 mv.visitLabel(elseTarget);
80 mv.visitInsn(ICONST_0);
81 mv.visitLabel(endOfIf);
82 cf.pushDescriptor(this.exitTypeDescriptor);
83 }
84
85 }