45 import java.nio.file.Paths; |
45 import java.nio.file.Paths; |
46 import java.nio.file.StandardCopyOption; |
46 import java.nio.file.StandardCopyOption; |
47 import java.util.*; |
47 import java.util.*; |
48 import java.util.function.Consumer; |
48 import java.util.function.Consumer; |
49 import java.util.function.Function; |
49 import java.util.function.Function; |
50 import java.util.function.Supplier; |
|
51 import java.util.regex.Pattern; |
50 import java.util.regex.Pattern; |
52 import java.util.stream.Collectors; |
51 import java.util.stream.Collectors; |
53 import java.util.stream.Stream; |
52 import java.util.stream.Stream; |
54 import java.util.zip.*; |
53 import java.util.zip.*; |
55 import java.util.jar.*; |
54 import java.util.jar.*; |
56 import java.util.jar.Pack200.*; |
55 import java.util.jar.Pack200.*; |
57 import java.util.jar.Manifest; |
56 import java.util.jar.Manifest; |
58 import java.text.MessageFormat; |
57 import java.text.MessageFormat; |
59 |
58 |
60 import jdk.internal.misc.JavaLangModuleAccess; |
|
61 import jdk.internal.misc.SharedSecrets; |
|
62 import jdk.internal.module.Checks; |
59 import jdk.internal.module.Checks; |
63 import jdk.internal.module.ModuleHashes; |
60 import jdk.internal.module.ModuleHashes; |
|
61 import jdk.internal.module.ModuleInfo; |
64 import jdk.internal.module.ModuleInfoExtender; |
62 import jdk.internal.module.ModuleInfoExtender; |
|
63 import jdk.internal.module.ModuleResolution; |
65 import jdk.internal.util.jar.JarIndex; |
64 import jdk.internal.util.jar.JarIndex; |
66 |
65 |
67 import static jdk.internal.util.jar.JarIndex.INDEX_NAME; |
66 import static jdk.internal.util.jar.JarIndex.INDEX_NAME; |
68 import static java.util.jar.JarFile.MANIFEST_NAME; |
67 import static java.util.jar.JarFile.MANIFEST_NAME; |
69 import static java.util.stream.Collectors.joining; |
68 import static java.util.stream.Collectors.joining; |
209 boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, nflag, pflag, dflag; |
208 boolean cflag, uflag, xflag, tflag, vflag, flag0, Mflag, iflag, nflag, pflag, dflag; |
210 |
209 |
211 /* To support additional GNU Style informational options */ |
210 /* To support additional GNU Style informational options */ |
212 enum Info { |
211 enum Info { |
213 HELP(GNUStyleOptions::printHelp), |
212 HELP(GNUStyleOptions::printHelp), |
|
213 HELP_EXTRA(GNUStyleOptions::printHelpExtra), |
214 COMPAT_HELP(GNUStyleOptions::printCompatHelp), |
214 COMPAT_HELP(GNUStyleOptions::printCompatHelp), |
215 USAGE_TRYHELP(GNUStyleOptions::printUsageTryHelp), |
215 USAGE_TRYHELP(GNUStyleOptions::printUsageTryHelp), |
216 VERSION(GNUStyleOptions::printVersion); |
216 VERSION(GNUStyleOptions::printVersion); |
217 |
217 |
218 private Consumer<PrintWriter> printFunction; |
218 private Consumer<PrintWriter> printFunction; |
223 |
223 |
224 |
224 |
225 /* Modular jar related options */ |
225 /* Modular jar related options */ |
226 Version moduleVersion; |
226 Version moduleVersion; |
227 Pattern modulesToHash; |
227 Pattern modulesToHash; |
|
228 ModuleResolution moduleResolution = ModuleResolution.empty(); |
228 ModuleFinder moduleFinder = ModuleFinder.of(); |
229 ModuleFinder moduleFinder = ModuleFinder.of(); |
229 |
230 |
230 private static final String MODULE_INFO = "module-info.class"; |
231 private static final String MODULE_INFO = "module-info.class"; |
231 |
232 |
232 static final String MANIFEST_DIR = "META-INF/"; |
233 static final String MANIFEST_DIR = "META-INF/"; |
1989 if (set.isEmpty()) { return ""; } |
1990 if (set.isEmpty()) { return ""; } |
1990 return set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT)) |
1991 return set.stream().map(e -> e.toString().toLowerCase(Locale.ROOT)) |
1991 .collect(joining(" ")); |
1992 .collect(joining(" ")); |
1992 } |
1993 } |
1993 |
1994 |
1994 private static final JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess(); |
|
1995 |
|
1996 private void printModuleDescriptor(InputStream entryInputStream) |
1995 private void printModuleDescriptor(InputStream entryInputStream) |
1997 throws IOException |
1996 throws IOException |
1998 { |
1997 { |
1999 ModuleDescriptor md = ModuleDescriptor.read(entryInputStream); |
1998 ModuleInfo.Attributes attrs = ModuleInfo.read(entryInputStream, null); |
|
1999 ModuleDescriptor md = attrs.descriptor(); |
|
2000 ModuleHashes hashes = attrs.recordedHashes(); |
|
2001 |
2000 StringBuilder sb = new StringBuilder(); |
2002 StringBuilder sb = new StringBuilder(); |
2001 sb.append("\n"); |
2003 sb.append("\n"); |
2002 if (md.isOpen()) |
2004 if (md.isOpen()) |
2003 sb.append("open "); |
2005 sb.append("open "); |
2004 sb.append(md.toNameAndVersion()); |
2006 sb.append(md.toNameAndVersion()); |
2041 |
2043 |
2042 md.osArch().ifPresent(v -> sb.append("\n operating-system-architecture " + v)); |
2044 md.osArch().ifPresent(v -> sb.append("\n operating-system-architecture " + v)); |
2043 |
2045 |
2044 md.osVersion().ifPresent(v -> sb.append("\n operating-system-version " + v)); |
2046 md.osVersion().ifPresent(v -> sb.append("\n operating-system-version " + v)); |
2045 |
2047 |
2046 JLMA.hashes(md).ifPresent(hashes -> |
2048 if (hashes != null) { |
2047 hashes.names().stream().sorted().forEach( |
2049 hashes.names().stream().sorted().forEach( |
2048 mod -> sb.append("\n hashes ").append(mod).append(" ") |
2050 mod -> sb.append("\n hashes ").append(mod).append(" ") |
2049 .append(hashes.algorithm()).append(" ") |
2051 .append(hashes.algorithm()).append(" ") |
2050 .append(hashes.hashFor(mod)))); |
2052 .append(toHex(hashes.hashFor(mod)))); |
|
2053 } |
2051 |
2054 |
2052 output(sb.toString()); |
2055 output(sb.toString()); |
|
2056 } |
|
2057 |
|
2058 private static String toHex(byte[] ba) { |
|
2059 StringBuilder sb = new StringBuilder(ba.length); |
|
2060 for (byte b: ba) { |
|
2061 sb.append(String.format("%02x", b & 0xff)); |
|
2062 } |
|
2063 return sb.toString(); |
2053 } |
2064 } |
2054 |
2065 |
2055 private static String toBinaryName(String classname) { |
2066 private static String toBinaryName(String classname) { |
2056 return (classname.replace('.', '/')) + ".class"; |
2067 return (classname.replace('.', '/')) + ".class"; |
2057 } |
2068 } |
2210 // should it issue warning or silent? |
2221 // should it issue warning or silent? |
2211 System.out.println("warning: no module is recorded in hash in " + mn); |
2222 System.out.println("warning: no module is recorded in hash in " + mn); |
2212 } |
2223 } |
2213 } |
2224 } |
2214 |
2225 |
|
2226 if (moduleResolution.value() != 0) { |
|
2227 extender.moduleResolution(moduleResolution); |
|
2228 } |
|
2229 |
2215 extender.write(baos); |
2230 extender.write(baos); |
2216 return baos.toByteArray(); |
2231 return baos.toByteArray(); |
2217 } |
2232 } |
2218 |
2233 |
2219 /** |
2234 /** |
2226 final Configuration configuration; |
2241 final Configuration configuration; |
2227 Hasher(ModuleDescriptor descriptor, String fname) throws IOException { |
2242 Hasher(ModuleDescriptor descriptor, String fname) throws IOException { |
2228 // Create a module finder that finds the modular JAR |
2243 // Create a module finder that finds the modular JAR |
2229 // being created/updated |
2244 // being created/updated |
2230 URI uri = Paths.get(fname).toUri(); |
2245 URI uri = Paths.get(fname).toUri(); |
2231 ModuleReference mref = new ModuleReference(descriptor, uri, |
2246 ModuleReference mref = new ModuleReference(descriptor, uri) { |
2232 new Supplier<>() { |
2247 @Override |
2233 @Override |
2248 public ModuleReader open() { |
2234 public ModuleReader get() { |
2249 throw new UnsupportedOperationException("should not reach here"); |
2235 throw new UnsupportedOperationException("should not reach here"); |
2250 } |
2236 } |
2251 }; |
2237 }); |
|
2238 |
2252 |
2239 // Compose a module finder with the module path and |
2253 // Compose a module finder with the module path and |
2240 // the modular JAR being created or updated |
2254 // the modular JAR being created or updated |
2241 this.finder = ModuleFinder.compose(moduleFinder, |
2255 this.finder = ModuleFinder.compose(moduleFinder, |
2242 new ModuleFinder() { |
2256 new ModuleFinder() { |