Skip to content

Commit

Permalink
Add test for unused class to shrinker-test (#2455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Aug 22, 2023
1 parent 392cc65 commit 393db09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions shrinker-test/common.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
-keepclassmembernames class com.example.ClassWithVersionAnnotations {
<fields>;
}

# Keep the name of the class to allow using reflection to check if this class still exists
# after shrinking
-keepnames class com.example.UnusedClass
17 changes: 17 additions & 0 deletions shrinker-test/src/main/java/com/example/UnusedClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

import com.google.gson.annotations.SerializedName;

/**
* Class with no-args constructor and field annotated with {@code @SerializedName},
* but which is not actually used anywhere in the code.
*
* <p>The default ProGuard rules should not keep this class.
*/
public class UnusedClass {
public UnusedClass() {
}

@SerializedName("i")
public int i;
}
16 changes: 16 additions & 0 deletions shrinker-test/src/test/java/com/google/gson/it/ShrinkingIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import com.example.UnusedClass;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
Expand Down Expand Up @@ -248,4 +250,18 @@ public void testNoSerializedName_NoDefaultConstructor() throws Exception {
}
});
}

@Test
public void testUnusedClassRemoved() throws Exception {
// For some reason this test only works for R8 but not for ProGuard; ProGuard keeps the unused class
assumeTrue(jarToTest.equals(R8_RESULT_PATH));

String className = UnusedClass.class.getName();
ClassNotFoundException e = assertThrows(ClassNotFoundException.class, () -> {
runTest(className, c -> {
fail("Class should have been removed during shrinking: " + c);
});
});
assertThat(e).hasMessageThat().contains(className);
}
}

0 comments on commit 393db09

Please sign in to comment.