예제 #1
0
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var rightOperand = RightOperand;
            var left         = LeftOperand.GetValueInternal(state);
            var right        = rightOperand.GetValueInternal(state);
            var leftValue    = left.Value;
            var rightValue   = right.Value;
            BooleanTypedValue result;

            if (!(rightValue is Type))
            {
                throw new SpelEvaluationException(RightOperand.StartPosition, SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND, rightValue == null ? "null" : rightValue.GetType().FullName);
            }

            var rightClass = (Type)rightValue;

            if (leftValue == null)
            {
                result = BooleanTypedValue.FALSE;  // null is not an instanceof anything
            }
            else
            {
                result = BooleanTypedValue.ForValue(rightClass.IsAssignableFrom(leftValue.GetType()));
            }

            _type = rightClass;
            if (rightOperand is TypeReference)
            {
                // Can only generate bytecode where the right operand is a direct type reference,
                // not if it is indirect (for example when right operand is a variable reference)
                _exitTypeDescriptor = "Z";
            }

            return(result);
        }
예제 #2
0
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left  = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            if (!(right is IList) || ((IList)right).Count != 2)
            {
                throw new SpelEvaluationException(RightOperand.StartPosition, SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
            }

            var list = (IList)right;
            var low  = list[0];
            var high = list[1];
            var comp = state.TypeComparator;

            try
            {
                return(BooleanTypedValue.ForValue(comp.Compare(left, low) >= 0 && comp.Compare(left, high) <= 0));
            }
            catch (SpelEvaluationException ex)
            {
                ex.Position = StartPosition;
                throw;
            }
        }
예제 #3
0
파일: OpNE.cs 프로젝트: kimbell/Steeltoe
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftValue  = LeftOperand.GetValueInternal(state).Value;
            var rightValue = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor  = CodeFlow.ToDescriptorFromObject(leftValue);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(rightValue);
            return(BooleanTypedValue.ForValue(!EqualityCheck(state.EvaluationContext, leftValue, rightValue)));
        }
예제 #4
0
파일: OpAnd.cs 프로젝트: kimbell/Steeltoe
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            if (!GetBooleanValue(state, LeftOperand))
            {
                // no need to evaluate right operand
                return(BooleanTypedValue.FALSE);
            }

            return(BooleanTypedValue.ForValue(GetBooleanValue(state, RightOperand)));
        }
예제 #5
0
 public override ITypedValue GetValueInternal(ExpressionState state)
 {
     try
     {
         var value = _children[0].GetValue <bool>(state);
         return(BooleanTypedValue.ForValue(!value));
     }
     catch (SpelEvaluationException ex)
     {
         ex.Position = GetChild(0).StartPosition;
         throw;
     }
     catch (Exception ex)
     {
         throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, ex, "null", "System.Boolean");
     }
 }
예제 #6
0
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOp  = LeftOperand;
            var rightOp = RightOperand;
            var left    = leftOp.GetValue <string>(state);
            var right   = RightOperand.GetValue(state);

            if (left == null)
            {
                throw new SpelEvaluationException(leftOp.StartPosition, SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, (object)null);
            }

            if (!(right is string))
            {
                throw new SpelEvaluationException(rightOp.StartPosition, SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
            }

            try
            {
                var rightString = (string)right;
                _patternCache.TryGetValue(rightString, out var pattern);
                if (pattern == null)
                {
                    pattern = new Regex(rightString, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
                    _patternCache.TryAdd(rightString, pattern);
                }

                return(BooleanTypedValue.ForValue(pattern.IsMatch(left)));
            }
            catch (ArgumentException ex)
            {
                throw new SpelEvaluationException(rightOp.StartPosition, ex, SpelMessage.INVALID_PATTERN, right);
            }
            catch (RegexMatchTimeoutException ex)
            {
                throw new SpelEvaluationException(rightOp.StartPosition, ex, SpelMessage.FLAWED_PATTERN, right);
            }
        }
예제 #7
0
 public BooleanLiteral(string payload, int startPos, int endPos, bool value)
     : base(payload, startPos, endPos)
 {
     _value = BooleanTypedValue.ForValue(value);
     _exitTypeDescriptor = "Z";
 }
예제 #8
0
파일: OpLE.cs 프로젝트: kimbell/Steeltoe
        public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left  = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor  = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv  = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal  = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) <= 0));
                }
                else if (left is double || right is double)
                {
                    var leftVal  = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is float || right is float)
                {
                    var leftVal  = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is long || right is long)
                {
                    var leftVal  = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is int || right is int)
                {
                    var leftVal  = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is short || right is short)
                {
                    var leftVal  = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is byte || right is byte)
                {
                    var leftVal  = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal  = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is uint || right is uint)
                {
                    var leftVal  = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal  = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal  = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return(BooleanTypedValue.ForValue(leftVal <= rightVal));
                }
            }

            return(BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) <= 0));
        }