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 May 18, 2023
1 parent fc879b1 commit 3c76136
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 175 deletions.
6 changes: 5 additions & 1 deletion src/main/java/ognl/ASTChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ 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;

Expand Down
143 changes: 98 additions & 45 deletions src/test/java/ognl/test/LambdaExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,117 @@
*/
package ognl.test;

import junit.framework.TestSuite;
import ognl.DefaultMemberAccess;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.SimpleNode;
import org.junit.Before;
import org.junit.Test;

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

public class LambdaExpressionTest extends OgnlTestCase {

private static 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)},
{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, "#call = :[ \"calling \" + [0] + \" on \" + [1] ], (#call)({ \"x\", \"y\" })", "calling x on y"},

};

/*
* =================================================================== Public static methods
* ===================================================================
*/
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]));
}
return result;
import static org.junit.Assert.assertEquals;

public class LambdaExpressionTest {

private OgnlContext context;

@Before
public void setUp() throws Exception {
this.context = Ognl.createDefaultContext(null, new DefaultMemberAccess(false));
}

private SimpleNode getExpression(Object root, String expressionStr) throws Exception {
// validate expression
Ognl.parseExpression(expressionStr);
// compile expression
return (SimpleNode) Ognl.compileExpression(context, root, expressionStr);
}

/*
* =================================================================== Constructors
* ===================================================================
*/
public LambdaExpressionTest() {
super();
@Test
public void shouldReadArrayLength() throws Exception {
// given
Object root = new Object[]{};
String expressionStr = "#a=:[33](20).longValue().{0}.toArray().length";
int expectedResult = 33;

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

@Test
public void shouldEvaluateLambda1() throws Exception {
// given
Object root = null;
String expressionStr = "#fact=:[#this <=1 ? 1 : #fact(#this-1) * #this], #fact(30)";
int expectedResult = 1409286144;

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

public LambdaExpressionTest(String name) {
super(name);
@Test
public void shouldEvaluateLambda2() throws Exception {
// given
Object root = null;
String expressionStr = "#fact=:[#this <= 1 ? 1 : #fact(#this-1) * #this], #fact(30L)";
long expectedResult = -8764578968847253504L;

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue, Object expectedAfterSetResult) {
super(name, root, expressionString, expectedResult, setValue, expectedAfterSetResult);
@Test
public void shouldEvaluateLambda3() throws Exception {
// given
Object root = null;
String expressionStr = "#fact=:[#this <= 1 ? 1 : #fact(#this-1) * #this], #fact(30h)";
BigInteger expectedResult = new BigInteger("265252859812191058636308480000000");

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult,
Object setValue) {
super(name, root, expressionString, expectedResult, setValue);
@Test
public void shouldEvaluateLambda4() throws Exception {
// given
Object root = null;
String expressionStr = "#bump = :[ #this.{ #this + 1 } ], (#bump)({ 1, 2, 3 })";
List<Integer> expectedResult = Arrays.asList(2, 3, 4);

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

public LambdaExpressionTest(String name, Object root, String expressionString, Object expectedResult) {
super(name, root, expressionString, expectedResult);
@Test
public void shouldEvaluateLambda5() throws Exception {
// given
Object root = null;
String expressionStr = "#call = :[ \"calling \" + [0] + \" on \" + [1] ], (#call)({ \"x\", \"y\" })";
String expectedResult = "calling x on y";

// when
SimpleNode expression = getExpression(root, expressionStr);

// then
assertEquals(expectedResult, Ognl.getValue(expression, context, root));
}

}
96 changes: 96 additions & 0 deletions src/test/java/ognl/test/PropertyAccessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package ognl.test;

import ognl.DefaultMemberAccess;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class PropertyAccessorTest {

private OgnlContext context;

@Test
public void shouldAccessProperty_usingCustomAccessor() throws Exception {
// given
Parent root = new Parent(new Child("Luk"));
String expectedResult = "Luk";

// then
assertEquals(expectedResult, Ognl.getValue("child", context, root));
}

@Before
public void setUp() {
this.context = Ognl.createDefaultContext(null, new DefaultMemberAccess(false));
OgnlRuntime.setPropertyAccessor(Parent.class, new ChildPropertyAccessor());
}

public static class Child {
String name;

public Child(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public static class Parent {
Child child;

public Parent(Child child) {
this.child = child;
}

public Child getChild() {
return child;
}
}

public static class ChildPropertyAccessor implements PropertyAccessor {
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
}

public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException {
if (target instanceof Parent && "child".equals(name)) {
return OgnlRuntime.getProperty(context, ((Parent) target).getChild(), "name");
}
return null;
}

public String getSourceAccessor(OgnlContext context, Object target, Object index) {
return index.toString();
}

public String getSourceSetter(OgnlContext context, Object target, Object index) {
return index.toString();
}
}

}
Loading

0 comments on commit 3c76136

Please sign in to comment.