Skip module-info.class files in ijar

PiperOrigin-RevId: 168789962
diff --git a/third_party/ijar/ijar.cc b/third_party/ijar/ijar.cc
index ce057e5..df97969 100644
--- a/third_party/ijar/ijar.cc
+++ b/third_party/ijar/ijar.cc
@@ -63,11 +63,22 @@
 
 bool JarStripperProcessor::Accept(const char* filename, const u4 attr) {
   const size_t filename_len = strlen(filename);
-  if (filename_len >= CLASS_EXTENSION_LENGTH) {
-    return strcmp(filename + filename_len - CLASS_EXTENSION_LENGTH,
-                  CLASS_EXTENSION) == 0;
+  if (filename_len < CLASS_EXTENSION_LENGTH ||
+      strcmp(filename + filename_len - CLASS_EXTENSION_LENGTH,
+             CLASS_EXTENSION) != 0) {
+    return false;
   }
-  return false;
+  // skip module-info.class files, which don't need stripping
+  const char* slash = strrchr(filename, '/');
+  if (slash == NULL) {
+    slash = filename;
+  } else {
+    slash++;
+  }
+  if (strcmp(slash, "module-info.class") == 0) {
+    return false;
+  }
+  return true;
 }
 
 void JarStripperProcessor::Process(const char* filename, const u4 attr,
diff --git a/third_party/ijar/test/BUILD b/third_party/ijar/test/BUILD
index 1450b36..455ab29 100644
--- a/third_party/ijar/test/BUILD
+++ b/third_party/ijar/test/BUILD
@@ -142,6 +142,31 @@
     tools = ["//third_party/ijar"],
 )
 
+java_binary(
+    name = "GenModuleInfo",
+    testonly = 1,
+    srcs = ["GenModuleInfo.java"],
+    main_class = "GenModuleInfo",
+    deps = ["//third_party:guava"],
+)
+
+genrule(
+    name = "module_info",
+    testonly = 1,
+    outs = ["module_info.jar"],
+    cmd = "$(location :GenModuleInfo) $@",
+    tools = [":GenModuleInfo"],
+)
+
+genrule(
+    name = "module_info_interface",
+    testonly = 1,
+    srcs = [":module_info.jar"],
+    outs = ["module_info-interface.jar"],
+    cmd = "$(location //third_party/ijar) $< $@",
+    tools = ["//third_party/ijar"],
+)
+
 java_test(
     name = "IjarTests",
     size = "small",
@@ -157,6 +182,7 @@
         ":interface_ijar_testlib",
         ":liblocal_and_anonymous_lib.jar",
         ":local_and_anonymous-interface.jar",
+        ":module_info-interface.jar",
         "//third_party/java/jdk/langtools:javac_jar",
     ],
     jvm_flags = [
diff --git a/third_party/ijar/test/GenModuleInfo.java b/third_party/ijar/test/GenModuleInfo.java
new file mode 100644
index 0000000..ddfcc9c
--- /dev/null
+++ b/third_party/ijar/test/GenModuleInfo.java
@@ -0,0 +1,44 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// 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.
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import com.google.common.io.ByteStreams;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.jar.JarOutputStream;
+import java.util.zip.ZipEntry;
+
+/** A generator for a jar file containing module-info.class files, and one real class file. */
+public class GenModuleInfo {
+  public static void main(String[] args) throws IOException {
+    try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(Paths.get(args[0])))) {
+      addEntry(jos, "module-info.class");
+      jos.write("hello".getBytes(UTF_8));
+
+      addEntry(jos, "foo/module-info.class");
+      jos.write("goodbye".getBytes(UTF_8));
+
+      addEntry(jos, "java/lang/String.class");
+      ByteStreams.copy(String.class.getResourceAsStream("/java/lang/String.class"), jos);
+    }
+  }
+
+  private static void addEntry(JarOutputStream jos, String name) throws IOException {
+    ZipEntry ze = new ZipEntry(name);
+    ze.setTime(0);
+    jos.putNextEntry(ze);
+  }
+}
diff --git a/third_party/ijar/test/IjarTests.java b/third_party/ijar/test/IjarTests.java
index 7abdbb7..43abc41 100644
--- a/third_party/ijar/test/IjarTests.java
+++ b/third_party/ijar/test/IjarTests.java
@@ -266,4 +266,10 @@
             /*flags=*/ 0);
     return innerClasses;
   }
+
+  @Test
+  public void moduleInfo() throws Exception {
+    Map<String, byte[]> lib = readJar("third_party/ijar/test/module_info-interface.jar");
+    assertThat(lib.keySet()).containsExactly("java/lang/String.class");
+  }
 }