short-circuit evaluation of logic operators in SpEL [SPRNET-1381]

This commit is contained in:
bbaia
2010-10-18 08:40:11 +00:00
parent 5c4ad017a9
commit f49ab125f7
6 changed files with 210 additions and 174 deletions

View File

@@ -1,85 +1,93 @@
#region License
/*
* Copyright <20> 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
#region License
/*
* Copyright <20> 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Runtime.Serialization;
using Spring.Util;
namespace Spring.Expressions
{
/// <summary>
/// Represents OR operator (both, bitwise and logical).
/// </summary>
/// <author>Aleksandar Seovic</author>
[Serializable]
public class OpOR : BinaryOperator
{
/// <summary>
/// Create a new instance
/// </summary>
public OpOR():base()
{
}
/// <summary>
/// Create a new instance
/// </summary>
public OpOR(BaseNode left, BaseNode right)
:base(left, right)
{
}
/// <summary>
/// Create a new instance from SerializationInfo
/// </summary>
protected OpOR(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
/// <summary>
/// Returns a value for the logical OR operator node.
/// </summary>
/// <param name="context">Context to evaluate expressions against.</param>
/// <param name="evalContext">Current expression evaluation context.</param>
using Spring.Util;
namespace Spring.Expressions
{
/// <summary>
/// Represents OR operator (both, bitwise and logical).
/// </summary>
/// <author>Aleksandar Seovic</author>
[Serializable]
public class OpOR : BinaryOperator
{
/// <summary>
/// Create a new instance
/// </summary>
public OpOR():base()
{
}
/// <summary>
/// Create a new instance
/// </summary>
public OpOR(BaseNode left, BaseNode right)
:base(left, right)
{
}
/// <summary>
/// Create a new instance from SerializationInfo
/// </summary>
protected OpOR(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
/// <summary>
/// Returns a value for the logical OR operator node.
/// </summary>
/// <param name="context">Context to evaluate expressions against.</param>
/// <param name="evalContext">Current expression evaluation context.</param>
/// <returns>Node's value.</returns>
protected override object Get(object context, EvaluationContext evalContext)
protected override object Get(object context, EvaluationContext evalContext)
{
object l = GetLeftValue(context, evalContext);
object r = GetRightValue(context, evalContext);
if (NumberUtils.IsInteger(l))
{
object r = GetRightValue(context, evalContext);
if (NumberUtils.IsInteger(r))
{
return NumberUtils.BitwiseOr(l, r);
}
}
else if (l is Enum)
{
object r = GetRightValue(context, evalContext);
if (l.GetType() == r.GetType())
{
Type enumType = l.GetType();
Type integralType = Enum.GetUnderlyingType(enumType);
l = Convert.ChangeType(l, integralType);
r = Convert.ChangeType(r, integralType);
object result = NumberUtils.BitwiseOr(l, r);
return Enum.ToObject(enumType, result);
}
}
if (NumberUtils.IsInteger(l) && NumberUtils.IsInteger(r))
{
return NumberUtils.BitwiseOr(l, r);
}
else if (l is Enum && l.GetType() == r.GetType())
{
Type enumType = l.GetType();
Type integralType = Enum.GetUnderlyingType(enumType);
l = Convert.ChangeType(l, integralType);
r = Convert.ChangeType(r, integralType);
object result = NumberUtils.BitwiseOr(l, r);
return Enum.ToObject(enumType, result);
}
else
return Convert.ToBoolean(l) || Convert.ToBoolean(r);
}
}
return Convert.ToBoolean(l) ||
Convert.ToBoolean(GetRightValue(context, evalContext));
}
}
}

View File

@@ -1,19 +1,19 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
@@ -30,40 +30,40 @@ namespace Spring.Expressions
[Serializable]
public class OpXOR : BinaryOperator
{
/// <summary>
/// Create a new instance
/// </summary>
/// <summary>
/// Create a new instance
/// </summary>
public OpXOR()
{ }
/// <summary>
/// Create a new instance
/// </summary>
public OpXOR(BaseNode left, BaseNode right)
:base(left, right)
{
}
/// <summary>
/// Create a new instance from SerializationInfo
/// <summary>
/// Create a new instance
/// </summary>
protected OpXOR(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
/// <summary>
/// Returns a value for the logical AND operator node.
/// </summary>
/// <param name="context">Context to evaluate expressions against.</param>
/// <param name="evalContext">Current expression evaluation context.</param>
public OpXOR(BaseNode left, BaseNode right)
:base(left, right)
{
}
/// <summary>
/// Create a new instance from SerializationInfo
/// </summary>
protected OpXOR(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
/// <summary>
/// Returns a value for the logical AND operator node.
/// </summary>
/// <param name="context">Context to evaluate expressions against.</param>
/// <param name="evalContext">Current expression evaluation context.</param>
/// <returns>Node's value.</returns>
protected override object Get(object context, EvaluationContext evalContext)
{
object l = GetLeftValue(context, evalContext);
object r = GetRightValue(context, evalContext);
if (NumberUtils.IsInteger(l) && NumberUtils.IsInteger(r))
protected override object Get(object context, EvaluationContext evalContext)
{
object l = GetLeftValue(context, evalContext);
object r = GetRightValue(context, evalContext);
if (NumberUtils.IsInteger(l) && NumberUtils.IsInteger(r))
{
return NumberUtils.BitwiseXor(l, r);
}
@@ -76,7 +76,7 @@ namespace Spring.Expressions
object result = NumberUtils.BitwiseXor(l, r);
return Enum.ToObject(enumType, result);
}
return Convert.ToBoolean(l) ^ Convert.ToBoolean(r);
return Convert.ToBoolean(l) ^ Convert.ToBoolean(r);
}
}
}

View File

@@ -272,13 +272,6 @@ namespace Spring.Expressions
#endregion Serialization Tests
[Test(Description = "SPRNET-1381")]
public void TestShortcircuitAndOperator()
{
object boolean = ExpressionEvaluator.GetValue(new Inventor(), "Name != null and Name.Length == 0");
Assert.AreEqual(false, boolean);
}
[Test]
public void TestBitwiseXOR()
{

View File

@@ -1,19 +1,19 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
@@ -23,6 +23,7 @@ using NUnit.Framework;
namespace Spring.Expressions
{
/// <summary>
/// Unit tests for the OpAND class.
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
@@ -34,5 +35,22 @@ namespace Spring.Expressions
OpAND band = new OpAND(new IntLiteralNode("2"), new IntLiteralNode("3"));
Assert.AreEqual( 2 & 3, band.GetValue(null,null) );
}
[Test]
public void AndsBooleans()
{
OpAND band1 = new OpAND(new BooleanLiteralNode("true"), new BooleanLiteralNode("true"));
Assert.AreEqual(true, band1.GetValue(null, null));
OpAND band2 = new OpAND(new BooleanLiteralNode("true"), new BooleanLiteralNode("false"));
Assert.AreEqual(false, band2.GetValue(null, null));
}
[Test(Description = "SPRNET-1381")]
public void TestShortcircuitAndOperator()
{
object boolean = ExpressionEvaluator.GetValue(new Inventor(), "Name != null and Name.Length == 0");
Assert.AreEqual(false, boolean);
}
}
}

View File

@@ -1,19 +1,19 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
@@ -24,6 +24,7 @@ using NUnit.Framework;
namespace Spring.Expressions
{
/// <summary>
/// Unit tests for the OpOR class.
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
@@ -42,5 +43,12 @@ namespace Spring.Expressions
OpOR bor = new OpOR(new BooleanLiteralNode("false"), new BooleanLiteralNode("true"));
Assert.AreEqual(false || true , bor.GetValue(null, null));
}
[Test(Description = "SPRNET-1381")]
public void TestShortcircuitOrOperator()
{
object boolean = ExpressionEvaluator.GetValue(new Inventor(), "Name == null or Name.Length == 0");
Assert.AreEqual(true, boolean);
}
}
}

View File

@@ -1,19 +1,19 @@
#region License
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
@@ -23,6 +23,7 @@ using NUnit.Framework;
namespace Spring.Expressions
{
/// <summary>
/// Unit tests for the OpXOR class.
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
@@ -33,8 +34,16 @@ namespace Spring.Expressions
{
OpXOR bxor = new OpXOR(new IntLiteralNode("2"), new IntLiteralNode("3"));
Assert.AreEqual(2 ^ 3, bxor.GetValue(null, null));
bxor = new OpXOR(new BooleanLiteralNode("true"), new BooleanLiteralNode("false"));
Assert.AreEqual(true ^ false, bxor.GetValue(null, null));
}
[Test]
public void XorsBooleans()
{
OpXOR bxor1 = new OpXOR(new BooleanLiteralNode("true"), new BooleanLiteralNode("false"));
Assert.AreEqual(true ^ false, bxor1.GetValue(null, null));
OpXOR bxor2 = new OpXOR(new BooleanLiteralNode("true"), new BooleanLiteralNode("true"));
Assert.AreEqual(true ^ true, bxor2.GetValue(null, null));
}
}
}