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

fixed pojoutils string class issue #14102

Open
wants to merge 6 commits into
base: 3.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,11 @@ private static Object realize1(
} else if (field != null) {
value = realize1(value, field.getType(), field.getGenericType(), mapGeneric, history);
try {
field.set(dest, value);
if (value.getClass() == String.class && dest.getClass() == String.class) {
return value;
} else {
field.set(dest, value);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Failed to set field " + name + " of pojo "
Expand Down Expand Up @@ -774,7 +778,8 @@ private static Field getField(Class<?> cls, String fieldName) {
for (Class<?> acls = cls; acls != null; acls = acls.getSuperclass()) {
try {
result = acls.getDeclaredField(fieldName);
if (!Modifier.isPublic(result.getModifiers())) {
// the field is not public and it not jdk class, we will setAccessible to true
if (!Modifier.isPublic(result.getModifiers()) && acls.getClassLoader() != null) {
result.setAccessible(true);
}
} catch (NoSuchFieldException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,20 @@ public int hashCode() {
}
}

@Test
void testPojoWithPrimitivesStringClass() {
String expect = "abc123";
JSONObject jsonObject = new JSONObject();
jsonObject.put("value", expect);
jsonObject.put("class", "java.lang.String");
Object[] objects = new Object[] {jsonObject};
Class<?>[] types = new Class<?>[] {String.class};
Type[] gtType = new Type[] {String.class};
Object[] realize = PojoUtils.realize(objects, types, gtType);
assertEquals(1, realize.length);
assertEquals(expect, realize[0]);
}

public enum Day {
SUNDAY,
MONDAY,
Expand Down