< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java

Print this page




 363     private void list(BasicImageReader reader, String name, ImageLocation location) {
 364         print(reader, name);
 365     }
 366 
 367       void verify(BasicImageReader reader, String name, ImageLocation location) {
 368         if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
 369             try {
 370                 byte[] bytes = reader.getResource(location);
 371                 ClassReader cr = new ClassReader(bytes);
 372                 ClassNode cn = new ClassNode();
 373                 cr.accept(cn, 0);
 374             } catch (Exception ex) {
 375                 log.println("Error(s) in Class: " + name);
 376             }
 377         }
 378     }
 379 
 380     private void iterate(JImageAction jimageAction,
 381             ModuleAction moduleAction,
 382             ResourceAction resourceAction) throws IOException, BadArgs {
 383         if (options.jimages.isEmpty()) {
 384             throw TASK_HELPER.newBadArgs("err.no.jimage");
 385         }
 386 
 387         for (File file : options.jimages) {
 388             if (!file.exists() || !file.isFile()) {
 389                 throw TASK_HELPER.newBadArgs("err.not.a.jimage", file.getName());
 390             }
 391 
 392             try (BasicImageReader reader = BasicImageReader.open(file.toPath())) {
 393                 if (jimageAction != null) {
 394                     jimageAction.apply(file, reader);
 395                 }
 396 
 397                 if (resourceAction != null) {
 398                     String[] entryNames = reader.getEntryNames();
 399                     String oldModule = "";
 400 
 401                     for (String name : entryNames) {
 402                         boolean match = includePredicates.isEmpty();
 403 
 404                         for (Predicate<String> predicate : includePredicates) {
 405                             if (predicate.test(name)) {


 413                         }
 414 
 415                         if (!ImageResourcesTree.isTreeInfoResource(name)) {
 416                             if (moduleAction != null) {
 417                                 int offset = name.indexOf('/', 1);
 418 
 419                                 String newModule = offset != -1 ?
 420                                         name.substring(1, offset) :
 421                                         "<unknown>";
 422 
 423                                 if (!oldModule.equals(newModule)) {
 424                                     moduleAction.apply(reader, oldModule, newModule);
 425                                     oldModule = newModule;
 426                                 }
 427                             }
 428 
 429                             ImageLocation location = reader.findLocation(name);
 430                             resourceAction.apply(reader, name, location);
 431                         }
 432                     }



















 433                 }
 434             }
 435         }
 436     }
 437 
 438     private boolean run() throws Exception, BadArgs {
 439         switch (options.task) {
 440             case EXTRACT:
 441                 iterate(null, null, this::extract);
 442                 break;
 443             case INFO:
 444                 iterate(this::info, null, null);
 445                 break;
 446             case LIST:
 447                 iterate(this::listTitle, this::listModule, this::list);
 448                 break;
 449             case VERIFY:
 450                 iterate(this::listTitle, null, this::verify);
 451                 break;
 452             default:


 363     private void list(BasicImageReader reader, String name, ImageLocation location) {
 364         print(reader, name);
 365     }
 366 
 367       void verify(BasicImageReader reader, String name, ImageLocation location) {
 368         if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
 369             try {
 370                 byte[] bytes = reader.getResource(location);
 371                 ClassReader cr = new ClassReader(bytes);
 372                 ClassNode cn = new ClassNode();
 373                 cr.accept(cn, 0);
 374             } catch (Exception ex) {
 375                 log.println("Error(s) in Class: " + name);
 376             }
 377         }
 378     }
 379 
 380     private void iterate(JImageAction jimageAction,
 381             ModuleAction moduleAction,
 382             ResourceAction resourceAction) throws IOException, BadArgs {
 383         validateOptions();


 384 
 385         for (File file : options.jimages) {
 386             if (!file.exists() || !file.isFile()) {
 387                 throw TASK_HELPER.newBadArgs("err.not.a.jimage", file.getName());
 388             }
 389 
 390             try (BasicImageReader reader = BasicImageReader.open(file.toPath())) {
 391                 if (jimageAction != null) {
 392                     jimageAction.apply(file, reader);
 393                 }
 394 
 395                 if (resourceAction != null) {
 396                     String[] entryNames = reader.getEntryNames();
 397                     String oldModule = "";
 398 
 399                     for (String name : entryNames) {
 400                         boolean match = includePredicates.isEmpty();
 401 
 402                         for (Predicate<String> predicate : includePredicates) {
 403                             if (predicate.test(name)) {


 411                         }
 412 
 413                         if (!ImageResourcesTree.isTreeInfoResource(name)) {
 414                             if (moduleAction != null) {
 415                                 int offset = name.indexOf('/', 1);
 416 
 417                                 String newModule = offset != -1 ?
 418                                         name.substring(1, offset) :
 419                                         "<unknown>";
 420 
 421                                 if (!oldModule.equals(newModule)) {
 422                                     moduleAction.apply(reader, oldModule, newModule);
 423                                     oldModule = newModule;
 424                                 }
 425                             }
 426 
 427                             ImageLocation location = reader.findLocation(name);
 428                             resourceAction.apply(reader, name, location);
 429                         }
 430                     }
 431                 }
 432             }
 433         }
 434     }
 435 
 436     private void validateOptions() throws BadArgs {
 437         if (options.jimages.isEmpty()) {
 438             throw TASK_HELPER.newBadArgs("err.no.jimage");
 439         }
 440 
 441         if (options.task == Task.EXTRACT) {
 442             File extractTargetDir = new File(options.directory);
 443             if (extractTargetDir.exists()) {
 444                 if (extractTargetDir.isDirectory()) {
 445                     if (extractTargetDir.listFiles().length > 0) {
 446                         throw TASK_HELPER.newBadArgs("err.not.an.empty.dir", options.directory);
 447                     }
 448                 } else {
 449                     throw TASK_HELPER.newBadArgs("err.not.a.dir", options.directory);
 450                 }
 451             }
 452         }
 453     }
 454 
 455     private boolean run() throws Exception, BadArgs {
 456         switch (options.task) {
 457             case EXTRACT:
 458                 iterate(null, null, this::extract);
 459                 break;
 460             case INFO:
 461                 iterate(this::info, null, null);
 462                 break;
 463             case LIST:
 464                 iterate(this::listTitle, this::listModule, this::list);
 465                 break;
 466             case VERIFY:
 467                 iterate(this::listTitle, null, this::verify);
 468                 break;
 469             default:
< prev index next >