src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocTool.java
author jjg
Mon, 08 Oct 2018 11:14:59 -0700
changeset 52043 50dc6dd40e6a
parent 49197 cc2673fa8c20
child 53881 db24a4cb8139
permissions -rw-r--r--
8211407: Bad links to non-existent entries on serialized-form page Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
52043
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
     2
 * Copyright (c) 2001, 2018, 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: 2212
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: 2212
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: 2212
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
    26
package jdk.javadoc.internal.tool;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
    27
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    29
import java.io.File;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
    30
import java.util.ArrayList;
23116
4c134ccd6b8d 8029145: javadoc fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 22163
diff changeset
    31
import java.util.HashSet;
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
    32
import java.util.LinkedHashSet;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
    33
import java.util.List;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    34
import java.util.Map;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    35
import java.util.Set;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    36
52043
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
    37
import javax.lang.model.element.Element;
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
    38
import javax.lang.model.element.ElementKind;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    39
import javax.tools.JavaFileObject;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    40
import javax.tools.StandardJavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    42
import com.sun.tools.javac.code.ClassFinder;
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 47216
diff changeset
    43
import com.sun.tools.javac.code.DeferredCompletionFailureHandler;
30066
d74c06a92bd8 8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents: 25874
diff changeset
    44
import com.sun.tools.javac.code.Symbol.Completer;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    45
import com.sun.tools.javac.code.Symbol.CompletionFailure;
52043
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
    46
import com.sun.tools.javac.code.Symbol.PackageSymbol;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    47
import com.sun.tools.javac.comp.Enter;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    48
import com.sun.tools.javac.tree.JCTree;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    51
import com.sun.tools.javac.util.Abort;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    52
import com.sun.tools.javac.util.Context;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    53
import com.sun.tools.javac.util.ListBuffer;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    54
import com.sun.tools.javac.util.Position;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
    55
import jdk.javadoc.doclet.DocletEnvironment;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
    57
import static jdk.javadoc.internal.tool.Main.Result.*;
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
    58
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *  This class could be the main entry point for Javadoc when Javadoc is used as a
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  component in a larger software system. It provides operations to
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  construct a new javadoc processor, and to run it on a set of source
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  files.
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    64
 *
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    65
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    66
 *  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: 14259
diff changeset
    67
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    68
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    69
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *  @author Neal Gafter
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
    73
    ToolEnvironment toolEnv;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    final Messager messager;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    76
    final ClassFinder javadocFinder;
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 47216
diff changeset
    77
    final DeferredCompletionFailureHandler dcfh;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    78
    final Enter javadocEnter;
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
    79
    final Set<JavaFileObject> uniquefiles;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Construct a new JavaCompiler processor, using appropriately
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * extended phases of the underlying compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    protected JavadocTool(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        super(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        messager = Messager.instance0(context);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    88
        javadocFinder = JavadocClassFinder.instance(context);
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 47216
diff changeset
    89
        dcfh = DeferredCompletionFailureHandler.instance(context);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
    90
        javadocEnter = JavadocEnter.instance(context);
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
    91
        uniquefiles = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 35426
diff changeset
    97
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    protected boolean keepComments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     *  Construct a new javadoc tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public static JavadocTool make0(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        Messager messager = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        try {
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
   108
            // force the use of Javadoc's class finder
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 23973
diff changeset
   109
            JavadocClassFinder.preRegister(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            // force the use of Javadoc's own enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            JavadocEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            // force the use of Javadoc's own member enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            JavadocMemberEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            // force the use of Javadoc's own todo phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            JavadocTodo.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            // force the use of Messager as a Log
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            messager = Messager.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            return new JavadocTool(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            messager.error(Position.NOPOS, ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   130
    public DocletEnvironment getEnvironment(Map<ToolOption,
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   131
            Object> jdtoolOpts,
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   132
            List<String> javaNames,
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   133
            Iterable<? extends JavaFileObject> fileObjects) throws ToolException {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
   134
        toolEnv = ToolEnvironment.instance(context);
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   135
        toolEnv.initialize(jdtoolOpts);
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   136
        ElementsTable etable = new ElementsTable(context, jdtoolOpts);
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   137
        javadocFinder.sourceCompleter = etable.xclasses
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   138
                ? Completer.NULL_COMPLETER
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   139
                : sourceCompleter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   141
        if (etable.xclasses) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   142
            // If -Xclasses is set, the args should be a list of class names
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   143
            for (String arg: javaNames) {
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   144
                if (!isValidPackageName(arg)) { // checks
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   145
                    String text = messager.getText("main.illegal_class_name", arg);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   146
                    throw new ToolException(CMDERR, text);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   147
                }
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   148
            }
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   149
            if (messager.hasErrors()) {
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   150
                return null;
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   151
            }
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   152
            etable.setClassArgList(javaNames);
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   153
            // prepare, force the data structures to be analyzed
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   154
            etable.analyze();
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   155
            return new DocEnvImpl(toolEnv, etable);
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   156
        }
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   157
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   158
        ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        try {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
   161
            StandardJavaFileManager fm = toolEnv.fileManager instanceof StandardJavaFileManager
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   162
                    ? (StandardJavaFileManager) toolEnv.fileManager
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   163
                    : null;
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   164
            Set<String> packageNames = new LinkedHashSet<>();
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   165
            // Normally, the args should be a series of package names or file names.
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   166
            // Parse the files and collect the package names.
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   167
            for (String arg: javaNames) {
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   168
                if (fm != null && arg.endsWith(".java") && new File(arg).exists()) {
44288
c33a416cfcf8 8176539: javadoc ignores module-info files on the command line
ksrini
parents: 44287
diff changeset
   169
                    parse(fm.getJavaFileObjects(arg), classTrees, true);
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   170
                } else if (isValidPackageName(arg)) {
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   171
                    packageNames.add(arg);
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   172
                } else if (arg.endsWith(".java")) {
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   173
                    if (fm == null) {
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   174
                        String text = messager.getText("main.assertion.error", "fm == null");
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   175
                        throw new ToolException(ABNORMAL, text);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   176
                    } else {
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   177
                        String text = messager.getText("main.file_not_found", arg);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   178
                        throw new ToolException(ERROR, text);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   179
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                } else {
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   181
                    String text = messager.getText("main.illegal_package_name", arg);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   182
                    throw new ToolException(CMDERR, text);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   186
            // Parse file objects provide via the DocumentationTool API
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   187
            parse(fileObjects, classTrees, true);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 35426
diff changeset
   188
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   189
            etable.packages(packageNames)
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   190
                    .classTrees(classTrees.toList())
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   191
                    .scanSpecifiedItems();
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   192
44181
2075fecdcce1 8175860: javadoc crashes with incorrect module sourcepath
ksrini
parents: 43269
diff changeset
   193
            // abort, if errors were encountered during modules initialization
2075fecdcce1 8175860: javadoc crashes with incorrect module sourcepath
ksrini
parents: 43269
diff changeset
   194
            if (messager.hasErrors()) {
2075fecdcce1 8175860: javadoc crashes with incorrect module sourcepath
ksrini
parents: 43269
diff changeset
   195
                return null;
2075fecdcce1 8175860: javadoc crashes with incorrect module sourcepath
ksrini
parents: 43269
diff changeset
   196
            }
2075fecdcce1 8175860: javadoc crashes with incorrect module sourcepath
ksrini
parents: 43269
diff changeset
   197
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   198
            // Parse the files in the packages and subpackages to be documented
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   199
            ListBuffer<JCCompilationUnit> packageTrees = new ListBuffer<>();
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   200
            parse(etable.getFilesToParse(), packageTrees, false);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 35426
diff changeset
   201
            modules.enter(packageTrees.toList(), null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   203
            if (messager.hasErrors()) {
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   204
                return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            }
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   206
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   207
            // Enter symbols for all files
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
   208
            toolEnv.notice("main.Building_tree");
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   209
            javadocEnter.main(classTrees.toList().appendList(packageTrees));
44287
829205b133d4 8175219: javadoc should exit when it encounters compilation errors.
ksrini
parents: 44181
diff changeset
   210
829205b133d4 8175219: javadoc should exit when it encounters compilation errors.
ksrini
parents: 44181
diff changeset
   211
            if (messager.hasErrors()) {
829205b133d4 8175219: javadoc should exit when it encounters compilation errors.
ksrini
parents: 44181
diff changeset
   212
                return null;
829205b133d4 8175219: javadoc should exit when it encounters compilation errors.
ksrini
parents: 44181
diff changeset
   213
            }
829205b133d4 8175219: javadoc should exit when it encounters compilation errors.
ksrini
parents: 44181
diff changeset
   214
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   215
            etable.setClassDeclList(listClasses(classTrees.toList()));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 35426
diff changeset
   216
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 47216
diff changeset
   217
            dcfh.setHandler(dcfh.userCodeHandler);
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   218
            etable.analyze();
52043
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   219
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   220
            // Ensure that package-info is read for all included packages
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   221
            for (Element e : etable.getIncludedElements()) {
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   222
                if (e.getKind() == ElementKind.PACKAGE) {
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   223
                    PackageSymbol packge = (PackageSymbol) e;
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   224
                    if (packge.package_info != null) {
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   225
                        packge.package_info.complete();
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   226
                    }
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   227
                }
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   228
            }
50dc6dd40e6a 8211407: Bad links to non-existent entries on serialized-form page
jjg
parents: 49197
diff changeset
   229
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   230
        } catch (CompletionFailure cf) {
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   231
            throw new ToolException(ABNORMAL, cf.getMessage(), cf);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   232
        } catch (Abort abort) {
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   233
            if (messager.hasErrors()) {
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   234
                // presumably a message has been emitted, keep silent
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   235
                throw new ToolException(ABNORMAL, "", abort);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   236
            } else {
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   237
                String text = messager.getText("main.internal.error");
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   238
                Throwable t = abort.getCause() == null ? abort : abort.getCause();
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   239
                throw new ToolException(ABNORMAL, text, t);
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   240
            }
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   241
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
41451
a847c7aa25a7 8151102: Cleanup javadoc exception handling
ksrini
parents: 40508
diff changeset
   243
        if (messager.hasErrors())
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   244
            return null;
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   245
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   246
        toolEnv.docEnv = new DocEnvImpl(toolEnv, etable);
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
   247
        return toolEnv.docEnv;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    /** Is the given string a valid package name? */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    boolean isValidPackageName(String s) {
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   252
        if (s.contains("/")) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   253
            String[] a = s.split("/");
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   254
            if (a.length == 2) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   255
                 return isValidPackageName0(a[0]) && isValidPackageName0(a[1]);
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   256
            }
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   257
            return false;
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   258
        }
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   259
        return isValidPackageName0(s);
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   260
    }
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   261
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   262
    private boolean isValidPackageName0(String s) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   263
        for (int index = s.indexOf('.') ; index != -1; index = s.indexOf('.')) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   264
            if (!isValidClassName(s.substring(0, index))) {
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   265
                return false;
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   266
            }
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   267
            s = s.substring(index + 1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        return isValidClassName(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   272
    private void parse(Iterable<? extends JavaFileObject> files, ListBuffer<JCCompilationUnit> trees,
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   273
                       boolean trace) {
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   274
        for (JavaFileObject fo: files) {
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   275
            if (uniquefiles.add(fo)) { // ignore duplicates
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   276
                if (trace)
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 38619
diff changeset
   277
                    toolEnv.notice("main.Loading_source_file", fo.getName());
32163
2bdcabf14e91 8133223: Clean up package handling code in JavadocTool
jjg
parents: 30066
diff changeset
   278
                trees.append(parse(fo));
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   279
            }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   280
        }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   281
    }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   282
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   283
    /** Are surrogates supported? */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    final static boolean surrogatesSupported = surrogatesSupported();
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    private static boolean surrogatesSupported() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            boolean b = Character.isHighSurrogate('a');
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        } catch (NoSuchMethodError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     * Return true if given file name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     * (including "package-info").
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10950
diff changeset
   297
     * @param s the name of the class to check.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     * @return true if given class name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    public static boolean isValidClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        if (s.length() < 1) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        if (s.equals("package-info")) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        if (surrogatesSupported) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            int cp = s.codePointAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            if (!Character.isJavaIdentifierStart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                return false;
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   308
            for (int j = Character.charCount(cp); j < s.length(); j += Character.charCount(cp)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                cp = s.codePointAt(j);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                if (!Character.isJavaIdentifierPart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            if (!Character.isJavaIdentifierStart(s.charAt(0)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                return false;
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40311
diff changeset
   316
            for (int j = 1; j < s.length(); j++)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                if (!Character.isJavaIdentifierPart(s.charAt(j)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * From a list of top level trees, return the list of contained class definitions
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
   327
        List<JCClassDecl> result = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        for (JCCompilationUnit t : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            for (JCTree def : t.defs) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10815
diff changeset
   330
                if (def.hasTag(JCTree.Tag.CLASSDEF))
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
   331
                    result.add((JCClassDecl)def);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 32163
diff changeset
   334
        return result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
}