Do not translate File.separatorChar to / when reading from archives.

We already have the split between Paths and Strings when guessing
descriptors. Consistently use the String based on for archive
entry names and the Path based one for file names.

This will make sure that an archive provider will not accidentally
include a descriptor that cannot be read from the archive.

R=gavra@google.com, sgjesse@google.com

Bug: 80510937
Change-Id: Ia895820cb260aabaf091be830077d5d2e1b53191
diff --git a/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java b/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
index 37aa208..499ba47 100644
--- a/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
+++ b/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
@@ -54,17 +54,18 @@
       while (entries.hasMoreElements()) {
         ZipEntry entry = entries.nextElement();
         try (InputStream stream = zipFile.getInputStream(entry)) {
-          Path name = Paths.get(entry.getName());
-          Origin entryOrigin = new ArchiveEntryOrigin(entry.getName(), origin);
-          if (archive.matchesFile(name)) {
-            if (isDexFile(name)) {
+          String name = entry.getName();
+          Path path = Paths.get(name);
+          Origin entryOrigin = new ArchiveEntryOrigin(name, origin);
+          if (archive.matchesFile(path)) {
+            if (isDexFile(path)) {
               if (!ignoreDexInArchive) {
                 ProgramResource resource =
                     OneShotByteResource.create(
                         Kind.DEX, entryOrigin, ByteStreams.toByteArray(stream), null);
                 dexResources.add(resource);
               }
-            } else if (isClassFile(name)) {
+            } else if (isClassFile(path)) {
               String descriptor = DescriptorUtils.guessTypeDescriptor(name);
               ProgramResource resource =
                   OneShotByteResource.create(
diff --git a/src/main/java/com/android/tools/r8/utils/DescriptorUtils.java b/src/main/java/com/android/tools/r8/utils/DescriptorUtils.java
index fa045e6..a846ea7 100644
--- a/src/main/java/com/android/tools/r8/utils/DescriptorUtils.java
+++ b/src/main/java/com/android/tools/r8/utils/DescriptorUtils.java
@@ -341,22 +341,35 @@
     }
   }
 
-  // Guess class descriptor from location of the class file.
+  /**
+   * Guess class descriptor from location of the class file on the file system
+   *
+   * @param name Path of the file to convert to the corresponding descriptor
+   * @return java class descriptor
+   */
   public static String guessTypeDescriptor(Path name) {
-    return guessTypeDescriptor(name.toString());
+    String fileName = name.toString();
+    if (File.separatorChar != '/') {
+      fileName = fileName.replace(File.separatorChar, '/');
+    }
+    return guessTypeDescriptor(fileName);
   }
 
-  // Guess class descriptor from location of the class file.
+  /**
+   * Guess class descriptor from location of the class file. This method assumes that the
+   * name uses '/' as the separator. Therefore, this should not be the name of a file
+   * on a file system.
+   *
+   * @param name the location of the class file to convert to descriptor
+   * @return java class descriptor
+   */
   public static String guessTypeDescriptor(String name) {
     assert name != null;
     assert name.endsWith(CLASS_EXTENSION) :
         "Name " + name + " must have " + CLASS_EXTENSION + " suffix";
-    String fileName =
-        File.separatorChar == '/' ? name.toString() :
-            name.toString().replace(File.separatorChar, '/');
-    String descriptor = fileName.substring(0, fileName.length() - CLASS_EXTENSION.length());
+    String descriptor = name.substring(0, name.length() - CLASS_EXTENSION.length());
     if (descriptor.contains(".")) {
-      throw new CompilationError("Unexpected class file name: " + fileName);
+      throw new CompilationError("Unexpected class file name: " + name);
     }
     return 'L' + descriptor + ';';
   }
diff --git a/src/test/java/com/android/tools/r8/utils/DescriptorUtilsTest.java b/src/test/java/com/android/tools/r8/utils/DescriptorUtilsTest.java
index 58180e3..7ad9163 100644
--- a/src/test/java/com/android/tools/r8/utils/DescriptorUtilsTest.java
+++ b/src/test/java/com/android/tools/r8/utils/DescriptorUtilsTest.java
@@ -6,7 +6,10 @@
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import org.junit.Test;
 
 public class DescriptorUtilsTest {
@@ -67,4 +70,16 @@
     assertEquals("java.lang.Object", DescriptorUtils.descriptorToJavaType("Ljava/lang/Object;"));
     assertEquals("a.b.C", DescriptorUtils.descriptorToJavaType("La/b/C;"));
   }
+
+  @Test
+  public void guessClassDescriptor() {
+    String obj = "java/lang/Object.class";
+    assertEquals("Ljava/lang/Object;", DescriptorUtils.guessTypeDescriptor(obj));
+    String objBackslash = "java\\lang\\Object.class";
+    assertEquals("Ljava\\lang\\Object;", DescriptorUtils.guessTypeDescriptor(objBackslash));
+    String objFileSeparatorChar =
+        "java" + File.separatorChar + "lang" + File.separatorChar + "Object.class";
+    assertEquals("Ljava/lang/Object;",
+        DescriptorUtils.guessTypeDescriptor(Paths.get(objFileSeparatorChar)));
+  }
 }