langtools/src/share/classes/com/sun/tools/javadoc/JavadocTool.java
author ksrini
Wed, 16 Apr 2014 18:15:48 -0700
changeset 23973 4b5f3a297142
parent 23116 4c134ccd6b8d
child 24604 7f68545b5128
permissions -rw-r--r--
8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23116
4c134ccd6b8d 8029145: javadoc fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 22163
diff changeset
     2
 * Copyright (c) 2001, 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: 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    28
import java.io.File;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    29
import java.io.IOException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    31
import java.util.EnumSet;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    32
import java.util.HashMap;
23116
4c134ccd6b8d 8029145: javadoc fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 22163
diff changeset
    33
import java.util.HashSet;
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;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    36
import javax.tools.JavaFileManager.Location;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    37
import javax.tools.JavaFileObject;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    38
import javax.tools.StandardJavaFileManager;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    39
import javax.tools.StandardLocation;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    41
import com.sun.tools.javac.code.Symbol.CompletionFailure;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    42
import com.sun.tools.javac.tree.JCTree;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    43
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    44
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    45
import com.sun.tools.javac.util.Abort;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    46
import com.sun.tools.javac.util.Context;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    47
import com.sun.tools.javac.util.List;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    48
import com.sun.tools.javac.util.ListBuffer;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
    49
import com.sun.tools.javac.util.Position;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *  This class could be the main entry point for Javadoc when Javadoc is used as a
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *  component in a larger software system. It provides operations to
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  construct a new javadoc processor, and to run it on a set of source
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  files.
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    57
 *
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    58
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
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: 14259
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: 14259
diff changeset
    61
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    62
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  @author Neal Gafter
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    DocEnv docenv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    final Messager messager;
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
    69
    final JavadocClassReader javadocReader;
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
    70
    final JavadocEnter javadocEnter;
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
    71
    final Set<JavaFileObject> uniquefiles;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * Construct a new JavaCompiler processor, using appropriately
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * extended phases of the underlying compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    protected JavadocTool(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        super(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        messager = Messager.instance0(context);
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
    80
        javadocReader = JavadocClassReader.instance0(context);
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
    81
        javadocEnter = JavadocEnter.instance0(context);
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
    82
        uniquefiles = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    protected boolean keepComments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     *  Construct a new javadoc tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static JavadocTool make0(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        Messager messager = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            // force the use of Javadoc's class reader
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            JavadocClassReader.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            // force the use of Javadoc's own enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            JavadocEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            // force the use of Javadoc's own member enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            JavadocMemberEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            // force the use of Javadoc's own todo phase
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            JavadocTodo.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            // force the use of Messager as a Log
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            messager = Messager.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            return new JavadocTool(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            messager.error(Position.NOPOS, ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public RootDocImpl getRootDocImpl(String doclocale,
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                                      String encoding,
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                                      ModifierFilter filter,
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                                      List<String> javaNames,
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                                      List<String[]> options,
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   125
                                      Iterable<? extends JavaFileObject> fileObjects,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                                      boolean breakiterator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                                      List<String> subPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                                      List<String> excludedPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                                      boolean docClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                                      boolean legacyDoclet,
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                      boolean quiet) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        docenv = DocEnv.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        docenv.showAccess = filter;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   134
        docenv.quiet = quiet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        docenv.breakiterator = breakiterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        docenv.setLocale(doclocale);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        docenv.setEncoding(encoding);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        docenv.docClasses = docClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        docenv.legacyDoclet = legacyDoclet;
19921
58bcbe156057 8023974: Drop 'implements Completer' and 'implements SourceCompleter' from ClassReader resp. JavaCompiler.
jfranck
parents: 14545
diff changeset
   140
        javadocReader.sourceCompleter = docClasses ? null : thisCompleter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   142
        ListBuffer<String> names = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   143
        ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   144
        ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        try {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   147
            StandardJavaFileManager fm = docenv.fileManager instanceof StandardJavaFileManager
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   148
                    ? (StandardJavaFileManager) docenv.fileManager : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                String name = it.head;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   151
                if (!docClasses && fm != null && name.endsWith(".java") && new File(name).exists()) {
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   152
                    JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next();
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   153
                    parse(fo, classTrees, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                } else if (isValidPackageName(name)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                    names = names.append(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                } else if (name.endsWith(".java")) {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   157
                    if (fm == null)
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   158
                        throw new IllegalArgumentException();
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   159
                    else
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
   160
                        docenv.error(null, "main.file_not_found", name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                    docenv.error(null, "main.illegal_package_name", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            }
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   165
            for (JavaFileObject fo: fileObjects) {
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   166
                parse(fo, classTrees, true);
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14543
diff changeset
   167
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            if (!docClasses) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                // Recursively search given subpackages.  If any packages
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                //are found, add them to the list.
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   172
                Map<String,List<JavaFileObject>> packageFiles =
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   173
                        searchSubPackages(subPackages, names, excludedPackages);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                // Parse the packages
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                    // Parse sources ostensibly belonging to package.
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   178
                    String packageName = packs.head;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   179
                    parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                if (messager.nerrors() != 0) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                // Enter symbols for all files
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                docenv.notice("main.Building_tree");
14543
43edeaf6d0a9 8003257: refactor javadoc tool option handling
jjg
parents: 14260
diff changeset
   186
                javadocEnter.main(classTrees.toList().appendList(packTrees.toList()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        } catch (Abort ex) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   190
        if (messager.nerrors() != 0)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   191
            return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        if (docClasses)
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            return new RootDocImpl(docenv, javaNames, options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    /** Is the given string a valid package name? */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    boolean isValidPackageName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        int index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        while ((index = s.indexOf('.')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            if (!isValidClassName(s.substring(0, index))) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            s = s.substring(index+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        return isValidClassName(s);
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
     * search all directories in path for subdirectory name. Add all
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     * .java files found in such a directory to args.
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    private void parsePackageClasses(String name,
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   214
            List<JavaFileObject> files,
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   215
            ListBuffer<JCCompilationUnit> trees,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   216
            List<String> excludedPackages)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   217
            throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        if (excludedPackages.contains(name)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        }
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   221
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        docenv.notice("main.Loading_source_files_for_package", name);
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   223
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   224
        if (files == null) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   225
            Location location = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   226
                    ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   227
            ListBuffer<JavaFileObject> lb = new ListBuffer<>();
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   228
            for (JavaFileObject fo: docenv.fileManager.list(
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   229
                    location, name, EnumSet.of(JavaFileObject.Kind.SOURCE), false)) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   230
                String binaryName = docenv.fileManager.inferBinaryName(location, fo);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   231
                String simpleName = getSimpleName(binaryName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   232
                if (isValidClassName(simpleName)) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   233
                    lb.append(fo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            }
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   236
            files = lb.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        }
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   238
        if (files.nonEmpty()) {
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   239
            for (JavaFileObject fo : files) {
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   240
                parse(fo, trees, false);
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   241
            }
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   242
        } else {
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   243
            messager.warning(Messager.NOPOS, "main.no_source_files_for_package",
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   244
                             name.replace(File.separatorChar, '.'));
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   245
        }
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   246
    }
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   247
23973
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   248
    private void parse(JavaFileObject fo, ListBuffer<JCCompilationUnit> trees,
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   249
                       boolean trace) {
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   250
        if (uniquefiles.add(fo)) { // ignore duplicates
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   251
            if (trace)
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   252
                docenv.notice("main.Loading_source_file", fo.getName());
4b5f3a297142 8037484: [javadoc] fails with java.lang.IllegalStateException: endPosTable already set
ksrini
parents: 23116
diff changeset
   253
            trees.append(parse(fo));
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   254
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     * Recursively search all directories in path for subdirectory name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     * Add all packages found in such a directory to packages list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     */
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   261
    private Map<String,List<JavaFileObject>> searchSubPackages(
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   262
            List<String> subPackages,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   263
            ListBuffer<String> packages,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   264
            List<String> excludedPackages)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   265
            throws IOException {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   266
        Map<String,List<JavaFileObject>> packageFiles = new HashMap<>();
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   267
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   268
        Map<String,Boolean> includedPackages = new HashMap<>();
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   269
        includedPackages.put("", true);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   270
        for (String p: excludedPackages)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   271
            includedPackages.put(p, false);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   272
10635
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   273
        StandardLocation path = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH)
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   274
                ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   275
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   276
        searchSubPackages(subPackages,
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   277
                includedPackages,
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   278
                packages, packageFiles,
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   279
                path,
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   280
                EnumSet.of(JavaFileObject.Kind.SOURCE));
028f55edd8f4 7091528: javadoc attempts to parse .class files
jjg
parents: 6716
diff changeset
   281
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   282
        return packageFiles;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   283
    }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   284
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    private void searchSubPackages(List<String> subPackages,
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   286
            Map<String,Boolean> includedPackages,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   287
            ListBuffer<String> packages,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   288
            Map<String, List<JavaFileObject>> packageFiles,
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   289
            StandardLocation location, Set<JavaFileObject.Kind> kinds)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   290
            throws IOException {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   291
        for (String subPackage: subPackages) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   292
            if (!isIncluded(subPackage, includedPackages))
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   293
                continue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   295
            for (JavaFileObject fo: docenv.fileManager.list(location, subPackage, kinds, true)) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   296
                String binaryName = docenv.fileManager.inferBinaryName(location, fo);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   297
                String packageName = getPackageName(binaryName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   298
                String simpleName = getSimpleName(binaryName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   299
                if (isIncluded(packageName, includedPackages) && isValidClassName(simpleName)) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   300
                    List<JavaFileObject> list = packageFiles.get(packageName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   301
                    list = (list == null ? List.of(fo) : list.prepend(fo));
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   302
                    packageFiles.put(packageName, list);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   303
                    if (!packages.contains(packageName))
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   304
                        packages.add(packageName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   305
                }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   306
            }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   307
        }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   308
    }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   309
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   310
    private String getPackageName(String name) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   311
        int lastDot = name.lastIndexOf(".");
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   312
        return (lastDot == -1 ? "" : name.substring(0, lastDot));
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   313
    }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   314
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   315
    private String getSimpleName(String name) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   316
        int lastDot = name.lastIndexOf(".");
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   317
        return (lastDot == -1 ? name : name.substring(lastDot + 1));
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   318
    }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   319
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   320
    private boolean isIncluded(String packageName, Map<String,Boolean> includedPackages) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   321
        Boolean b = includedPackages.get(packageName);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   322
        if (b == null) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   323
            b = isIncluded(getPackageName(packageName), includedPackages);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   324
            includedPackages.put(packageName, b);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   325
        }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 735
diff changeset
   326
        return b;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
     * Recursively search all directories in path for subdirectory name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
     * Add all packages found in such a directory to packages list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    private void searchSubPackage(String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                                  ListBuffer<String> packages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                                  List<String> excludedPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                                  Collection<File> pathnames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        if (excludedPackages.contains(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        String packageFilename = packageName.replace('.', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        boolean addedPackage = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        for (File pathname : pathnames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            File f = new File(pathname, packageFilename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            String filenames[] = f.list();
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            // if filenames not null, then found directory
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            if (filenames != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                for (String filename : filenames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                    if (!addedPackage
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                            && (isValidJavaSourceFile(filename) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                                isValidJavaClassFile(filename))
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                            && !packages.contains(packageName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                        packages.append(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                        addedPackage = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    } else if (isValidClassName(filename) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                               (new File(f, filename)).isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                        searchSubPackage(packageName + "." + filename,
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                                         packages, excludedPackages, pathnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     * Return true if given file name is a valid class file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
     * @param file the name of the file to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     * @return true if given file name is a valid class file name
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    private static boolean isValidJavaClassFile(String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        if (!file.endsWith(".class")) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        String clazzName = file.substring(0, file.length() - ".class".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        return isValidClassName(clazzName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     * Return true if given file name is a valid Java source file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     * @param file the name of the file to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     * @return true if given file name is a valid Java source file name
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    private static boolean isValidJavaSourceFile(String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        if (!file.endsWith(".java")) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        String clazzName = file.substring(0, file.length() - ".java".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        return isValidClassName(clazzName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    /** Are surrogates supported?
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    final static boolean surrogatesSupported = surrogatesSupported();
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
    private static boolean surrogatesSupported() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            boolean b = Character.isHighSurrogate('a');
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        } catch (NoSuchMethodError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
     * Return true if given file name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * (including "package-info").
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10950
diff changeset
   403
     * @param s the name of the class to check.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
     * @return true if given class name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
    public static boolean isValidClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        if (s.length() < 1) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        if (s.equals("package-info")) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        if (surrogatesSupported) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            int cp = s.codePointAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            if (!Character.isJavaIdentifierStart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                cp = s.codePointAt(j);
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                if (!Character.isJavaIdentifierPart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            if (!Character.isJavaIdentifierStart(s.charAt(0)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            for (int j=1; j<s.length(); j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                if (!Character.isJavaIdentifierPart(s.charAt(j)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     * From a list of top level trees, return the list of contained class definitions
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19921
diff changeset
   433
        ListBuffer<JCClassDecl> result = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        for (JCCompilationUnit t : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            for (JCTree def : t.defs) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10815
diff changeset
   436
                if (def.hasTag(JCTree.Tag.CLASSDEF))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                    result.append((JCClassDecl)def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
}