langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/Start.java
author jjg
Wed, 29 Oct 2014 18:01:22 -0700
changeset 27320 f0739350a62d
parent 25874 83c19f00452c
child 27379 5ae894733e09
permissions -rw-r--r--
8062504: javadoc Start does not close file managers that it opens Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
     2
 * Copyright (c) 1997, 2014, 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;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    35
import java.util.Map;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    36
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    37
import javax.tools.JavaFileManager;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    38
import javax.tools.JavaFileObject;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 11052
diff changeset
    39
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.javadoc.*;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    41
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.main.CommandLine;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    43
import com.sun.tools.javac.main.Option;
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    44
import com.sun.tools.javac.util.BaseFileManager;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    45
import com.sun.tools.javac.util.ClientCodeException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.util.ListBuffer;
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
    49
import com.sun.tools.javac.util.Log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.util.Options;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    51
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * Main program of Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * Previously named "Main".
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    58
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    59
 *  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
    60
 *  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
    61
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    62
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * @since 1.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 */
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
    67
public class Start extends ToolOption.Helper {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    68
    /** Context for this invocation. */
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    69
    private final Context context;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private final String defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    72
    private final ClassLoader docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private static final String javadocName = "javadoc";
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private static final String standardDocletClassName =
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        "com.sun.tools.doclets.standard.Standard";
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    79
    private final long defaultFilter = PUBLIC | PROTECTED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
    81
    private final Messager messager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private DocletInvoker docletInvoker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    85
    /**
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    86
     * 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
    87
     * propagated using ClientCodeException.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    88
     */
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    89
    private boolean apiMode;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
    90
27320
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
    91
    private JavaFileManager fileManager;
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
    92
    private boolean closeFileManagerOnExit;
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
    93
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    Start(String programName,
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
          PrintWriter errWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
          PrintWriter warnWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
          PrintWriter noticeWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
          String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
    99
        this(programName, errWriter, warnWriter, noticeWriter, defaultDocletClassName, null);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   100
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   101
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   102
    Start(String programName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   103
          PrintWriter errWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   104
          PrintWriter warnWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   105
          PrintWriter noticeWriter,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   106
          String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   107
          ClassLoader docletParentClassLoader) {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   108
        context = new Context();
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   109
        messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   111
        this.docletParentClassLoader = docletParentClassLoader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    Start(String programName, String defaultDocletClassName) {
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   115
        this(programName, defaultDocletClassName, null);
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, String defaultDocletClassName,
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   119
          ClassLoader docletParentClassLoader) {
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   120
        context = new Context();
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   121
        messager = new Messager(context, programName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        this.defaultDocletClassName = defaultDocletClassName;
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   123
        this.docletParentClassLoader = docletParentClassLoader;
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   124
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   125
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   126
    Start(String programName, ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   127
        this(programName, standardDocletClassName, docletParentClassLoader);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    Start(String programName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        this(programName, standardDocletClassName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
1365
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   134
    Start(ClassLoader docletParentClassLoader) {
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   135
        this(javadocName, docletParentClassLoader);
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   136
    }
ced0e3c4678f 6748546: javadoc API should be classloader-friendly
jjg
parents: 1363
diff changeset
   137
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    Start() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        this(javadocName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   142
    public Start(Context context) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   143
        context.getClass(); // null check
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   144
        this.context = context;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   145
        apiMode = true;
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   146
        defaultDocletClassName = standardDocletClassName;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   147
        docletParentClassLoader = null;
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
        Log log = context.get(Log.logKey);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   150
        if (log instanceof Messager)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   151
            messager = (Messager) log;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   152
        else {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   153
            PrintWriter out = context.get(Log.outKey);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   154
            messager = (out == null) ? new Messager(context, javadocName)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   155
                    : new Messager(context, javadocName, out, out, out);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   156
        }
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
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * Usage
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   162
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   163
    void usage() {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   164
        usage(true);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   165
    }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   166
18005
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   167
    void usage(boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   168
        usage("main.usage", "-help", null, exit);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   169
    }
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   170
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   171
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   172
    void Xusage() {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   173
        Xusage(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
18005
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   176
    void Xusage(boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   177
        usage("main.Xusage", "-X", "main.Xusage.foot", exit);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   178
    }
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   179
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   180
    private void usage(String main, String doclet, String foot, boolean exit) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   181
        // 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
   182
        // write a header, then help for each option, then a footer.
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   183
        messager.notice(main);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   184
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   185
        // let doclet print usage information (does nothing on error)
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   186
        if (docletInvoker != null) {
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   187
            // 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
   188
            // help info. Moreover, the output appears on stdout,
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   189
            // and <i>not</i> on any of the standard streams passed
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   190
            // to javadoc, and in particular, not to the noticeWriter
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   191
            // But, to fix this, we need to fix the Doclet API.
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   192
            docletInvoker.optionLength(doclet);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   193
        }
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   194
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   195
        if (foot != null)
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   196
            messager.notice(foot);
598e2e068668 8007687: javadoc -X does not include -Xdoclint
jjg
parents: 14545
diff changeset
   197
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   198
        if (exit) exit();
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   199
    }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   200
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   201
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * Exit
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    private void exit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        messager.exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     * Main program - external wrapper
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
1363
d7fc53d64560 6748601: javadoc API should allow varargs use
jjg
parents: 10
diff changeset
   212
    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
   213
        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
   214
        return ok ? 0 : 1;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   215
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   216
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   217
    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
   218
        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
   219
        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
   220
        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
   221
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   222
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   223
    private boolean begin(Class<?> docletClass, String[] options, Iterable<? extends JavaFileObject> fileObjects) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        boolean failed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        try {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   227
            failed = !parseAndExecute(docletClass, options, fileObjects);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   228
        } catch (Messager.ExitJavadoc exc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            // ignore, we just exit this way
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        } catch (OutOfMemoryError ee) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   231
            messager.error(Messager.NOPOS, "main.out.of.memory");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            failed = true;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   233
        } catch (ClientCodeException e) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   234
            // 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
   235
            throw e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        } catch (Error ee) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   237
            ee.printStackTrace(System.err);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   238
            messager.error(Messager.NOPOS, "main.fatal.error");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        } catch (Exception ee) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   241
            ee.printStackTrace(System.err);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   242
            messager.error(Messager.NOPOS, "main.fatal.exception");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        } finally {
27320
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   245
            if (fileManager != null && closeFileManagerOnExit) {
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   246
                try {
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   247
                    fileManager.close();
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   248
                } catch (IOException ignore) {
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   249
                }
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   250
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            messager.exitNotice();
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            messager.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        failed |= messager.nerrors() > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        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
   256
        return !failed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     * Main program - internal
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     */
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   262
    private boolean parseAndExecute(
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   263
            Class<?> docletClass,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   264
            String[] argv,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   265
            Iterable<? extends JavaFileObject> fileObjects) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        long tm = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19508
diff changeset
   268
        ListBuffer<String> javaNames = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        // Preprocess @file arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            argv = CommandLine.parse(argv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        } catch (FileNotFoundException e) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   274
            messager.error(Messager.NOPOS, "main.cant.read", e.getMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        } catch (IOException e) {
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 8633
diff changeset
   277
            e.printStackTrace(System.err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   281
27320
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   282
        fileManager = context.get(JavaFileManager.class);
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   283
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   284
        setDocletInvoker(docletClass, fileManager, argv);
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5520
diff changeset
   285
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   286
        compOpts = Options.instance(context);
19508
e2cd0ed6c9b0 8011043: Warn about use of 1.5 and earlier source and target values
darcy
parents: 18005
diff changeset
   287
        // 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
   288
        compOpts.put("-Xlint:-options", "-Xlint:-options");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        // Parse arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            String arg = argv[i];
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   293
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   294
            ToolOption o = ToolOption.get(arg);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   295
            if (o != null) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   296
                // hack: this restriction should be removed
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   297
                if (o == ToolOption.LOCALE && i > 0)
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   298
                    usageError("main.locale_first");
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   299
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   300
                if (o.hasArg) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   301
                    oneArg(argv, i++);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   302
                    o.process(this, argv[i]);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   303
                } else {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   304
                    setOption(arg);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   305
                    o.process(this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                }
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   307
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            } else if (arg.startsWith("-XD")) {
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   309
                // hidden javac options
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                String s = arg.substring("-XD".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                int eq = s.indexOf('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                String key = (eq < 0) ? s : s.substring(0, eq);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                String value = (eq < 0) ? s : s.substring(eq+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                compOpts.put(key, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            // call doclet for its options
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            // other arg starts with - is invalid
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   318
            else if (arg.startsWith("-")) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                int optionLength;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                optionLength = docletInvoker.optionLength(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                if (optionLength < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                    // error already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                    exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                } else if (optionLength == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                    // option not found
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                    usageError("main.invalid_flag", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                    // doclet added option
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                    if ((i + optionLength) > argv.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                        usageError("main.requires_argument", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                    }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19508
diff changeset
   332
                    ListBuffer<String> args = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    for (int j = 0; j < optionLength-1; ++j) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                        args.append(argv[++i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                    setOption(arg, args.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                javaNames.append(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        }
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   342
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   343
        if (fileManager == null) {
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   344
            JavacFileManager.preRegister(context);
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   345
            fileManager = context.get(JavaFileManager.class);
27320
f0739350a62d 8062504: javadoc Start does not close file managers that it opens
jjg
parents: 25874
diff changeset
   346
            closeFileManagerOnExit = true;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   347
        }
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   348
        if (fileManager instanceof BaseFileManager) {
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   349
            ((BaseFileManager) fileManager).handleOptions(fileManagerOpts);
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   350
        }
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   351
14442
6dc10c88c07a 8002168: Cleanup initialization of javadoc Messager
jjg
parents: 14441
diff changeset
   352
        compOpts.notifyListeners();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   354
        if (javaNames.isEmpty() && subPackages.isEmpty() && isEmpty(fileObjects)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            usageError("main.No_packages_or_classes_specified");
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        if (!docletInvoker.validOptions(options.toList())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            // error message already displayed
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            exit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        JavadocTool comp = JavadocTool.make0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        if (comp == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        if (showAccess == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            setFilter(defaultFilter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        LanguageVersion languageVersion = docletInvoker.languageVersion();
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        RootDocImpl root = comp.getRootDocImpl(
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   372
                docLocale,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   373
                encoding,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   374
                showAccess,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   375
                javaNames.toList(),
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   376
                options.toList(),
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   377
                fileObjects,
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   378
                breakiterator,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   379
                subPackages.toList(),
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   380
                excludedPackages.toList(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                docClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                // legacy?
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   383
                languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1,
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   384
                quiet);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
14441
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   386
        // release resources
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   387
        comp = null;
6c066a762e05 8002146: javadoc doesn't release resources in a timely manner
jjg
parents: 14260
diff changeset
   388
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        // pass off control to the doclet
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        boolean ok = root != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        if (ok) ok = docletInvoker.start(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        // We're done.
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        if (compOpts.get("-verbose") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            tm = System.currentTimeMillis() - tm;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            messager.notice("main.done_in", Long.toString(tm));
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   402
    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
   403
        return !iter.iterator().hasNext();
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   404
    }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   405
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   406
    /**
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   407
     * Init the doclet invoker.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   408
     * 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
   409
     * argv.
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   410
     * 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
   411
     * 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
   412
     * -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
   413
     * @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
   414
     * @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
   415
     * 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
   416
     * @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
   417
     */
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   418
    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
   419
        if (docletClass != null) {
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   420
            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
   421
            // 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
   422
            return;
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   423
        }
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   424
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        String docletClassName = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        String docletPath = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        // Parse doclet specifying arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
        for (int i = 0 ; i < argv.length ; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            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
   431
            if (arg.equals(ToolOption.DOCLET.opt)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                if (docletClassName != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                    usageError("main.more_than_one_doclet_specified_0_and_1",
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                               docletClassName, argv[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                docletClassName = argv[i];
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   438
            } else if (arg.equals(ToolOption.DOCLETPATH.opt)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                oneArg(argv, i++);
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                if (docletPath == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                    docletPath = argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                    docletPath += File.pathSeparator + argv[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        if (docletClassName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            docletClassName = defaultDocletClassName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        // 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
   453
        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
   454
                docletClassName, docletPath,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   455
                docletParentClassLoader,
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   456
                apiMode);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
     * Set one arg option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
     * Error and exit if one argument is not provided.
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    private void oneArg(String[] args, int index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        if ((index + 1) < args.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            setOption(args[index], args[index+1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            usageError("main.requires_argument", args[index]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   471
    @Override
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   472
    void usageError(String key, Object... args) {
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   473
        messager.error(Messager.NOPOS, key, args);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   474
        usage(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
     * indicate an option with no arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    private void setOption(String opt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        String[] option = { opt };
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
     * indicate an option with one argument was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    private void setOption(String opt, String argument) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        String[] option = { opt, argument };
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        options.append(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
     * indicate an option with the specified list of arguments was given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    private void setOption(String opt, List<String> arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
        String[] args = new String[arguments.length() + 1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        int k = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        args[k++] = opt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            args[k++] = i.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        }
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14442
diff changeset
   503
        options.append(args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
}