Skip to content

Commit 240e27e

Browse files
committed
produce classCastException
1 parent 6b6e69a commit 240e27e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/com/poiji/util/ReflectUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void putFieldMultiValueMapData(Field field, String columnName, Obj
7272
field.setAccessible(true);
7373
MultiValuedMap<String, Object> multiValuedMap = (MultiValuedMap<String, Object>) field.get(instance);
7474
multiValuedMap.put(columnName, o);
75-
} catch (ClassCastException | IllegalAccessException e) {
75+
} catch (IllegalAccessException | IllegalAccessError e) {
7676
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
7777
}
7878
}

src/test/java/com/poiji/util/ReflectUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.poiji.util;
22

3+
import com.poiji.annotation.ExcelCellsJoinedByName;
34
import com.poiji.exception.PoijiExcelType;
45
import com.poiji.exception.PoijiInstantiationException;
6+
57
import org.junit.BeforeClass;
68
import org.junit.Test;
79

10+
import java.lang.reflect.Field;
811
import java.lang.reflect.ReflectPermission;
912

1013
import static org.junit.Assert.assertNotNull;
@@ -49,7 +52,18 @@ public void newInstanceOfEnum() {
4952
ReflectUtil.newInstanceOf(PoijiExcelType.class);
5053
}
5154

55+
@Test(expected = ClassCastException.class)
56+
public void illegalCast() throws NoSuchFieldException, SecurityException {
57+
PackageModel pm = new PackageModel();
58+
Object o = "new_value";
59+
Field field = PackageModel.class.getDeclaredField("field");
60+
61+
ReflectUtil.putFieldMultiValueMapData(field, "Artist", o, pm);
62+
}
63+
5264
static class PackageModel {
65+
@ExcelCellsJoinedByName(expression = "Artist")
66+
private String field = "";
5367
}
5468

5569
static class PackageModelWithPrivateConstructor {

0 commit comments

Comments
 (0)