Replace the use of switchmaps with direct use of ordinal.
Bug:
Change-Id: Iee5cd16ba848617a7eaac3d55283621c54b36ff1
diff --git a/src/test/examples/switchmaps/Colors.java b/src/test/examples/switchmaps/Colors.java
new file mode 100644
index 0000000..b2c8dc1
--- /dev/null
+++ b/src/test/examples/switchmaps/Colors.java
@@ -0,0 +1,19 @@
+// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package switchmaps;
+
+public enum Colors {
+ RED("rar"), BLUE("blew"), GREEN("soylent"), GRAY("fifty");
+
+ private String aField;
+
+ Colors(String string) {
+ aField = string;
+ }
+
+ @Override
+ public String toString() {
+ return aField;
+ }
+}
diff --git a/src/test/examples/switchmaps/Days.java b/src/test/examples/switchmaps/Days.java
new file mode 100644
index 0000000..b484bb5
--- /dev/null
+++ b/src/test/examples/switchmaps/Days.java
@@ -0,0 +1,8 @@
+// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package switchmaps;
+
+public enum Days {
+ MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
+}
diff --git a/src/test/examples/switchmaps/Switches.java b/src/test/examples/switchmaps/Switches.java
new file mode 100644
index 0000000..bf16856
--- /dev/null
+++ b/src/test/examples/switchmaps/Switches.java
@@ -0,0 +1,66 @@
+// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package switchmaps;
+
+public class Switches {
+
+ public static void main(String... args) {
+ for (Days value : Days.values()) {
+ switchWithDefault(value);
+ switchFull(value);
+ }
+ for (Colors color : Colors.values()) {
+ switchOnColors(color);
+ }
+ }
+
+ private static void switchOnColors(Colors color) {
+ System.out.println(color.toString());
+ switch (color) {
+ case GRAY:
+ System.out.println("not really");
+ break;
+ case GREEN:
+ System.out.println("sooo green");
+ break;
+ default:
+ System.out.println("colorful");
+ }
+ }
+
+ private static void switchWithDefault(Days day) {
+ switch (day) {
+ case WEDNESDAY:
+ case FRIDAY:
+ System.out.println("3 or 5");
+ break;
+ case SUNDAY:
+ System.out.println("7");
+ break;
+ default:
+ System.out.println("other");
+ }
+ }
+
+ private static void switchFull(Days day) {
+ switch (day) {
+ case MONDAY:
+ case WEDNESDAY:
+ case THURSDAY:
+ System.out.println("1, 3 or 4");
+ case TUESDAY:
+ case FRIDAY:
+ System.out.println("2 or 5");
+ break;
+ case SUNDAY:
+ System.out.println("7");
+ break;
+ case SATURDAY:
+ System.out.println("6");
+ break;
+ default:
+ System.out.println("other");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
index 9e7588d..4a06085 100644
--- a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
@@ -100,6 +100,7 @@
{"minification.Minification", null},
{"enclosingmethod.Main", null},
{"interfaceinlining.Main", null},
+ {"switchmaps.Switches", null},
};
List<String[]> fullTestList = new ArrayList<>(tests.length * 2);
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index a93fb12..bcb9f7d 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -34,7 +34,7 @@
/**
* Write lines of text to a temporary file.
*/
- protected Path writeTextToTempFile(String... lines) throws IOException{
+ protected Path writeTextToTempFile(String... lines) throws IOException {
Path file = temp.newFile().toPath();
FileUtils.writeTextFile(file, lines);
return file;
@@ -86,7 +86,7 @@
/**
* Compile an application with R8 using the supplied proguard configuration.
*/
- protected AndroidApp compileWithR8(List<Class> classes, Path proguardConfig)
+ protected AndroidApp compileWithR8(List<Class> classes, Path proguardConfig)
throws CompilationException, ProguardRuleParserException, ExecutionException, IOException {
return compileWithR8(readClasses(classes), proguardConfig);
}
diff --git a/src/test/java/com/android/tools/r8/rewrite/switchmaps/RewriteSwitchMapsTest.java b/src/test/java/com/android/tools/r8/rewrite/switchmaps/RewriteSwitchMapsTest.java
new file mode 100644
index 0000000..4e8881a
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/rewrite/switchmaps/RewriteSwitchMapsTest.java
@@ -0,0 +1,36 @@
+// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.rewrite.switchmaps;
+
+import com.android.tools.r8.CompilationException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.shaking.ProguardRuleParserException;
+import com.android.tools.r8.utils.AndroidApp;
+import com.android.tools.r8.utils.DexInspector;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.ExecutionException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class RewriteSwitchMapsTest extends TestBase {
+
+ private static final String JAR_FILE = "switchmaps.jar";
+ private static final String SWITCHMAP_CLASS_NAME = "switchmaps.Switches$1";
+ private static final String PG_CONFIG =
+ "-keep class switchmaps.Switches { public static void main(...); } " +
+ "-dontobfuscate";
+
+ @Test
+ public void checkSwitchMapsRemoved()
+ throws IOException, ProguardRuleParserException, ExecutionException, CompilationException {
+ AndroidApp.Builder builder = AndroidApp.builder();
+ builder.addLibraryFiles(Paths.get(ToolHelper.getDefaultAndroidJar()));
+ builder.addProgramFiles(Paths.get(ToolHelper.EXAMPLES_BUILD_DIR).resolve(JAR_FILE));
+ AndroidApp result = compileWithR8(builder.build(), writeTextToTempFile(PG_CONFIG));
+ DexInspector inspector = new DexInspector(result);
+ Assert.assertFalse(inspector.clazz(SWITCHMAP_CLASS_NAME).isPresent());
+ }
+}