Revert "Fix duplicate types supporting embedded rules in library providers"

This reverts commit 53df2f58605d1bc2702918f87e031d96f1ca0f3b.

Revert "Refactor DataResourceProvider visiting of a zip archive"

This reverts commit de8db57bbd5f86b597442884fbd9a4c9022f9252.

Bug: b/289087274
Change-Id: I9e206960df03cd0e7df3781637f010e07e9223c8
diff --git a/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java b/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
index 43fbaa1..d3ee009 100644
--- a/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
+++ b/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
@@ -38,8 +38,7 @@
  * resources in the descriptor set will then force the read of zip entry contents.
  */
 @KeepForApi
-public class ArchiveClassFileProvider
-    implements ClassFileResourceProvider, DataResourceProvider, Closeable {
+public class ArchiveClassFileProvider implements ClassFileResourceProvider, Closeable {
   private final Path archive;
   private final Origin origin;
   private final Predicate<String> include;
@@ -108,16 +107,6 @@
     lazyDescriptors = null;
   }
 
-  @Override
-  public DataResourceProvider getDataResourceProvider() {
-    return this;
-  }
-
-  @Override
-  public void accept(Visitor resourceBrowser) throws ResourceException {
-    ZipUtils.visitWithResourceBrowser(archive, origin, resourceBrowser);
-  }
-
   private void reopenZipFile() throws IOException {
     assert lazyZipFile == null;
     assert lazyDescriptors == null;
diff --git a/src/main/java/com/android/tools/r8/R8Command.java b/src/main/java/com/android/tools/r8/R8Command.java
index f0e029f..489f9e0 100644
--- a/src/main/java/com/android/tools/r8/R8Command.java
+++ b/src/main/java/com/android/tools/r8/R8Command.java
@@ -788,6 +788,7 @@
       // TODO(b/248408342): Remove this and parse annotations as part of R8 root-set & enqueuer.
       extractKeepAnnotationRules(parser);
       ProguardConfiguration configuration = configurationBuilder.build();
+      getAppBuilder().addFilteredLibraryArchives(configuration.getLibraryjars());
 
       assert getProgramConsumer() != null;
 
@@ -905,23 +906,18 @@
             .map(ClassFileResourceProvider::getDataResourceProvider)
             .filter(Objects::nonNull)
             .forEach(providers::add);
-        // Find resources in library providers from API and added through legacy -libraryjars
+        // Find resources in library providers. Both from API and added through legacy -libraryjars
         // in configuration files.
         getAppBuilder().getLibraryResourceProviders().stream()
             .map(ClassFileResourceProvider::getDataResourceProvider)
             .filter(Objects::nonNull)
             .forEach(providers::add);
-      }
-      // Find resources in library providers added through legacy -libraryjars
-      // in configuration files.
-      for (FilteredClassPath libraryjar :
-          parser.getConfigurationBuilder().build().getLibraryjars()) {
-        if (seen.add(libraryjar)) {
-          ClassFileResourceProvider provider =
-              getAppBuilder().createAndAddLibraryProvider(libraryjar);
-          if (readEmbeddedRulesFromClasspathAndLibrary) {
+        for (FilteredClassPath libraryjar :
+            parser.getConfigurationBuilder().build().getLibraryjars()) {
+          if (seen.add(libraryjar)) {
+            ArchiveResourceProvider provider = getAppBuilder().createAndAddProvider(libraryjar);
             if (provider != null) {
-              providers.add(provider.getDataResourceProvider());
+              providers.add(provider);
             }
           }
         }
diff --git a/src/main/java/com/android/tools/r8/utils/AndroidApp.java b/src/main/java/com/android/tools/r8/utils/AndroidApp.java
index 31f0ed8..6672f02 100644
--- a/src/main/java/com/android/tools/r8/utils/AndroidApp.java
+++ b/src/main/java/com/android/tools/r8/utils/AndroidApp.java
@@ -1086,26 +1086,6 @@
       return null;
     }
 
-    public FilteredArchiveClassFileProvider createAndAddLibraryProvider(FilteredClassPath archive) {
-      if (isArchive(archive.getPath())) {
-        try {
-          FilteredArchiveClassFileProvider provider = new FilteredArchiveClassFileProvider(archive);
-          archiveProvidersToClose.add(provider);
-          libraryResourceProviders.add(provider);
-          return provider;
-        } catch (IOException e) {
-          reporter.error(new ExceptionDiagnostic(e, new PathOrigin(archive.getPath())));
-          return null;
-        }
-      }
-      reporter.error(
-          new StringDiagnostic(
-              "Unexpected input type. Only archive types are supported, e.g., .jar, .zip, etc.",
-              archive.getOrigin(),
-              archive.getPosition()));
-      return null;
-    }
-
     /** Add filtered archives of program resources. */
     public Builder addFilteredProgramArchives(Collection<FilteredClassPath> filteredArchives) {
       for (FilteredClassPath archive : filteredArchives) {
@@ -1169,7 +1149,22 @@
     /** Add library file resources. */
     public Builder addFilteredLibraryArchives(Collection<FilteredClassPath> filteredArchives) {
       for (FilteredClassPath archive : filteredArchives) {
-        createAndAddLibraryProvider(archive);
+        if (isArchive(archive.getPath())) {
+          try {
+            FilteredArchiveClassFileProvider provider =
+                new FilteredArchiveClassFileProvider(archive);
+            archiveProvidersToClose.add(provider);
+            libraryResourceProviders.add(provider);
+          } catch (IOException e) {
+            reporter.error(new ExceptionDiagnostic(e, new PathOrigin(archive.getPath())));
+          }
+        } else {
+          reporter.error(
+              new StringDiagnostic(
+                  "Unexpected input type. Only archive types are supported, e.g., .jar, .zip, etc.",
+                  archive.getOrigin(),
+                  archive.getPosition()));
+        }
       }
       return this;
     }
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 b5b9f3e..b73119d 100644
--- a/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
+++ b/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
@@ -5,6 +5,8 @@
 
 import static com.android.tools.r8.utils.FileUtils.isArchive;
 
+import com.android.tools.r8.DataDirectoryResource;
+import com.android.tools.r8.DataEntryResource;
 import com.android.tools.r8.DataResourceProvider;
 import com.android.tools.r8.ProgramResource;
 import com.android.tools.r8.ProgramResource.Kind;
@@ -115,7 +117,27 @@
 
   @Override
   public void accept(Visitor resourceBrowser) throws ResourceException {
-    ZipUtils.visitWithResourceBrowser(archive.getPath(), origin, resourceBrowser);
+    try (ZipFile zipFile =
+        FileUtils.createZipFile(archive.getPath().toFile(), StandardCharsets.UTF_8)) {
+      final Enumeration<? extends ZipEntry> entries = zipFile.entries();
+      while (entries.hasMoreElements()) {
+        ZipEntry entry = entries.nextElement();
+        String name = entry.getName();
+        if (archive.matchesFile(name) && !isProgramResourceName(name)) {
+          if (entry.isDirectory()) {
+            resourceBrowser.visit(DataDirectoryResource.fromZip(zipFile, entry));
+          } else {
+            resourceBrowser.visit(DataEntryResource.fromZip(zipFile, entry));
+          }
+        }
+      }
+    } catch (ZipException e) {
+      throw new ResourceException(origin, new CompilationError(
+          "Zip error while reading '" + archive + "': " + e.getMessage(), e));
+    } catch (IOException e) {
+      throw new ResourceException(origin, new CompilationError(
+          "I/O exception while reading '" + archive + "': " + e.getMessage(), e));
+    }
   }
 
   private boolean isProgramResourceName(String name) {
diff --git a/src/main/java/com/android/tools/r8/utils/InternalArchiveClassFileProvider.java b/src/main/java/com/android/tools/r8/utils/InternalArchiveClassFileProvider.java
index db5a798..7685a95 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalArchiveClassFileProvider.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalArchiveClassFileProvider.java
@@ -7,6 +7,8 @@
 import static com.android.tools.r8.utils.FileUtils.isArchive;
 
 import com.android.tools.r8.ClassFileResourceProvider;
+import com.android.tools.r8.DataDirectoryResource;
+import com.android.tools.r8.DataEntryResource;
 import com.android.tools.r8.DataResourceProvider;
 import com.android.tools.r8.DiagnosticsHandler;
 import com.android.tools.r8.ProgramResource;
@@ -141,6 +143,22 @@
 
   @Override
   public void accept(Visitor resourceBrowser) throws ResourceException {
-    ZipUtils.visitWithResourceBrowser(path, origin, resourceBrowser);
+    try {
+      ZipUtils.iterWithZipFile(
+          path,
+          (zipFile, entry) -> {
+            if (!ZipUtils.isClassFile(entry.getName())) {
+              if (entry.isDirectory()) {
+                resourceBrowser.visit(DataDirectoryResource.fromZip(zipFile, entry));
+              } else {
+                resourceBrowser.visit(DataEntryResource.fromZip(zipFile, entry));
+              }
+            }
+          });
+    } catch (IOException e) {
+      throw new ResourceException(
+          origin,
+          new CompilationError("I/O exception while reading '" + path + "': " + e.getMessage(), e));
+    }
   }
 }
diff --git a/src/main/java/com/android/tools/r8/utils/ZipUtils.java b/src/main/java/com/android/tools/r8/utils/ZipUtils.java
index 253ba73..fb33e96 100644
--- a/src/main/java/com/android/tools/r8/utils/ZipUtils.java
+++ b/src/main/java/com/android/tools/r8/utils/ZipUtils.java
@@ -10,12 +10,10 @@
 import com.android.tools.r8.ByteDataView;
 import com.android.tools.r8.DataDirectoryResource;
 import com.android.tools.r8.DataEntryResource;
-import com.android.tools.r8.DataResourceProvider.Visitor;
 import com.android.tools.r8.ProgramResource;
 import com.android.tools.r8.ResourceException;
 import com.android.tools.r8.androidapi.AndroidApiDataAccess;
 import com.android.tools.r8.errors.CompilationError;
-import com.android.tools.r8.origin.Origin;
 import com.android.tools.r8.references.ClassReference;
 import com.google.common.io.ByteStreams;
 import com.google.common.io.Closer;
@@ -46,7 +44,6 @@
 import java.util.stream.StreamSupport;
 import java.util.zip.CRC32;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
@@ -395,29 +392,4 @@
     }
     return -1;
   }
-
-  public static void visitWithResourceBrowser(Path path, Origin origin, Visitor resourceBrowser)
-      throws ResourceException {
-    try {
-      ZipUtils.iterWithZipFile(
-          path,
-          (zipFile, entry) -> {
-            if (!ZipUtils.isClassFile(entry.getName())) {
-              if (entry.isDirectory()) {
-                resourceBrowser.visit(DataDirectoryResource.fromZip(zipFile, entry));
-              } else {
-                resourceBrowser.visit(DataEntryResource.fromZip(zipFile, entry));
-              }
-            }
-          });
-    } catch (ZipException e) {
-      throw new ResourceException(
-          origin,
-          new CompilationError("Zip error while reading '" + path + "': " + e.getMessage(), e));
-    } catch (IOException e) {
-      throw new ResourceException(
-          origin,
-          new CompilationError("I/O exception while reading '" + path + "': " + e.getMessage(), e));
-    }
-  }
 }
diff --git a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesFromClasspathOrLibraryTest.java b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesFromClasspathOrLibraryTest.java
index 7ddfd96..d8fb33f 100644
--- a/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesFromClasspathOrLibraryTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/LibraryProvidedProguardRulesFromClasspathOrLibraryTest.java
@@ -6,6 +6,7 @@
 
 import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
 import static com.android.tools.r8.DiagnosticsMatcher.diagnosticOrigin;
+import static com.android.tools.r8.DiagnosticsMatcher.diagnosticType;
 import static com.android.tools.r8.OriginMatcher.hasPart;
 import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
 import static com.android.tools.r8.utils.codeinspector.Matchers.notIf;
@@ -28,6 +29,7 @@
 import com.android.tools.r8.ResourceException;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.errors.DuplicateTypesDiagnostic;
 import com.android.tools.r8.origin.ArchiveEntryOrigin;
 import com.android.tools.r8.origin.Origin;
 import com.android.tools.r8.utils.AndroidApiLevel;
@@ -111,19 +113,23 @@
         buildLibrary(
             ImmutableList.of(
                 "-keep class * implements " + Interface.class.getTypeName() + " { *; }"));
-    testForR8(parameters.getBackend())
-        .addProgramClasses(A.class, B.class)
-        .addKeepRules("-libraryjars " + library.toAbsolutePath())
-        .addKeepRules("-libraryjars " + library.toAbsolutePath())
-        .setMinApi(parameters)
-        .allowStdoutMessages()
-        .apply(b -> ToolHelper.setReadEmbeddedRulesFromClasspathAndLibrary(b.getBuilder(), true))
-        .compile()
-        .inspect(
-            inspector -> {
-              assertThat(inspector.clazz(A.class), isPresent());
-              assertThat(inspector.clazz(B.class), not(isPresent()));
-            });
+    assertThrows(
+        CompilationFailedException.class,
+        () ->
+            testForR8(parameters.getBackend())
+                .addProgramClasses(A.class, B.class)
+                .addKeepRules("-libraryjars " + library.toAbsolutePath())
+                .addKeepRules("-libraryjars " + library.toAbsolutePath())
+                .setMinApi(parameters)
+                .allowStdoutMessages()
+                .apply(
+                    b ->
+                        ToolHelper.setReadEmbeddedRulesFromClasspathAndLibrary(
+                            b.getBuilder(), true))
+                .compileWithExpectedDiagnostics(
+                    diagnostics ->
+                        diagnostics.assertErrorsMatch(
+                            diagnosticType(DuplicateTypesDiagnostic.class))));
   }
 
   @Test