Skip to content

Commit

Permalink
produce classCastException
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Feb 25, 2024
1 parent 6b6e69a commit 240e27e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/poiji/util/ReflectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void putFieldMultiValueMapData(Field field, String columnName, Obj
field.setAccessible(true);
MultiValuedMap<String, Object> multiValuedMap = (MultiValuedMap<String, Object>) field.get(instance);
multiValuedMap.put(columnName, o);
} catch (ClassCastException | IllegalAccessException e) {
} catch (IllegalAccessException | IllegalAccessError e) {
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/poiji/util/ReflectUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.poiji.util;

import com.poiji.annotation.ExcelCellsJoinedByName;
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiInstantiationException;

import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.ReflectPermission;

import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -49,7 +52,18 @@ public void newInstanceOfEnum() {
ReflectUtil.newInstanceOf(PoijiExcelType.class);
}

@Test(expected = ClassCastException.class)
public void illegalCast() throws NoSuchFieldException, SecurityException {
PackageModel pm = new PackageModel();
Object o = "new_value";
Field field = PackageModel.class.getDeclaredField("field");

ReflectUtil.putFieldMultiValueMapData(field, "Artist", o, pm);
}

static class PackageModel {
@ExcelCellsJoinedByName(expression = "Artist")
private String field = "";
}

static class PackageModelWithPrivateConstructor {
Expand Down

0 comments on commit 240e27e

Please sign in to comment.