Skip to content

Commit

Permalink
TypeConverter / OgnlOps: If we have an array (i.e. Object[]) as targe…
Browse files Browse the repository at this point in the history
…t and a Collection (i.e. ArrayList) as variable convert using Collection.toArray() insted of new Object[] { Collection }
  • Loading branch information
marvin-enthus committed May 17, 2016
1 parent d9edc17 commit 410dee5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/java/ognl/OgnlOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Enumeration;

/**
Expand Down Expand Up @@ -529,6 +530,9 @@ public static Object toArray(Object value, Class toType, boolean preventNulls)
if (toType == Character.TYPE)
return stringValue(value).toCharArray();

if (value instanceof Collection)
return ((Collection)value).toArray((Object[])Array.newInstance(toType, 0));

Object arr = Array.newInstance(toType, 1);
Array.set(arr, 0, convertValue(value, toType, preventNulls));

Expand Down Expand Up @@ -571,7 +575,11 @@ public static Object convertValue(Object value, Class toType, boolean preventNul

result = stringValue(value).toCharArray();
} else if (toType.getComponentType() == Object.class) {
return new Object[] { value };
if (value instanceof Collection) {
Collection vc = (Collection) value;
return vc.toArray(new Object[0]);
} else
return new Object[] { value };
}
} else {
if ((toType == Integer.class) || (toType == Integer.TYPE)) {
Expand Down
18 changes: 11 additions & 7 deletions src/test/java/org/ognl/test/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ public class MethodTest extends OgnlTestCase
{ "testMethods.getBean('TestBean')", ROOT.getTestMethods().getBean("TestBean") } ,
// https://issues.apache.org/jira/browse/OGNL-250 - OnglRuntime getMethodValue fails to find method matching propertyName
{ "testMethods.testProperty", ROOT.getTestMethods().testProperty() } ,
// TODO: some further java <> OGNL inconsistencies: - related to https://github.com/jkuhnert/ognl/issues/16
// Java 'ROOT.getTestMethods().argsTest1(Arrays.asList( ROOT.getOne() )' doesn't compile:
// --> The method argsTest1(Object[]) in the type MethodTestMethods is not applicable for the arguments (List<Integer>)
{ "testMethods.argsTest1({one})", "Array: [[1]]" }, // "Array: [[1]]" means dual-cast is done.
// Java: ROOT.getTestMethods().argsTest(Arrays.asList( ROOT.getOne() ))
// --> The method argsTest2(List<Object>) in the type MethodTestMethods is not applicable for the arguments (List<Integer>)
{ "testMethods.argsTest2({one})", "List: [1]" },
{ "testMethods.argsTest1({one})", ROOT.getTestMethods().argsTest1(Arrays.asList( ROOT.getOne() ).toArray()) }, // toArray() is automatically done by OGNL type conversion
// we need to cast out generics (insert "Object")
{ "testMethods.argsTest2({one})", ROOT.getTestMethods().argsTest2(Arrays.asList( (Object) ROOT.getOne() )) },
// Java 'ROOT.getTestMethods().argsTest1(Arrays.asList( ROOT.getOne() )' doesn't compile:
// --> The method argsTest(Object[]) in the type MethodTestMethods is not applicable for the arguments (List<Integer>)
{ "testMethods.argsTest3({one})", "List: [1]" },
{ "testMethods.showList(testMethods.getObjectList())", ROOT.getTestMethods().showList(ROOT.getTestMethods().getObjectList().toArray()) },
{ "testMethods.showList(testMethods.getStringList())", ROOT.getTestMethods().showList(ROOT.getTestMethods().getStringList().toArray()) },
{ "testMethods.showList(testMethods.getStringArray())", ROOT.getTestMethods().showList(ROOT.getTestMethods().getStringArray()) },
// TODO This one doesn't work - even 'toArray(new String[0]) returns Object[] and so the wrong method is called - currently no idea how to handle this...
// { "testMethods.showList(testMethods.getStringList().toArray(new String[0]))", ROOT.getTestMethods().showList(ROOT.getTestMethods().getStringList().toArray(new String[0])) },
// but this one works - at least in interpretation mode...
{ "testMethods.showStringList(testMethods.getStringList().toArray(new String[0]))", ROOT.getTestMethods().showStringList(ROOT.getTestMethods().getStringList().toArray(new String[0])) },

// https://github.com/jkuhnert/ognl/issues/23 - Exception selecting overloaded method in 3.1.4
{ "testMethods.avg({ 5, 5 })", ROOT.getTestMethods().avg((List)Arrays.asList(5, 5)) },
};
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/ognl/test/objects/MethodTestMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,28 @@ public double avg(final double[] target) {
}
return total/target.length;
}

public String[] getStringArray() {
return new String[] { "Hello", "World" };
}

public List<String> getStringList() {
return Arrays.asList("Hello", "World");
}

public List<Object> getObjectList() {
return Arrays.asList((Object)"Object");
}

public String showList(String[] args) {
return "Strings: " + Arrays.toString(args);
}

public String showList(Object[] args) {
return "Objects: " + Arrays.toString(args);
}

public String showStringList(String[] args) {
return "Strings: " + Arrays.toString(args);
}
}

0 comments on commit 410dee5

Please sign in to comment.