Skip to content

Commit c8fff38

Browse files
committed
1 parent 4695414 commit c8fff38

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/main/java/org/apache/ibatis/builder/xml/dynamic/ExpressionEvaluator.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.apache.ibatis.ognl.Ognl;
66
import org.apache.ibatis.ognl.OgnlException;
77

8+
import java.lang.reflect.Array;
89
import java.math.BigDecimal;
9-
import java.util.Arrays;
10+
import java.util.ArrayList;
11+
import java.util.List;
1012

1113
public class ExpressionEvaluator {
1214

@@ -21,12 +23,24 @@ public boolean evaluateBoolean(String expression, Object parameterObject) {
2123
}
2224
}
2325

24-
public Iterable evaluateIterable(String expression, Object parameterObject) {
26+
public Iterable<?> evaluateIterable(String expression, Object parameterObject) {
2527
try {
2628
Object value = Ognl.getValue(expression, parameterObject);
2729
if (value == null) throw new SqlMapperException("The expression '" + expression + "' evaluated to a null value.");
28-
if (value instanceof Iterable) return (Iterable) value;
29-
if (value.getClass().isArray()) return Arrays.asList((Object[]) value);
30+
if (value instanceof Iterable) return (Iterable<?>) value;
31+
if (value.getClass().isArray()) {
32+
// the array may be primitive, so Arrays.asList() may throw
33+
// a ClassCastException (issue 209). Do the work manually
34+
// Curse primitives! :) (JGB)
35+
int size = Array.getLength(value);
36+
List<Object> answer = new ArrayList<Object>();
37+
for (int i = 0; i < size; i++) {
38+
Object o = Array.get(value, i);
39+
answer.add(o);
40+
}
41+
42+
return answer;
43+
}
3044
throw new BuilderException("Error evaluating expression '" + expression + "'. Return value (" + value + ") was not iterable.");
3145
} catch (OgnlException e) {
3246
throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e);

src/test/java/org/apache/ibatis/submitted/nested/NestedForEach.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
</foreach>
2727
</where>
2828
</select>
29+
30+
<select id="simpleSelectWithPrimitives" parameterType="map" resultType="map">
31+
select *
32+
from names
33+
<where>
34+
<foreach collection="ids" item="id" separator="," open="id in (" close=")">
35+
${id}
36+
</foreach>
37+
</where>
38+
</select>
2939
</mapper>

src/test/java/org/apache/ibatis/submitted/nested/NestedForEachTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.Reader;
1414
import java.sql.Connection;
1515
import java.sql.DriverManager;
16+
import java.util.HashMap;
1617
import java.util.List;
1718
import java.util.Map;
1819

@@ -67,6 +68,24 @@ public void testSimpleSelect() {
6768
}
6869
}
6970

71+
@Test
72+
@SuppressWarnings("unchecked")
73+
public void testSimpleSelectWithPrimitives() {
74+
SqlSession sqlSession = sqlSessionFactory.openSession();
75+
try {
76+
Map<String, Object> parameter = new HashMap<String, Object>();
77+
int[] array = new int[] {1, 3, 5};
78+
parameter.put("ids", array);
79+
80+
List<Map<String, Object>> answer =
81+
sqlSession.selectList("org.apache.ibatis.submitted.nested.simpleSelectWithPrimitives", parameter);
82+
83+
assertEquals(3, answer.size());
84+
} finally {
85+
sqlSession.close();
86+
}
87+
}
88+
7089
@Test
7190
@SuppressWarnings("unchecked")
7291
public void testNestedSelect() {

0 commit comments

Comments
 (0)