Skip to content

Commit

Permalink
Adds support for null varargs
Browse files Browse the repository at this point in the history
Closes #112
  • Loading branch information
lukaszlenart committed Nov 14, 2020
1 parent 75a45d3 commit f59b236
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/main/java/ognl/Ognl.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ public static Map createDefaultContext(Object root, MemberAccess memberAccess)
@Deprecated
public static Map addDefaultContext(Object root, Map context)
{
return addDefaultContext(root, null, null, null, context);
MemberAccess memberAccess = new AbstractMemberAccess() {
@Override
public boolean isAccessible(Map context, Object target, Member member, String propertyName) {
int modifiers = member.getModifiers();
return Modifier.isPublic(modifiers);
}
};
return addDefaultContext(root, memberAccess, null, null, context);
}

/**
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1491,10 +1491,14 @@ public static ArgsCompatbilityReport areArgsCompatible(Class[] args, Class[] cla
boolean varArgs = m != null && isJdk15() && m.isVarArgs();

if ( args==null || args.length == 0 ) { // handle methods without arguments
if ( classes == null || classes.length == 0 )
if ( classes == null || classes.length == 0 ) {
return NoArgsReport;
else
} else {
if (varArgs) {
return NoArgsReport;
}
return null;
}
}
if (args.length != classes.length && !varArgs) {
return null;
Expand Down Expand Up @@ -1717,13 +1721,13 @@ public static Method getAppropriateMethod(OgnlContext context, Object source, Ob
Class[] mParameterTypes = mm.mParameterTypes;
System.arraycopy(args, 0, actualArgs, 0, args.length);

for (int j = 0; j < mParameterTypes.length; j++)
{
Class type = mParameterTypes[j];
if (actualArgs.length > 0) {
for (int j = 0; j < mParameterTypes.length; j++) {
Class type = mParameterTypes[j];

if (mm.report.conversionNeeded[j] || (type.isPrimitive() && (actualArgs[j] == null)))
{
actualArgs[j] = getConvertedType(context, source, result, propertyName, args[j], type);
if (mm.report.conversionNeeded[j] || (type.isPrimitive() && (actualArgs[j] == null))) {
actualArgs[j] = getConvertedType(context, source, result, propertyName, args[j], type);
}
}
}
}
Expand Down Expand Up @@ -1937,7 +1941,9 @@ public static Object callAppropriateMethod(OgnlContext context, Object source, O
if (parmTypes[i].isArray())
{
convertedArgs = new Object[i + 1];
System.arraycopy(actualArgs, 0, convertedArgs, 0, convertedArgs.length);
if (actualArgs.length > 0) {
System.arraycopy(actualArgs, 0, convertedArgs, 0, convertedArgs.length);
}

Object[] varArgs;

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/ognl/test/ChainTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2020 OGNL Contributors
*
* 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.
*/
package org.ognl.test;

import junit.framework.TestCase;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/ognl/test/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import junit.framework.TestSuite;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import ognl.Ognl;
import ognl.OgnlException;
import org.ognl.test.objects.*;

public class MethodTest extends OgnlTestCase
Expand All @@ -60,6 +63,7 @@ public class MethodTest extends OgnlTestCase
{ LIST, "addValue(name)", Boolean.TRUE},
{ "getDisplayValue(methodsTest.allowDisplay)", "test"},
{ "isThisVarArgsWorking(three, rootValue)", Boolean.TRUE},
{ "isThisVarArgsWorking()", Boolean.TRUE },
{ GENERIC, "service.getFullMessageFor(value, null)", "Halo 3"},
// TestCase for https://github.com/jkuhnert/ognl/issues/17 - ArrayIndexOutOfBoundsException when trying to access BeanFactory
{ "testMethods.getBean('TestBean')", ROOT.getTestMethods().getBean("TestBean") } ,
Expand All @@ -83,6 +87,13 @@ public class MethodTest extends OgnlTestCase
{ "testMethods.avg({ 5, 5 })", ROOT.getTestMethods().avg((List)Arrays.asList(5, 5)) },
};

public void testNullVarArgs() throws OgnlException {
Object value = Ognl.getValue("isThisVarArgsWorking()", new HashMap(), ROOT);

assertTrue(value instanceof Boolean);
assertTrue((Boolean) value);
}

public static class A
{
public boolean isProperty()
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/ognl/test/VarArgsMethodTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 OGNL Contributors
*
* 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.
*/
package org.ognl.test;

import junit.framework.TestCase;
import ognl.Ognl;
import ognl.OgnlException;
import org.ognl.test.objects.Simple;

import java.util.HashMap;

public class VarArgsMethodTest extends TestCase {

private static Simple ROOT = new Simple();

public void testNullVarArgs() throws OgnlException {
Object value = Ognl.getValue("isThisVarArgsWorking()", new HashMap(), ROOT);

assertTrue(value instanceof Boolean);
assertTrue((Boolean) value);
}

}

0 comments on commit f59b236

Please sign in to comment.