Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves problem with setting varargs parameter #92

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1913,8 +1913,12 @@ public static Object callAppropriateMethod(OgnlContext context, Object source, O
{
varArgs = new Object[0];
}

convertedArgs[i] = varArgs;
// If this is the only parameter, explode the array
if (actualArgs.length == 1) {
convertedArgs = varArgs;
} else { // there are more parameters, varargs is the last one
convertedArgs[i] = varArgs;
}
break;
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/ognl/OgnlRuntimeTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ognl;

import org.hamcrest.core.IsEqual;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -500,4 +502,50 @@ public void testUseFirstMatchGetSetStateFlag() {
optionDefinedInEnvironment ? flagValueFromEnvironment : defaultValue, OgnlRuntime.getUseFirstMatchGetSetLookupValue());
}

private Map defaultContext = Ognl.createDefaultContext(null, new DefaultMemberAccess(false));

@Test // Success
public void testForArray() throws Exception {
Bean bean = new Bean();
Ognl.setValue("chars", defaultContext, bean, new Character[]{'%', '_'});
Assert.assertThat(bean.chars.length, IsEqual.equalTo(2));
Assert.assertThat(bean.chars[0], IsEqual.equalTo('%'));
Assert.assertThat(bean.chars[1], IsEqual.equalTo('_'));
}

@Test // Fail
public void testForVarArgs() throws Exception {
Bean bean = new Bean();
Ognl.setValue("strings", defaultContext, bean, new String[]{"%", "_"});
Assert.assertThat(bean.strings.length, IsEqual.equalTo(2));
Assert.assertThat(bean.strings[0], IsEqual.equalTo("%"));
Assert.assertThat(bean.strings[1], IsEqual.equalTo("_"));
}

static class Bean {
private Character[] chars;
private Integer index;
private String[] strings;

public void setChars(Character[] chars) {
this.chars = chars;
}
public Character[] getChars() {
return chars;
}
public void setStrings(String... strings) {
this.strings = strings;
}
public String[] getStrings() {
return strings;
}
public void setMix(Integer index, String... strings) {
this.index = index;
this.strings = strings;
}
public Integer getIndex() {
return index;
}
}

}