< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java

Print this page
rev 14279 : [mq]: 8140281-deprecation-optional.get


 246             if (zip != null)
 247                 zip.close();
 248         }
 249     }
 250 
 251     private Map<String, Path> modulesToPath(Set<ModuleDescriptor> modules) {
 252         ModuleFinder finder = options.moduleFinder;
 253 
 254         Map<String,Path> modPaths = new HashMap<>();
 255         for (ModuleDescriptor m : modules) {
 256             String name = m.name();
 257 
 258             Optional<ModuleReference> omref = finder.find(name);
 259             if (!omref.isPresent()) {
 260                 // this should not happen, module path bug?
 261                 fail(InternalError.class,
 262                      "Selected module %s not on module path",
 263                      name);
 264             }
 265 
 266             URI uri = omref.get().location().get();
 267             modPaths.put(name, Paths.get(uri));
 268 
 269         }
 270         return modPaths;
 271     }
 272 
 273     private boolean describe() throws IOException {
 274         ZipFile zip = null;
 275         try {
 276             try {
 277                 zip = new ZipFile(options.jmodFile.toFile());
 278             } catch (IOException x) {
 279                 throw new IOException("error opening jmod file", x);
 280             }
 281 
 282             try (InputStream in = Files.newInputStream(options.jmodFile)) {
 283                 boolean found = printModuleDescriptor(in);
 284                 if (!found)
 285                     throw new CommandException("err.module.descriptor.not.found");
 286                 return found;


 329                     List<ModuleDescriptor.Exports> exports = sortExports(md.exports());
 330                     if (!exports.isEmpty()) {
 331                         exports.forEach(ex -> sb.append("\n  exports ").append(ex));
 332                     }
 333 
 334                     l = md.conceals().stream().sorted().collect(toList());
 335                     if (!l.isEmpty()) {
 336                         l.forEach(p -> sb.append("\n  conceals ").append(p));
 337                     }
 338 
 339                     Map<String, ModuleDescriptor.Provides> provides = md.provides();
 340                     if (!provides.isEmpty()) {
 341                         provides.values().forEach(p ->
 342                                 sb.append("\n  provides ").append(p.service())
 343                                   .append(" with ")
 344                                   .append(toString(p.providers())));
 345                     }
 346 
 347                     Optional<String> mc = md.mainClass();
 348                     if (mc.isPresent())
 349                         sb.append("\n  main-class " + mc.get());
 350 
 351 
 352 
 353                     Optional<String> osname = md.osName();
 354                     if (osname.isPresent())
 355                         sb.append("\n  operating-system-name " + osname.get());
 356 
 357                     Optional<String> osarch = md.osArch();
 358                     if (osarch.isPresent())
 359                         sb.append("\n  operating-system-architecture " + osarch.get());
 360 
 361                     Optional<String> osversion = md.osVersion();
 362                     if (osversion.isPresent())
 363                         sb.append("\n  operating-system-version " + osversion.get());
 364 
 365                     try {
 366                         Method m = ModuleDescriptor.class.getDeclaredMethod("hashes");
 367                         m.setAccessible(true);
 368                         @SuppressWarnings("unchecked")
 369                         Optional<Hasher.DependencyHashes> optHashes =
 370                                 (Optional<Hasher.DependencyHashes>) m.invoke(md);
 371 
 372                         if (optHashes.isPresent()) {
 373                             Hasher.DependencyHashes hashes = optHashes.get();
 374                             hashes.names().stream().forEach(mod ->
 375                                     sb.append("\n  hashes ").append(mod).append(" ")
 376                                       .append(hashes.algorithm()).append(" ")
 377                                       .append(hashes.hashFor(mod)));
 378                         }
 379                     } catch (ReflectiveOperationException x) {
 380                         throw new InternalError(x);
 381                     }
 382                     out.println(sb.toString());
 383                     return true;
 384                 }
 385             }
 386         }
 387         return false;
 388     }
 389 
 390     static List<ModuleDescriptor.Exports> sortExports(Set<ModuleDescriptor.Exports> exports) {
 391         Map<String,ModuleDescriptor.Exports> map =
 392                 exports.stream()
 393                        .collect(toMap(ModuleDescriptor.Exports::source,


 563 
 564         /**
 565          * Examines the module dependences of the given module
 566          * and computes the hash of any module that matches the
 567          * pattern {@code dependenciesToHash}.
 568          */
 569         DependencyHashes hashDependences(String name, Set<Requires> moduleDependences)
 570             throws IOException
 571         {
 572             Set<ModuleDescriptor> descriptors = new HashSet<>();
 573             for (Requires md: moduleDependences) {
 574                 String dn = md.name();
 575                 if (dependenciesToHash.matcher(dn).find()) {
 576                     try {
 577                         Optional<ModuleReference> omref = moduleFinder.find(dn);
 578                         if (!omref.isPresent()) {
 579                             throw new RuntimeException("Hashing module " + name
 580                                 + " dependencies, unable to find module " + dn
 581                                 + " on module path");
 582                         }
 583                         descriptors.add(omref.get().descriptor());
 584                     } catch (FindException x) {
 585                         throw new IOException("error reading module path", x);
 586                     }
 587                 }
 588             }
 589 
 590             Map<String, Path> map = modulesToPath(descriptors);
 591             if (map.size() == 0) {
 592                 return null;
 593             } else {
 594                 // use SHA-256 for now, easy to make this configurable if needed
 595                 return Hasher.generate(map, "SHA-256");
 596             }
 597         }
 598 
 599         /**
 600          * Returns the set of all packages on the given class path.
 601          */
 602         Set<String> findPackages(List<Path> classpath) {
 603             Set<String> packages = new HashSet<>();




 246             if (zip != null)
 247                 zip.close();
 248         }
 249     }
 250 
 251     private Map<String, Path> modulesToPath(Set<ModuleDescriptor> modules) {
 252         ModuleFinder finder = options.moduleFinder;
 253 
 254         Map<String,Path> modPaths = new HashMap<>();
 255         for (ModuleDescriptor m : modules) {
 256             String name = m.name();
 257 
 258             Optional<ModuleReference> omref = finder.find(name);
 259             if (!omref.isPresent()) {
 260                 // this should not happen, module path bug?
 261                 fail(InternalError.class,
 262                      "Selected module %s not on module path",
 263                      name);
 264             }
 265 
 266             URI uri = omref.getWhenPresent().location().getWhenPresent();
 267             modPaths.put(name, Paths.get(uri));
 268 
 269         }
 270         return modPaths;
 271     }
 272 
 273     private boolean describe() throws IOException {
 274         ZipFile zip = null;
 275         try {
 276             try {
 277                 zip = new ZipFile(options.jmodFile.toFile());
 278             } catch (IOException x) {
 279                 throw new IOException("error opening jmod file", x);
 280             }
 281 
 282             try (InputStream in = Files.newInputStream(options.jmodFile)) {
 283                 boolean found = printModuleDescriptor(in);
 284                 if (!found)
 285                     throw new CommandException("err.module.descriptor.not.found");
 286                 return found;


 329                     List<ModuleDescriptor.Exports> exports = sortExports(md.exports());
 330                     if (!exports.isEmpty()) {
 331                         exports.forEach(ex -> sb.append("\n  exports ").append(ex));
 332                     }
 333 
 334                     l = md.conceals().stream().sorted().collect(toList());
 335                     if (!l.isEmpty()) {
 336                         l.forEach(p -> sb.append("\n  conceals ").append(p));
 337                     }
 338 
 339                     Map<String, ModuleDescriptor.Provides> provides = md.provides();
 340                     if (!provides.isEmpty()) {
 341                         provides.values().forEach(p ->
 342                                 sb.append("\n  provides ").append(p.service())
 343                                   .append(" with ")
 344                                   .append(toString(p.providers())));
 345                     }
 346 
 347                     Optional<String> mc = md.mainClass();
 348                     if (mc.isPresent())
 349                         sb.append("\n  main-class " + mc.getWhenPresent());
 350 
 351 
 352 
 353                     Optional<String> osname = md.osName();
 354                     if (osname.isPresent())
 355                         sb.append("\n  operating-system-name " + osname.getWhenPresent());
 356 
 357                     Optional<String> osarch = md.osArch();
 358                     if (osarch.isPresent())
 359                         sb.append("\n  operating-system-architecture " + osarch.getWhenPresent());
 360 
 361                     Optional<String> osversion = md.osVersion();
 362                     if (osversion.isPresent())
 363                         sb.append("\n  operating-system-version " + osversion.getWhenPresent());
 364 
 365                     try {
 366                         Method m = ModuleDescriptor.class.getDeclaredMethod("hashes");
 367                         m.setAccessible(true);
 368                         @SuppressWarnings("unchecked")
 369                         Optional<Hasher.DependencyHashes> optHashes =
 370                                 (Optional<Hasher.DependencyHashes>) m.invoke(md);
 371 
 372                         if (optHashes.isPresent()) {
 373                             Hasher.DependencyHashes hashes = optHashes.getWhenPresent();
 374                             hashes.names().stream().forEach(mod ->
 375                                     sb.append("\n  hashes ").append(mod).append(" ")
 376                                       .append(hashes.algorithm()).append(" ")
 377                                       .append(hashes.hashFor(mod)));
 378                         }
 379                     } catch (ReflectiveOperationException x) {
 380                         throw new InternalError(x);
 381                     }
 382                     out.println(sb.toString());
 383                     return true;
 384                 }
 385             }
 386         }
 387         return false;
 388     }
 389 
 390     static List<ModuleDescriptor.Exports> sortExports(Set<ModuleDescriptor.Exports> exports) {
 391         Map<String,ModuleDescriptor.Exports> map =
 392                 exports.stream()
 393                        .collect(toMap(ModuleDescriptor.Exports::source,


 563 
 564         /**
 565          * Examines the module dependences of the given module
 566          * and computes the hash of any module that matches the
 567          * pattern {@code dependenciesToHash}.
 568          */
 569         DependencyHashes hashDependences(String name, Set<Requires> moduleDependences)
 570             throws IOException
 571         {
 572             Set<ModuleDescriptor> descriptors = new HashSet<>();
 573             for (Requires md: moduleDependences) {
 574                 String dn = md.name();
 575                 if (dependenciesToHash.matcher(dn).find()) {
 576                     try {
 577                         Optional<ModuleReference> omref = moduleFinder.find(dn);
 578                         if (!omref.isPresent()) {
 579                             throw new RuntimeException("Hashing module " + name
 580                                 + " dependencies, unable to find module " + dn
 581                                 + " on module path");
 582                         }
 583                         descriptors.add(omref.getWhenPresent().descriptor());
 584                     } catch (FindException x) {
 585                         throw new IOException("error reading module path", x);
 586                     }
 587                 }
 588             }
 589 
 590             Map<String, Path> map = modulesToPath(descriptors);
 591             if (map.size() == 0) {
 592                 return null;
 593             } else {
 594                 // use SHA-256 for now, easy to make this configurable if needed
 595                 return Hasher.generate(map, "SHA-256");
 596             }
 597         }
 598 
 599         /**
 600          * Returns the set of all packages on the given class path.
 601          */
 602         Set<String> findPackages(List<Path> classpath) {
 603             Set<String> packages = new HashSet<>();


< prev index next >