diff --git a/gson/pom.xml b/gson/pom.xml index 83f52b3a29..14089475a0 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -16,6 +16,12 @@ junit test + + com.github.wvengen + proguard-maven-plugin + 2.3.1 + test + @@ -69,6 +75,87 @@ + + com.coderplus.maven.plugins + copy-rename-maven-plugin + 1.0 + + + pre-obfuscate-class + process-test-classes + + rename + + + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest.class + ${project.build.directory}/test-classes-obfuscated-injar/com/google/gson/functional/EnumWithObfuscatedTest.class + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + ${project.build.directory}/test-classes-obfuscated-injar/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + + + + + + + + com.github.wvengen + proguard-maven-plugin + + + process-test-classes + proguard + + + + 6.2.2 + true + test-classes-obfuscated-injar + test-classes-obfuscated-outjar + **/*.class + ${basedir}/src/test/resources/testcases-proguard.conf + + ${project.build.directory}/classes + ${java.home}/jmods/java.base.jmod + + + + + net.sf.proguard + proguard-base + 6.2.2 + runtime + + + + + maven-resources-plugin + 2.7 + + + post-obfuscate-class + process-test-classes + + copy-resources + + + ${project.build.directory}/test-classes/com/google/gson/functional + + + ${project.build.directory}/test-classes-obfuscated-outjar/com/google/gson/functional + + EnumWithObfuscatedTest.class + EnumWithObfuscatedTest$Gender.class + + + + + + + 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 354ce5a1fb..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 @@ -17,12 +17,15 @@ package com.google.gson.internal.bind; import java.io.IOException; +import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.BigInteger; import java.net.InetAddress; 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; @@ -776,9 +779,20 @@ private static final class EnumTypeAdapter> extends TypeAdapte public EnumTypeAdapter(Class classOfT) { try { - for (T constant : classOfT.getEnumConstants()) { + for (final Field field : classOfT.getDeclaredFields()) { + if (!field.isEnumConstant()) { + continue; + } + 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(); - SerializedName annotation = classOfT.getField(name).getAnnotation(SerializedName.class); + SerializedName annotation = field.getAnnotation(SerializedName.class); if (annotation != null) { name = annotation.value(); for (String alternate : annotation.alternate()) { @@ -788,7 +802,7 @@ public EnumTypeAdapter(Class classOfT) { nameToConstant.put(name, constant); constantToName.put(constant, name); } - } catch (NoSuchFieldException e) { + } catch (IllegalAccessException e) { throw new AssertionError(e); } } diff --git a/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java new file mode 100644 index 0000000000..080b81fa77 --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.functional; + +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; + +import junit.framework.TestCase; + +/** + * Functional tests for enums with Proguard. + * + * @author Young Cha + */ +public class EnumWithObfuscatedTest extends TestCase { + private Gson gson; + + @Override + protected void setUp() throws Exception { + super.setUp(); + gson = new Gson(); + } + + public enum Gender { + @SerializedName("MAIL") + MALE, + + @SerializedName("FEMAIL") + FEMALE + } + + public void testEnumClassWithObfuscated() { + for (Gender enumConstant: Gender.class.getEnumConstants()) { + try { + Gender.class.getField(enumConstant.name()); + fail("Enum is not obfuscated"); + } catch (NoSuchFieldException ignore) { + } + } + + assertEquals(Gender.MALE, gson.fromJson("\"MAIL\"", Gender.class)); + assertEquals("\"MAIL\"", gson.toJson(Gender.MALE, Gender.class)); + } +} diff --git a/gson/src/test/resources/testcases-proguard.conf b/gson/src/test/resources/testcases-proguard.conf new file mode 100644 index 0000000000..d42da0abef --- /dev/null +++ b/gson/src/test/resources/testcases-proguard.conf @@ -0,0 +1,20 @@ +# Options from Android Gradle Plugins +# https://android.googlesource.com/platform/tools/base/+/refs/heads/studio-master-dev/build-system/gradle-core/src/main/resources/com/android/build/gradle +-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* +-optimizationpasses 5 +-allowaccessmodification +-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keep enum com.google.gson.functional.EnumWithObfuscatedTest$Gender +-keep class com.google.gson.functional.EnumWithObfuscatedTest { + public void test*(); + protected void setUp(); +} + +-dontwarn com.google.gson.functional.EnumWithObfuscatedTest +-dontwarn junit.framework.TestCase +