Skip to content

Commit

Permalink
Improve exception for deserializing static final field
Browse files Browse the repository at this point in the history
Previously it would report "Unexpected IllegalAccessException occurred..."
due to the uncaught IllegalAccessException.
  • Loading branch information
Marcono1234 committed Oct 18, 2022
1 parent 55bbdca commit d72a9d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ private ReflectiveTypeAdapterFactory.BoundField createBoundField(
final Gson context, final Field field, final Method accessor, final String name,
final TypeToken<?> fieldType, boolean serialize, boolean deserialize,
final boolean blockInaccessible) {

final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());

int modifiers = field.getModifiers();
final boolean isStaticFinalField = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);

JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
TypeAdapter<?> mapped = null;
if (annotation != null) {
Expand Down Expand Up @@ -199,6 +204,11 @@ void readIntoField(JsonReader reader, Object target)
if (fieldValue != null || !isPrimitive) {
if (blockInaccessible) {
checkAccessible(target, field);
} else if (isStaticFinalField) {
// Reflection does not permit setting value of `static final` field, even after calling `setAccessible`
// Handle this here to avoid causing IllegalAccessException when calling `Field.set`
String description = ReflectionHelper.getAccessibleObjectDescription(field, jsonAdapterPresent);
throw new JsonIOException("Cannot set value of 'static final' " + description);
}
field.set(target, fieldValue);
}
Expand Down
16 changes: 16 additions & 0 deletions gson/src/test/java/com/google/gson/functional/ObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
Expand Down Expand Up @@ -524,6 +525,9 @@ public void testStaticFieldSerialization() {

String json = gson.toJson(new ClassWithStaticField());
assertEquals("{\"s\":\"initial\"}", json);

json = gson.toJson(new ClassWithStaticFinalField());
assertEquals("{\"s\":\"initial\"}", json);
}

/**
Expand All @@ -550,9 +554,21 @@ public void testStaticFieldDeserialization() {
} finally {
ClassWithStaticField.s = oldValue;
}

try {
gson.fromJson("{\"s\":\"custom\"}", ClassWithStaticFinalField.class);
fail();
} catch (JsonIOException e) {
assertEquals("Cannot set value of 'static final' field 'com.google.gson.functional.ObjectTest$ClassWithStaticFinalField#s'",
e.getMessage());
}
}

static class ClassWithStaticField {
static String s = "initial";
}

static class ClassWithStaticFinalField {
static final String s = "initial";
}
}

0 comments on commit d72a9d1

Please sign in to comment.