diff options
author | cushon <cushon@google.com> | 2017-09-15 06:11:42 +0200 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2017-09-15 11:29:31 +0200 |
commit | 6f3af28954ac65614b4dc8e2378829a5a653038f (patch) | |
tree | 7773db6a3f03d576e4f244b90c16f135a9e1c6d0 /third_party/ijar | |
parent | f863f9c1dd45903c6b5a75a7a8f37d8b1181243f (diff) |
Skip module-info.class files in ijar
PiperOrigin-RevId: 168789962
Diffstat (limited to 'third_party/ijar')
-rw-r--r-- | third_party/ijar/ijar.cc | 19 | ||||
-rw-r--r-- | third_party/ijar/test/BUILD | 26 | ||||
-rw-r--r-- | third_party/ijar/test/GenModuleInfo.java | 44 | ||||
-rw-r--r-- | third_party/ijar/test/IjarTests.java | 6 |
4 files changed, 91 insertions, 4 deletions
diff --git a/third_party/ijar/ijar.cc b/third_party/ijar/ijar.cc index ce057e57bf..df9796954a 100644 --- a/third_party/ijar/ijar.cc +++ b/third_party/ijar/ijar.cc @@ -63,11 +63,22 @@ class JarStripperProcessor : public ZipExtractorProcessor { 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 1450b36e53..455ab29608 100644 --- a/third_party/ijar/test/BUILD +++ b/third_party/ijar/test/BUILD @@ -142,6 +142,31 @@ genrule( 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 @@ java_test( ":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 0000000000..ddfcc9c6f2 --- /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 7abdbb7868..43abc41773 100644 --- a/third_party/ijar/test/IjarTests.java +++ b/third_party/ijar/test/IjarTests.java @@ -266,4 +266,10 @@ public class IjarTests { /*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"); + } } |