001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.expression.spel.ast; 018 019import org.springframework.asm.Label; 020import org.springframework.asm.MethodVisitor; 021import org.springframework.expression.EvaluationException; 022import org.springframework.expression.spel.CodeFlow; 023import org.springframework.expression.spel.ExpressionState; 024import org.springframework.expression.spel.SpelEvaluationException; 025import org.springframework.expression.spel.SpelMessage; 026import org.springframework.expression.spel.support.BooleanTypedValue; 027 028/** 029 * Represents a NOT operation. 030 * 031 * @author Andy Clement 032 * @author Mark Fisher 033 * @author Oliver Becker 034 * @since 3.0 035 */ 036public class OperatorNot extends SpelNodeImpl { // Not is a unary operator so does not extend BinaryOperator 037 038 public OperatorNot(int pos, SpelNodeImpl operand) { 039 super(pos, operand); 040 this.exitTypeDescriptor = "Z"; 041 } 042 043 044 @Override 045 public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { 046 try { 047 Boolean value = this.children[0].getValue(state, Boolean.class); 048 if (value == null) { 049 throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean"); 050 } 051 return BooleanTypedValue.forValue(!value); 052 } 053 catch (SpelEvaluationException ex) { 054 ex.setPosition(getChild(0).getStartPosition()); 055 throw ex; 056 } 057 } 058 059 @Override 060 public String toStringAST() { 061 return "!" + getChild(0).toStringAST(); 062 } 063 064 @Override 065 public boolean isCompilable() { 066 SpelNodeImpl child = this.children[0]; 067 return (child.isCompilable() && CodeFlow.isBooleanCompatible(child.exitTypeDescriptor)); 068 } 069 070 @Override 071 public void generateCode(MethodVisitor mv, CodeFlow cf) { 072 this.children[0].generateCode(mv, cf); 073 cf.unboxBooleanIfNecessary(mv); 074 Label elseTarget = new Label(); 075 Label endOfIf = new Label(); 076 mv.visitJumpInsn(IFNE,elseTarget); 077 mv.visitInsn(ICONST_1); // TRUE 078 mv.visitJumpInsn(GOTO,endOfIf); 079 mv.visitLabel(elseTarget); 080 mv.visitInsn(ICONST_0); // FALSE 081 mv.visitLabel(endOfIf); 082 cf.pushDescriptor(this.exitTypeDescriptor); 083 } 084 085}