From 59a8aedb37ae35d4d2a4306c92efd463fffaaf69 Mon Sep 17 00:00:00 2001 From: YOUNG HO CHA Date: Wed, 4 Aug 2021 12:24:55 +0900 Subject: [PATCH] Use SecurityManager to read enum fields --- .../com/google/gson/internal/bind/TypeAdapters.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index e8052a708c..04b13ada81 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -24,6 +24,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.sql.Timestamp; import java.util.ArrayList; import java.util.BitSet; @@ -777,11 +779,16 @@ private static final class EnumTypeAdapter> extends TypeAdapte public EnumTypeAdapter(Class classOfT) { try { - for (Field field : classOfT.getDeclaredFields()) { + for (final Field field : classOfT.getDeclaredFields()) { if (!field.isEnumConstant()) { continue; } - field.setAccessible(true); + AccessController.doPrivileged(new PrivilegedAction() { + @Override public Void run() { + field.setAccessible(true); + return null; + } + }); @SuppressWarnings("unchecked") T constant = (T)(field.get(null)); String name = constant.name();