< prev index next >

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java

Print this page




 357         List<String> opts = new ArrayList<>();
 358         for (String opt: options)
 359             opts.add(opt);
 360 
 361         return begin(opts, fileObjects).isOK();
 362     }
 363 
 364     @SuppressWarnings("removal")
 365     private Result begin(List<String> options, Iterable<? extends JavaFileObject> fileObjects) {
 366         fileManager = context.get(JavaFileManager.class);
 367         if (fileManager == null) {
 368             JavacFileManager.preRegister(context);
 369             fileManager = context.get(JavaFileManager.class);
 370             if (fileManager instanceof BaseFileManager) {
 371                 ((BaseFileManager) fileManager).autoClose = true;
 372             }
 373         }
 374 
 375         // locale, doclet and maybe taglet, needs to be determined first
 376         try {
 377             doclet = preprocess(fileManager, options);
 378         } catch (ToolException te) {
 379             if (!te.result.isOK()) {
 380                 if (te.message != null) {
 381                     messager.printError(te.message);
 382                 }
 383                 Throwable t = te.getCause();
 384                 dumpStack(t == null ? te : t);
 385             }
 386             return te.result;
 387         } catch (OptionException oe) {
 388             if (oe.message != null) {
 389                 messager.printError(oe.message);
 390             }
 391             oe.m.run();
 392             Throwable t = oe.getCause();
 393             dumpStack(t == null ? oe : t);
 394             return oe.result;
 395         }


















 396 
 397         Result result = OK;
 398         try {
 399             result = parseAndExecute(options, fileObjects);
 400         } catch (com.sun.tools.javac.main.Option.InvalidValueException e) {
 401             messager.printError(e.getMessage());
 402             Throwable t = e.getCause();
 403             dumpStack(t == null ? e : t);
 404             return ERROR;
 405         } catch (OptionException toe) {
 406             if (toe.message != null)
 407                 messager.printError(toe.message);
 408 
 409             toe.m.run();
 410             Throwable t = toe.getCause();
 411             dumpStack(t == null ? toe : t);
 412             return toe.result;
 413         } catch (ToolException exc) {
 414             if (exc.message != null) {
 415                 messager.printError(exc.message);


 614                     }
 615                 } else {
 616                     if (args.size() - idx -1 < opt.getArgumentCount()) {
 617                         text = messager.getText("main.requires_argument", arg);
 618                         throw new OptionException(ERROR, this::usage, text);
 619                     }
 620                     opt.process(arg, args.subList(idx + 1, args.size()));
 621                     idx += opt.getArgumentCount();
 622                 }
 623                 return idx;
 624             }
 625         }
 626         // check if arg is accepted by the tool before emitting error
 627         if (!isToolOption) {
 628             text = messager.getText("main.invalid_flag", arg);
 629             throw new OptionException(ERROR, this::usage, text);
 630         }
 631         return idx;
 632     }
 633 
 634     private Doclet preprocess(JavaFileManager jfm,
 635             List<String> argv) throws ToolException, OptionException {
 636         // doclet specifying arguments
 637         String userDocletPath = null;
 638         String userDocletName = null;
 639 
 640         // taglet specifying arguments, since tagletpath is a doclet
 641         // functionality, assume they are repeated and inspect all.
 642         List<File> userTagletPath = new ArrayList<>();
 643         List<String> userTagletNames = new ArrayList<>();
 644 
 645         // Step 1: loop through the args, set locale early on, if found.
 646         for (int i = 0 ; i < argv.size() ; i++) {
 647             String arg = argv.get(i);
 648             if (arg.equals(ToolOption.DUMPONERROR.primaryName)) {
 649                 dumpOnError = true;
 650             } else if (arg.equals(ToolOption.LOCALE.primaryName)) {
 651                 checkOneArg(argv, i++);
 652                 String lname = argv.get(i);
 653                 locale = getLocale(lname);
 654             } else if (arg.equals(ToolOption.DOCLET.primaryName)) {


 671                             docletName, argv.get(i));
 672                     throw new ToolException(CMDERR, text);
 673                 }
 674                 userDocletName = argv.get(i);
 675             } else if (arg.equals(ToolOption.DOCLETPATH.primaryName)) {
 676                 checkOneArg(argv, i++);
 677                 if (userDocletPath == null) {
 678                     userDocletPath = argv.get(i);
 679                 } else {
 680                     userDocletPath += File.pathSeparator + argv.get(i);
 681                 }
 682             } else if ("-taglet".equals(arg)) {
 683                 userTagletNames.add(argv.get(i + 1));
 684             } else if ("-tagletpath".equals(arg)) {
 685                 for (String pathname : argv.get(i + 1).split(File.pathSeparator)) {
 686                     userTagletPath.add(new File(pathname));
 687                 }
 688             }
 689         }
 690 




 691 
 692         // Step 3: doclet name specified ? if so find a ClassLoader,
 693         // and load it.
 694         if(docletClass == null) {
 695             if (userDocletName != null) {
 696                 ClassLoader cl = classLoader;
 697                 if (cl == null) {
 698                     if (!fileManager.hasLocation(DOCLET_PATH)) {
 699                         List<File> paths = new ArrayList<>();
 700                         if (userDocletPath != null) {
 701                             for (String pathname : userDocletPath.split(File.pathSeparator)) {
 702                                 paths.add(new File(pathname));
 703                             }
 704                         }
 705                         try {
 706                             ((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
 707                         } catch (IOException ioe) {
 708                             if (apiMode) {
 709                                 throw new IllegalArgumentException("Could not set location for " +
 710                                         userDocletPath, ioe);
 711                             }
 712                             String text = messager.getText("main.doclet_could_not_set_location",
 713                                     userDocletPath);
 714                             throw new ToolException(CMDERR, text, ioe);
 715                         }
 716                     }
 717                     cl = fileManager.getClassLoader(DOCLET_PATH);
 718                     if (cl == null) {
 719                         // despite doclet specified on cmdline no classloader found!
 720                         if (apiMode) {
 721                             throw new IllegalArgumentException("Could not obtain classloader to load "
 722 
 723                                     + userDocletPath);
 724                         }
 725                         String text = messager.getText("main.doclet_no_classloader_found",
 726                                 userDocletName);
 727                         throw new ToolException(CMDERR, text);
 728                     }
 729                 }
 730                 docletClass = loadDocletClass(userDocletName, cl);
 731             } else if (docletName != null){
 732                 docletClass = loadDocletClass(docletName, getClass().getClassLoader());
 733             } else {
 734                 docletClass = StdDoclet;






 735             }
 736         }
 737 
 738         if (jdk.javadoc.doclet.Doclet.class.isAssignableFrom(docletClass)) {
 739             // no need to dispatch to old, safe to init now
 740             initMessager();
 741             messager.setLocale(locale);
 742             try {
 743                 Object o = docletClass.getConstructor().newInstance();
 744                 doclet = (Doclet) o;
 745             } catch (ReflectiveOperationException exc) {
 746                 if (apiMode) {
 747                     throw new ClientCodeException(exc);

 748                 }
 749                 String text = messager.getText("main.could_not_instantiate_class", docletClass.getName());
 750                 throw new ToolException(ERROR, text);
 751             }
 752         } else {
 753             String text = messager.getText("main.not_a_doclet", docletClass.getName());
 754             throw new ToolException(ERROR, text);
 755         }
 756         return doclet;







 757     }
 758 
 759     private Class<?> loadDocletClass(String docletName, ClassLoader classLoader) throws ToolException {
 760         try {
 761             return classLoader == null ? Class.forName(docletName) : classLoader.loadClass(docletName);
 762         } catch (ClassNotFoundException cnfe) {
 763             if (apiMode) {
 764                 throw new IllegalArgumentException("Cannot find doclet class " + docletName);
 765             }
 766             String text = messager.getText("main.doclet_class_not_found", docletName);
 767             throw new ToolException(CMDERR, text, cnfe);
 768         }
 769     }
 770 
 771     private void parseArgs(List<String> args, List<String> javaNames) throws ToolException,
 772             OptionException, com.sun.tools.javac.main.Option.InvalidValueException {
 773         for (int i = 0 ; i < args.size() ; i++) {
 774             String arg = args.get(i);
 775             ToolOption o = ToolOption.get(arg);
 776             if (o != null) {
 777                 // handle a doclet argument that may be needed however
 778                 // don't increment the index, and allow the tool to consume args
 779                 handleDocletOptions(i, args, true);
 780                 if (o.hasArg) {
 781                     if (arg.startsWith("--") && arg.contains("=")) {




 357         List<String> opts = new ArrayList<>();
 358         for (String opt: options)
 359             opts.add(opt);
 360 
 361         return begin(opts, fileObjects).isOK();
 362     }
 363 
 364     @SuppressWarnings("removal")
 365     private Result begin(List<String> options, Iterable<? extends JavaFileObject> fileObjects) {
 366         fileManager = context.get(JavaFileManager.class);
 367         if (fileManager == null) {
 368             JavacFileManager.preRegister(context);
 369             fileManager = context.get(JavaFileManager.class);
 370             if (fileManager instanceof BaseFileManager) {
 371                 ((BaseFileManager) fileManager).autoClose = true;
 372             }
 373         }
 374 
 375         // locale, doclet and maybe taglet, needs to be determined first
 376         try {
 377             docletClass = preprocess(fileManager, options);
 378         } catch (ToolException te) {
 379             if (!te.result.isOK()) {
 380                 if (te.message != null) {
 381                     messager.printError(te.message);
 382                 }
 383                 Throwable t = te.getCause();
 384                 dumpStack(t == null ? te : t);
 385             }
 386             return te.result;
 387         } catch (OptionException oe) {
 388             if (oe.message != null) {
 389                 messager.printError(oe.message);
 390             }
 391             oe.m.run();
 392             Throwable t = oe.getCause();
 393             dumpStack(t == null ? oe : t);
 394             return oe.result;
 395         }
 396         if (jdk.javadoc.doclet.Doclet.class.isAssignableFrom(docletClass)) {
 397             // no need to dispatch to old, safe to init now
 398             initMessager();
 399             messager.setLocale(locale);
 400             try {
 401                 Object o = docletClass.getConstructor().newInstance();
 402                 doclet = (Doclet) o;
 403             } catch (ReflectiveOperationException exc) {
 404                 if (apiMode) {
 405                     throw new ClientCodeException(exc);
 406                 }
 407                 error("main.could_not_instantiate_class", docletClass.getName());
 408                 return ERROR;
 409             }
 410         } else {
 411             error("main.not_a_doclet", docletClass.getName());
 412             return ERROR;
 413         }
 414 
 415         Result result = OK;
 416         try {
 417             result = parseAndExecute(options, fileObjects);
 418         } catch (com.sun.tools.javac.main.Option.InvalidValueException e) {
 419             messager.printError(e.getMessage());
 420             Throwable t = e.getCause();
 421             dumpStack(t == null ? e : t);
 422             return ERROR;
 423         } catch (OptionException toe) {
 424             if (toe.message != null)
 425                 messager.printError(toe.message);
 426 
 427             toe.m.run();
 428             Throwable t = toe.getCause();
 429             dumpStack(t == null ? toe : t);
 430             return toe.result;
 431         } catch (ToolException exc) {
 432             if (exc.message != null) {
 433                 messager.printError(exc.message);


 632                     }
 633                 } else {
 634                     if (args.size() - idx -1 < opt.getArgumentCount()) {
 635                         text = messager.getText("main.requires_argument", arg);
 636                         throw new OptionException(ERROR, this::usage, text);
 637                     }
 638                     opt.process(arg, args.subList(idx + 1, args.size()));
 639                     idx += opt.getArgumentCount();
 640                 }
 641                 return idx;
 642             }
 643         }
 644         // check if arg is accepted by the tool before emitting error
 645         if (!isToolOption) {
 646             text = messager.getText("main.invalid_flag", arg);
 647             throw new OptionException(ERROR, this::usage, text);
 648         }
 649         return idx;
 650     }
 651 
 652     private Class<?> preprocess(JavaFileManager jfm,
 653             List<String> argv) throws ToolException, OptionException {
 654         // doclet specifying arguments
 655         String userDocletPath = null;
 656         String userDocletName = null;
 657 
 658         // taglet specifying arguments, since tagletpath is a doclet
 659         // functionality, assume they are repeated and inspect all.
 660         List<File> userTagletPath = new ArrayList<>();
 661         List<String> userTagletNames = new ArrayList<>();
 662 
 663         // Step 1: loop through the args, set locale early on, if found.
 664         for (int i = 0 ; i < argv.size() ; i++) {
 665             String arg = argv.get(i);
 666             if (arg.equals(ToolOption.DUMPONERROR.primaryName)) {
 667                 dumpOnError = true;
 668             } else if (arg.equals(ToolOption.LOCALE.primaryName)) {
 669                 checkOneArg(argv, i++);
 670                 String lname = argv.get(i);
 671                 locale = getLocale(lname);
 672             } else if (arg.equals(ToolOption.DOCLET.primaryName)) {


 689                             docletName, argv.get(i));
 690                     throw new ToolException(CMDERR, text);
 691                 }
 692                 userDocletName = argv.get(i);
 693             } else if (arg.equals(ToolOption.DOCLETPATH.primaryName)) {
 694                 checkOneArg(argv, i++);
 695                 if (userDocletPath == null) {
 696                     userDocletPath = argv.get(i);
 697                 } else {
 698                     userDocletPath += File.pathSeparator + argv.get(i);
 699                 }
 700             } else if ("-taglet".equals(arg)) {
 701                 userTagletNames.add(argv.get(i + 1));
 702             } else if ("-tagletpath".equals(arg)) {
 703                 for (String pathname : argv.get(i + 1).split(File.pathSeparator)) {
 704                     userTagletPath.add(new File(pathname));
 705                 }
 706             }
 707         }
 708 
 709         // Step 2: a doclet is provided, nothing more to do.
 710         if (docletClass != null) {
 711             return docletClass;
 712         }
 713 
 714         // Step 3: doclet name specified ? if so find a ClassLoader,
 715         // and load it.
 716         if (userDocletName != null) {
 717             ClassLoader cl = classLoader;
 718             if (cl == null) {
 719                 if (!fileManager.hasLocation(DOCLET_PATH)) {
 720                     List<File> paths = new ArrayList<>();
 721                     if (userDocletPath != null) {
 722                         for (String pathname : userDocletPath.split(File.pathSeparator)) {
 723                             paths.add(new File(pathname));













 724                         }
 725                     }
 726                     try {
 727                         ((StandardJavaFileManager)fileManager).setLocation(DOCLET_PATH, paths);
 728                     } catch (IOException ioe) {
 729                         if (apiMode) {
 730                             throw new IllegalArgumentException("Could not set location for " +
 731                                     userDocletPath, ioe);

 732                         }
 733                         String text = messager.getText("main.doclet_could_not_set_location",
 734                                 userDocletPath);
 735                         throw new ToolException(CMDERR, text, ioe);
 736                     }
 737                 }
 738                 cl = fileManager.getClassLoader(DOCLET_PATH);
 739                 if (cl == null) {
 740                     // despite doclet specified on cmdline no classloader found!
 741                     if (apiMode) {
 742                         throw new IllegalArgumentException("Could not obtain classloader to load "
 743                                 + userDocletPath);
 744                     }
 745                     String text = messager.getText("main.doclet_no_classloader_found",
 746                             userDocletName);
 747                     throw new ToolException(CMDERR, text);
 748                 }
 749             }






 750             try {
 751                 return cl.loadClass(userDocletName);
 752             } catch (ClassNotFoundException cnfe) {

 753                 if (apiMode) {
 754                     throw new IllegalArgumentException("Cannot find doclet class " + userDocletName,
 755                             cnfe);
 756                 }
 757                 String text = messager.getText("main.doclet_class_not_found", userDocletName);
 758                 throw new ToolException(CMDERR, text, cnfe);
 759             }



 760         }
 761 
 762         // Step 4: we have a doclet, try loading it
 763         if (docletName != null) {
 764             return loadDocletClass(docletName);
 765         }
 766 
 767         // finally
 768         return StdDoclet;
 769     }
 770 
 771     private Class<?> loadDocletClass(String docletName) throws ToolException {
 772         try {
 773             return Class.forName(docletName, true, getClass().getClassLoader());
 774         } catch (ClassNotFoundException cnfe) {
 775             if (apiMode) {
 776                 throw new IllegalArgumentException("Cannot find doclet class " + docletName);
 777             }
 778             String text = messager.getText("main.doclet_class_not_found", docletName);
 779             throw new ToolException(CMDERR, text, cnfe);
 780         }
 781     }
 782 
 783     private void parseArgs(List<String> args, List<String> javaNames) throws ToolException,
 784             OptionException, com.sun.tools.javac.main.Option.InvalidValueException {
 785         for (int i = 0 ; i < args.size() ; i++) {
 786             String arg = args.get(i);
 787             ToolOption o = ToolOption.get(arg);
 788             if (o != null) {
 789                 // handle a doclet argument that may be needed however
 790                 // don't increment the index, and allow the tool to consume args
 791                 handleDocletOptions(i, args, true);
 792                 if (o.hasArg) {
 793                     if (arg.startsWith("--") && arg.contains("=")) {


< prev index next >