Skip to content

Commit

Permalink
OGNL-102 Improves performance when null was returned
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Mar 5, 2022
1 parent a518c97 commit e5e2491
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 375 deletions.
9 changes: 7 additions & 2 deletions src/main/java/ognl/ASTChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ public void jjtClose()
protected Object getValueBody(OgnlContext context, Object source)
throws OgnlException
{
Object result = source;
// short-circuit the chain only in case if the root is null
if (source == null) {
return null;
}

Object result = source;
for(int i = 0, ilast = _children.length - 1; i <= ilast; ++i)
{

boolean handled = false;

if (i < ilast) {
Expand Down Expand Up @@ -124,7 +129,7 @@ protected Object getValueBody(OgnlContext context, Object source)
+ "'"); }
}
}
if (!handled)
if (!handled)
{
result = OgnlRuntime.getIndexedProperty(context, result,
propertyNode.getProperty(context, result).toString(),
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,7 @@ public static PropertyAccessor getPropertyAccessor(Class cls)
if (answer != null)
return answer;

throw new OgnlException("No property accessor for class " + cls);
throw new OgnlException("No property accessor for " + getTargetClass(cls).getName());
}

public static ElementsAccessor getElementsAccessor(Class cls)
Expand Down Expand Up @@ -3326,32 +3326,23 @@ private static Object getHandler(Class forClass, ClassCache handlers)
public static Object getProperty(OgnlContext context, Object source, Object name)
throws OgnlException
{
PropertyAccessor accessor;

if (source == null)
{
throw new OgnlException("source is null for getProperty(null, \"" + name + "\")");
}
if ((accessor = getPropertyAccessor(getTargetClass(source))) == null)
{
throw new OgnlException("No property accessor for " + getTargetClass(source).getName());
}

PropertyAccessor accessor = getPropertyAccessor(getTargetClass(source));
return accessor.getProperty(context, source, name);
}

public static void setProperty(OgnlContext context, Object target, Object name, Object value)
throws OgnlException
{
PropertyAccessor accessor;

if (target == null) {
throw new OgnlException("target is null for setProperty(null, \"" + name + "\", " + value + ")");
}
if ((accessor = getPropertyAccessor(getTargetClass(target))) == null) {
throw new OgnlException("No property accessor for " + getTargetClass(target).getName());
}

PropertyAccessor accessor = getPropertyAccessor(getTargetClass(target));
accessor.setProperty(context, target, name, value);
}

Expand Down
417 changes: 211 additions & 206 deletions src/test/java/org/ognl/test/ArithmeticAndLogicalOperatorsTest.java

Large diffs are not rendered by default.

42 changes: 20 additions & 22 deletions src/test/java/org/ognl/test/LambdaExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@
import junit.framework.TestSuite;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;

public class LambdaExpressionTest extends OgnlTestCase {

private static Object[][] TESTS = {
private static final Object[][] TESTS = {
// Lambda expressions
{null, "#a=:[33](20).longValue().{0}.toArray().length", new Integer(33)},
{null, "#fact=:[#this<=1? 1 : #fact(#this-1) * #this], #fact(30)", new Integer(1409286144)},
{null, "#fact=:[#this<=1? 1 : #fact(#this-1) * #this], #fact(30L)", new Long(-8764578968847253504L)},
{new Object[]{}, "#a=:[33](20).longValue().{0}.toArray().length", 33},
{null, "#fact=:[#this<=1? 1 : #fact(#this-1) * #this], #fact(30)", 1409286144},
{null, "#fact=:[#this<=1? 1 : #fact(#this-1) * #this], #fact(30L)", -8764578968847253504L},
{null, "#fact=:[#this<=1? 1 : #fact(#this-1) * #this], #fact(30h)",
new BigInteger("265252859812191058636308480000000")},
{null, "#bump = :[ #this.{ #this + 1 } ], (#bump)({ 1, 2, 3 })",
new ArrayList(Arrays.asList(new Integer[]{new Integer(2), new Integer(3), new Integer(4)}))},
{null, "#bump = :[ #this.{ #this + 1 } ], (#bump)({ 1, 2, 3 })", Arrays.asList(2, 3, 4)},
{null, "#call = :[ \"calling \" + [0] + \" on \" + [1] ], (#call)({ \"x\", \"y\" })", "calling x on y"},

};
Expand All @@ -55,13 +53,18 @@ public class LambdaExpressionTest extends OgnlTestCase {
* =================================================================== Public static methods
* ===================================================================
*/
public static TestSuite suite()
{
public static TestSuite suite() {
TestSuite result = new TestSuite();

for (int i = 0; i < TESTS.length; i++) {
result.addTest(new LambdaExpressionTest((String) TESTS[i][1], TESTS[i][0], (String) TESTS[i][1],
TESTS[i][2]));
for (Object[] test : TESTS) {
result.addTest(
new LambdaExpressionTest(
(String) test[1],
test[0],
(String) test[1],
test[2]
)
);
}
return result;
}
Expand All @@ -70,30 +73,25 @@ public static TestSuite suite()
* =================================================================== Constructors
* ===================================================================
*/
public LambdaExpressionTest()
{
public LambdaExpressionTest() {
super();
}

public LambdaExpressionTest(String name)
{
public LambdaExpressionTest(String name) {
super(name);
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue, Object expectedAfterSetResult)
{
Object setValue, Object expectedAfterSetResult) {
super(name, root, expressionString, expectedResult, setValue, expectedAfterSetResult);
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue)
{
Object setValue) {
super(name, root, expressionString, expectedResult, setValue);
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult)
{
public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult) {
super(name, root, expressionString, expectedResult);
}
}
69 changes: 30 additions & 39 deletions src/test/java/org/ognl/test/ProjectionSelectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,57 @@

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;

public class ProjectionSelectionTest extends OgnlTestCase
{
private static Root ROOT = new Root();
private static Object[][] TESTS = {
public class ProjectionSelectionTest extends OgnlTestCase {

private static final Root ROOT = new Root();

private static final Object[][] TESTS = {
// Projection, selection
{ ROOT, "array.{class}",
Arrays.asList(new Class[] { Integer.class, Integer.class, Integer.class, Integer.class }) },
{ ROOT, "map.array.{? #this > 2 }", Arrays.asList(new Integer[] { new Integer(3), new Integer(4) }) },
{ ROOT, "map.array.{^ #this > 2 }", Arrays.asList(new Integer[] { new Integer(3) }) },
{ ROOT, "map.array.{$ #this > 2 }", Arrays.asList(new Integer[] { new Integer(4) }) },
{ ROOT, "map.array[*].{?true} instanceof java.util.Collection", Boolean.TRUE },
{ null, "#fact=1, 30H.{? #fact = #fact * (#this+1), false }, #fact",
new BigInteger("265252859812191058636308480000000") },
};

/*
* =================================================================== Public static methods
* ===================================================================
*/
public static TestSuite suite()
{
{ROOT, "array.{class}", Arrays.asList(Integer.class, Integer.class, Integer.class, Integer.class)},
{ROOT, "map.array.{? #this > 2 }", Arrays.asList(3, 4)},
{ROOT, "map.array.{^ #this > 2 }", Collections.singletonList(3)},
{ROOT, "map.array.{$ #this > 2 }", Collections.singletonList(4)},
{ROOT, "map.array[*].{?true} instanceof java.util.Collection", Boolean.TRUE},
{ROOT, "#fact=1, 30H.{? #fact = #fact * (#this+1), false }, #fact", new BigInteger("265252859812191058636308480000000")},
};

public static TestSuite suite() {
TestSuite result = new TestSuite();

for(int i = 0; i < TESTS.length; i++) {
result.addTest(new ProjectionSelectionTest((String) TESTS[i][1], TESTS[i][0], (String) TESTS[i][1],
TESTS[i][2]));

for (Object[] test : TESTS) {
result.addTest(
new ProjectionSelectionTest(
(String) test[1],
test[0],
(String) test[1],
test[2]
)
);
}
return result;
}

/*
* =================================================================== Constructors
* ===================================================================
*/
public ProjectionSelectionTest()
{
public ProjectionSelectionTest() {
super();
}

public ProjectionSelectionTest(String name)
{
public ProjectionSelectionTest(String name) {
super(name);
}

public ProjectionSelectionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue, Object expectedAfterSetResult)
{
Object setValue, Object expectedAfterSetResult) {
super(name, root, expressionString, expectedResult, setValue, expectedAfterSetResult);
}

public ProjectionSelectionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue)
{
Object setValue) {
super(name, root, expressionString, expectedResult, setValue);
}

public ProjectionSelectionTest(String name, Object root, String expressionString, Object expectedResult)
{
public ProjectionSelectionTest(String name, Object root, String expressionString, Object expectedResult) {
super(name, root, expressionString, expectedResult);
}
}
Loading

0 comments on commit e5e2491

Please sign in to comment.