langtools/src/share/classes/com/sun/tools/javadoc/Start.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 19508 e2cd0ed6c9b0
child 24897 655b72d7b96e
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
18005
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    28
import java.io.File;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    29
import java.io.FileNotFoundException;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    30
import java.io.IOException;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    31
import java.io.PrintWriter;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    32
import java.util.ArrayList;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    33
import java.util.Collection;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    34
import java.util.Collections;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    35
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    36
import javax.tools.JavaFileManager;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    37
import javax.tools.JavaFileObject;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    38
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.main.CommandLine;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    41
import com.sun.tools.javac.util.ClientCodeException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.util.ListBuffer;
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
    45
import com.sun.tools.javac.util.Log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * Main program of Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * Previously named "Main".
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    53
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    54
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    55
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    56
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    57
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * @since 1.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 */
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
    62
public class Start extends ToolOption.Helper {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    63
    /** Context for this invocation. */
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    64
    private final Context context;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private final String defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    67
    private final ClassLoader docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private static final String javadocName = "javadoc";
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private static final String standardDocletClassName =
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        "com.sun.tools.doclets.standard.Standard";
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private long defaultFilter = PUBLIC | PROTECTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    76
    private final Messager messager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    private DocletInvoker docletInvoker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    80
    /**
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    81
     * In API mode, exceptions thrown while calling the doclet are
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    82
     * propagated using ClientCodeException.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    83
     */
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    84
    private boolean apiMode;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    85
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    Start(String programName,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
          PrintWriter errWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
          PrintWriter warnWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
          PrintWriter noticeWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
          String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    91
        this(programName, errWriter, warnWriter, noticeWriter, defaultDocletClassName, null);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    92
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    93
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    94
    Start(String programName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    95
          PrintWriter errWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    96
          PrintWriter warnWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    97
          PrintWriter noticeWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    98
          String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    99
          ClassLoader docletParentClassLoader) {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   100
        context = new Context();
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   101
        messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   103
        this.docletParentClassLoader = docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    Start(String programName, String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   107
        this(programName, defaultDocletClassName, null);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   108
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   109
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   110
    Start(String programName, String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   111
          ClassLoader docletParentClassLoader) {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   112
        context = new Context();
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   113
        messager = new Messager(context, programName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   115
        this.docletParentClassLoader = docletParentClassLoader;
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   116
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   117
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   118
    Start(String programName, ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   119
        this(programName, standardDocletClassName, docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    Start(String programName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        this(programName, standardDocletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   126
    Start(ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   127
        this(javadocName, docletParentClassLoader);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   128
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   129
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    Start() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        this(javadocName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   134
    public Start(Context context) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   135
        context.getClass(); // null check
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   136
        this.context = context;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   137
        apiMode = true;
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   138
        defaultDocletClassName = standardDocletClassName;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   139
        docletParentClassLoader = null;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   140
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   141
        Log log = context.get(Log.logKey);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   142
        if (log instanceof Messager)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   143
            messager = (Messager) log;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   144
        else {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   145
            PrintWriter out = context.get(Log.outKey);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   146
            messager = (out == null) ? new Messager(context, javadocName)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   147
                    : new Messager(context, javadocName, out, out, out);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   148
        }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   149
    }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   150
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * Usage
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   154
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   155
    void usage() {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   156
        usage(true);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   157
    }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   158
18005
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   159
    void usage(boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   160
        usage("main.usage", "-help", null, exit);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   161
    }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   162
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   163
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   164
    void Xusage() {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   165
        Xusage(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
18005
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   168
    void Xusage(boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   169
        usage("main.Xusage", "-X", "main.Xusage.foot", exit);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   170
    }
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   171
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   172
    private void usage(String main, String doclet, String foot, boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   173
        // RFE: it would be better to replace the following with code to
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   174
        // write a header, then help for each option, then a footer.
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   175
        messager.notice(main);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   176
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   177
        // let doclet print usage information (does nothing on error)
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   178
        if (docletInvoker != null) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   179
            // RFE: this is a pretty bad way to get the doclet to show
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   180
            // help info. Moreover, the output appears on stdout,
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   181
            // and <i>not</i> on any of the standard streams passed
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   182
            // to javadoc, and in particular, not to the noticeWriter
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   183
            // But, to fix this, we need to fix the Doclet API.
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   184
            docletInvoker.optionLength(doclet);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   185
        }
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   186
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   187
        if (foot != null)
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   188
            messager.notice(foot);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   189
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   190
        if (exit) exit();
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   191
    }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   192
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   193
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * Exit
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    private void exit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        messager.exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * Main program - external wrapper
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
1363
d7fc53d64560 6748601: javadoc API should allow varargs use
jjg
parents: 10
diff changeset
   204
    int begin(String... argv) {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   205
        boolean ok = begin(null, argv, Collections.<JavaFileObject> emptySet());
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   206
        return ok ? 0 : 1;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   207
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   208
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   209
    public boolean begin(Class<?> docletClass, Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19508
diff changeset
   210
        Collection<String> opts = new ArrayList<>();
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   211
        for (String opt: options) opts.add(opt);
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   212
        return begin(docletClass, opts.toArray(new String[opts.size()]), fileObjects);
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   213
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   214
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   215
    private boolean begin(Class<?> docletClass, String[] options, Iterable<? extends JavaFileObject> fileObjects) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        boolean failed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        try {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   219
            failed = !parseAndExecute(docletClass, options, fileObjects);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   220
        } catch (Messager.ExitJavadoc exc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            // ignore, we just exit this way
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        } catch (OutOfMemoryError ee) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   223
            messager.error(Messager.NOPOS, "main.out.of.memory");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            failed = true;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   225
        } catch (ClientCodeException e) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   226
            // simply rethrow these exceptions, to be caught and handled by JavadocTaskImpl
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   227
            throw e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        } catch (Error ee) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   229
            ee.printStackTrace(System.err);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   230
            messager.error(Messager.NOPOS, "main.fatal.error");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        } catch (Exception ee) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   233
            ee.printStackTrace(System.err);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   234
            messager.error(Messager.NOPOS, "main.fatal.exception");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            messager.exitNotice();
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            messager.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        failed |= messager.nerrors() > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        failed |= rejectWarnings && messager.nwarnings() > 0;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   242
        return !failed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * Main program - internal
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   248
    private boolean parseAndExecute(
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   249
            Class<?> docletClass,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   250
            String[] argv,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   251
            Iterable<? extends JavaFileObject> fileObjects) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        long tm = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19508
diff changeset
   254
        ListBuffer<String> javaNames = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        // Preprocess @file arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            argv = CommandLine.parse(argv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        } catch (FileNotFoundException e) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   260
            messager.error(Messager.NOPOS, "main.cant.read", e.getMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        } catch (IOException e) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   263
            e.printStackTrace(System.err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   267
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   268
        JavaFileManager fileManager = context.get(JavaFileManager.class);
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   269
        setDocletInvoker(docletClass, fileManager, argv);
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   270
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   271
        compOpts = Options.instance(context);
19508
e2cd0ed6c9b0 8011043: Warn about use of 1.5 and earlier source and target values
darcy
parents: 18005
diff changeset
   272
        // Make sure no obsolete source/target messages are reported
e2cd0ed6c9b0 8011043: Warn about use of 1.5 and earlier source and target values
darcy
parents: 18005
diff changeset
   273
        compOpts.put("-Xlint:-options", "-Xlint:-options");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        // Parse arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            String arg = argv[i];
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   278
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   279
            ToolOption o = ToolOption.get(arg);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   280
            if (o != null) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   281
                // hack: this restriction should be removed
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   282
                if (o == ToolOption.LOCALE && i > 0)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   283
                    usageError("main.locale_first");
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   284
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   285
                if (o.hasArg) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   286
                    oneArg(argv, i++);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   287
                    o.process(this, argv[i]);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   288
                } else {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   289
                    setOption(arg);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   290
                    o.process(this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                }
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   292
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            } else if (arg.startsWith("-XD")) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   294
                // hidden javac options
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                String s = arg.substring("-XD".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                int eq = s.indexOf('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                String key = (eq < 0) ? s : s.substring(0, eq);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                String value = (eq < 0) ? s : s.substring(eq+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                compOpts.put(key, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            // call doclet for its options
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            // other arg starts with - is invalid
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   303
            else if (arg.startsWith("-")) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                int optionLength;
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                optionLength = docletInvoker.optionLength(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                if (optionLength < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                    // error already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                    exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                } else if (optionLength == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                    // option not found
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                    usageError("main.invalid_flag", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    // doclet added option
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    if ((i + optionLength) > argv.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                        usageError("main.requires_argument", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19508
diff changeset
   317
                    ListBuffer<String> args = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    for (int j = 0; j < optionLength-1; ++j) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                        args.append(argv[++i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    setOption(arg, args.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                javaNames.append(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        }
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   327
        compOpts.notifyListeners();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   329
        if (javaNames.isEmpty() && subPackages.isEmpty() && isEmpty(fileObjects)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            usageError("main.No_packages_or_classes_specified");
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        if (!docletInvoker.validOptions(options.toList())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            // error message already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        JavadocTool comp = JavadocTool.make0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        if (comp == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        if (showAccess == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            setFilter(defaultFilter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        LanguageVersion languageVersion = docletInvoker.languageVersion();
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        RootDocImpl root = comp.getRootDocImpl(
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   347
                docLocale,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   348
                encoding,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   349
                showAccess,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   350
                javaNames.toList(),
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   351
                options.toList(),
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   352
                fileObjects,
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   353
                breakiterator,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   354
                subPackages.toList(),
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   355
                excludedPackages.toList(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                docClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                // legacy?
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   358
                languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   359
                quiet);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
14441
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   361
        // release resources
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   362
        comp = null;
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   363
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        // pass off control to the doclet
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        boolean ok = root != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        if (ok) ok = docletInvoker.start(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        // We're done.
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        if (compOpts.get("-verbose") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            tm = System.currentTimeMillis() - tm;
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            messager.notice("main.done_in", Long.toString(tm));
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   377
    private <T> boolean isEmpty(Iterable<T> iter) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   378
        return !iter.iterator().hasNext();
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   379
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   380
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   381
    /**
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   382
     * Init the doclet invoker.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   383
     * The doclet class may be given explicitly, or via the -doclet option in
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   384
     * argv.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   385
     * If the doclet class is not given explicitly, it will be loaded from
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   386
     * the file manager's DOCLET_PATH location, if available, or via the
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   387
     * -doclet path option in argv.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   388
     * @param docletClass The doclet class. May be null.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   389
     * @param fileManager The file manager used to get the class loader to load
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   390
     * the doclet class if required. May be null.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   391
     * @param argv Args containing -doclet and -docletpath, in case they are required.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   392
     */
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   393
    private void setDocletInvoker(Class<?> docletClass, JavaFileManager fileManager, String[] argv) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   394
        if (docletClass != null) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   395
            docletInvoker = new DocletInvoker(messager, docletClass, apiMode);
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   396
            // TODO, check no -doclet, -docletpath
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   397
            return;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   398
        }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   399
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        String docletClassName = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        String docletPath = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        // Parse doclet specifying arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            String arg = argv[i];
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   406
            if (arg.equals(ToolOption.DOCLET.opt)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                if (docletClassName != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                    usageError("main.more_than_one_doclet_specified_0_and_1",
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                               docletClassName, argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                docletClassName = argv[i];
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   413
            } else if (arg.equals(ToolOption.DOCLETPATH.opt)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                if (docletPath == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    docletPath = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                    docletPath += File.pathSeparator + argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        if (docletClassName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            docletClassName = defaultDocletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        // attempt to find doclet
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   428
        docletInvoker = new DocletInvoker(messager, fileManager,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   429
                docletClassName, docletPath,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   430
                docletParentClassLoader,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   431
                apiMode);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
     * Set one arg option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
     * Error and exit if one argument is not provided.
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
    private void oneArg(String[] args, int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        if ((index + 1) < args.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            setOption(args[index], args[index+1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            usageError("main.requires_argument", args[index]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   446
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   447
    void usageError(String key, Object... args) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   448
        messager.error(Messager.NOPOS, key, args);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   449
        usage(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
     * indicate an option with no arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    private void setOption(String opt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        String[] option = { opt };
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
     * indicate an option with one argument was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    private void setOption(String opt, String argument) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        String[] option = { opt, argument };
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
     * indicate an option with the specified list of arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    private void setOption(String opt, List<String> arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        String[] args = new String[arguments.length() + 1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        int k = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        args[k++] = opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            args[k++] = i.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        }
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   478
        options.append(args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
}