From f59b2360651809118a05a4715663656d40b14a87 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sun, 8 Nov 2020 18:52:55 +0100 Subject: [PATCH] Adds support for null varargs Closes #112 --- src/main/java/ognl/Ognl.java | 9 ++++- src/main/java/ognl/OgnlRuntime.java | 24 ++++++++----- src/test/java/org/ognl/test/ChainTest.java | 15 ++++++++ src/test/java/org/ognl/test/MethodTest.java | 11 ++++++ .../java/org/ognl/test/VarArgsMethodTest.java | 36 +++++++++++++++++++ 5 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/ognl/test/VarArgsMethodTest.java diff --git a/src/main/java/ognl/Ognl.java b/src/main/java/ognl/Ognl.java index 1080d768..4c37b3ee 100644 --- a/src/main/java/ognl/Ognl.java +++ b/src/main/java/ognl/Ognl.java @@ -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); } /** diff --git a/src/main/java/ognl/OgnlRuntime.java b/src/main/java/ognl/OgnlRuntime.java index e13102de..98379fd7 100644 --- a/src/main/java/ognl/OgnlRuntime.java +++ b/src/main/java/ognl/OgnlRuntime.java @@ -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; @@ -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); + } } } } @@ -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; diff --git a/src/test/java/org/ognl/test/ChainTest.java b/src/test/java/org/ognl/test/ChainTest.java index a652c0fc..cff794a5 100644 --- a/src/test/java/org/ognl/test/ChainTest.java +++ b/src/test/java/org/ognl/test/ChainTest.java @@ -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; diff --git a/src/test/java/org/ognl/test/MethodTest.java b/src/test/java/org/ognl/test/MethodTest.java index 6db3440b..6af7b064 100644 --- a/src/test/java/org/ognl/test/MethodTest.java +++ b/src/test/java/org/ognl/test/MethodTest.java @@ -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 @@ -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") } , @@ -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() diff --git a/src/test/java/org/ognl/test/VarArgsMethodTest.java b/src/test/java/org/ognl/test/VarArgsMethodTest.java new file mode 100644 index 00000000..aa4a6ba9 --- /dev/null +++ b/src/test/java/org/ognl/test/VarArgsMethodTest.java @@ -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); + } + +}