langtools/src/share/classes/com/sun/tools/javadoc/JavadocTool.java
author xdono
Wed, 02 Jul 2008 12:56:02 -0700
changeset 735 372aa565a221
parent 731 1dd22bdb9ca5
child 1869 0e193a8f3520
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 731
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.comp.*;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    34
import com.sun.tools.javac.file.Paths;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.parser.DocCommentScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  This class could be the main entry point for Javadoc when Javadoc is used as a
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  component in a larger software system. It provides operations to
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  construct a new javadoc processor, and to run it on a set of source
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  files.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  @author Neal Gafter
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    DocEnv docenv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    final Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    final Messager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    final JavadocClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    final JavadocEnter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    final Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private final Paths paths;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * Construct a new JavaCompiler processor, using appropriately
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * extended phases of the underlying compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    protected JavadocTool(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        super(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        messager = Messager.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        reader = JavadocClassReader.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        enter = JavadocEnter.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        paths = Paths.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    protected boolean keepComments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     *  Construct a new javadoc tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    public static JavadocTool make0(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        Messager messager = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            // force the use of Javadoc's class reader
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            JavadocClassReader.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            // force the use of Javadoc's own enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            JavadocEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            // force the use of Javadoc's own member enter phase
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            JavadocMemberEnter.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            // force the use of Javadoc's own todo phase
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            JavadocTodo.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            // force the use of Messager as a Log
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            messager = Messager.instance0(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            // force the use of the scanner that captures Javadoc comments
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            DocCommentScanner.Factory.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            return new JavadocTool(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            messager.error(Position.NOPOS, ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public RootDocImpl getRootDocImpl(String doclocale,
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                                      String encoding,
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                                      ModifierFilter filter,
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                                      List<String> javaNames,
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                                      List<String[]> options,
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                                      boolean breakiterator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                                      List<String> subPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                                      List<String> excludedPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                                      boolean docClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                                      boolean legacyDoclet,
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                      boolean quiet) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        docenv = DocEnv.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        docenv.showAccess = filter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    docenv.quiet = quiet;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        docenv.breakiterator = breakiterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        docenv.setLocale(doclocale);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        docenv.setEncoding(encoding);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        docenv.docClasses = docClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        docenv.legacyDoclet = legacyDoclet;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        reader.sourceCompleter = docClasses ? null : this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        ListBuffer<String> names = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                String name = it.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                if (!docClasses && name.endsWith(".java") && new File(name).exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    docenv.notice("main.Loading_source_file", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                        JCCompilationUnit tree = parse(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                        classTrees.append(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                } else if (isValidPackageName(name)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                    names = names.append(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                } else if (name.endsWith(".java")) {
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
   145
                    docenv.error(null, "main.file_not_found", name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                    docenv.error(null, "main.illegal_package_name", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            if (!docClasses) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                // Recursively search given subpackages.  If any packages
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                //are found, add them to the list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                searchSubPackages(subPackages, names, excludedPackages);
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                // Parse the packages
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                    // Parse sources ostensibly belonging to package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                    parsePackageClasses(packs.head, packTrees, excludedPackages);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                if (messager.nerrors() != 0) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                // Enter symbols for all files
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                docenv.notice("main.Building_tree");
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                enter.main(classTrees.toList().appendList(packTrees.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        } catch (Abort ex) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        if (messager.nerrors() != 0) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        if (docClasses)
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            return new RootDocImpl(docenv, javaNames, options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** Is the given string a valid package name? */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    boolean isValidPackageName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        int index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        while ((index = s.indexOf('.')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            if (!isValidClassName(s.substring(0, index))) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            s = s.substring(index+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        return isValidClassName(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    private final static char pathSep = File.pathSeparatorChar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     * search all directories in path for subdirectory name. Add all
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     * .java files found in such a directory to args.
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    private void parsePackageClasses(String name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                                     ListBuffer<JCCompilationUnit> trees,
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                                     List<String> excludedPackages)
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        if (excludedPackages.contains(name)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        boolean hasFiles = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        docenv.notice("main.Loading_source_files_for_package", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        name = name.replace('.', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        for (File pathname : paths.sourceSearchPath()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            File f = new File(pathname, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            String names[] = f.list();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            // if names not null, then found directory with source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            if (names != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                String dir = f.getAbsolutePath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                if (!dir.endsWith(File.separator))
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                    dir = dir + File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                for (int j = 0; j < names.length; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                    if (isValidJavaSourceFile(names[j])) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                        String fn = dir + names[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                        // messager.notice("main.Loading_source_file", fn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                            trees.append(parse(fn));
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                        hasFiles = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        if (!hasFiles)
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            messager.warning(null, "main.no_source_files_for_package",
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                             name.replace(File.separatorChar, '.'));
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     * Recursively search all directories in path for subdirectory name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     * Add all packages found in such a directory to packages list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    private void searchSubPackages(List<String> subPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                                   ListBuffer<String> packages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                                   List<String> excludedPackages) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        // FIXME: This search path is bogus.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        // Only the effective source path should be searched for sources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        // Only the effective class path should be searched for classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        // Should the bootclasspath/extdirs also be searched for classes?
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        java.util.List<File> pathnames = new java.util.ArrayList<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        if (paths.sourcePath() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            for (File elt : paths.sourcePath())
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                pathnames.add(elt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        for (File elt : paths.userClassPath())
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            pathnames.add(elt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        for (String subPackage : subPackages)
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            searchSubPackage(subPackage, packages, excludedPackages, pathnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     * Recursively search all directories in path for subdirectory name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     * Add all packages found in such a directory to packages list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    private void searchSubPackage(String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                                  ListBuffer<String> packages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                                  List<String> excludedPackages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                                  Collection<File> pathnames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        if (excludedPackages.contains(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        String packageFilename = packageName.replace('.', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        boolean addedPackage = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        for (File pathname : pathnames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            File f = new File(pathname, packageFilename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            String filenames[] = f.list();
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            // if filenames not null, then found directory
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            if (filenames != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                for (String filename : filenames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    if (!addedPackage
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                            && (isValidJavaSourceFile(filename) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                                isValidJavaClassFile(filename))
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                            && !packages.contains(packageName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                        packages.append(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                        addedPackage = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    } else if (isValidClassName(filename) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                               (new File(f, filename)).isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                        searchSubPackage(packageName + "." + filename,
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                                         packages, excludedPackages, pathnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     * Return true if given file name is a valid class file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     * @param file the name of the file to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * @return true if given file name is a valid class file name
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    private static boolean isValidJavaClassFile(String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        if (!file.endsWith(".class")) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        String clazzName = file.substring(0, file.length() - ".class".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        return isValidClassName(clazzName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     * Return true if given file name is a valid Java source file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     * @param file the name of the file to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     * @return true if given file name is a valid Java source file name
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    private static boolean isValidJavaSourceFile(String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        if (!file.endsWith(".java")) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        String clazzName = file.substring(0, file.length() - ".java".length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        return isValidClassName(clazzName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    /** Are surrogates supported?
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    final static boolean surrogatesSupported = surrogatesSupported();
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    private static boolean surrogatesSupported() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            boolean b = Character.isHighSurrogate('a');
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        } catch (NoSuchMethodError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * Return true if given file name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     * (including "package-info").
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * @param clazzname the name of the class to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     * @return true if given class name is a valid class name
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     * and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    public static boolean isValidClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        if (s.length() < 1) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        if (s.equals("package-info")) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        if (surrogatesSupported) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            int cp = s.codePointAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            if (!Character.isJavaIdentifierStart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                cp = s.codePointAt(j);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                if (!Character.isJavaIdentifierPart(cp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            if (!Character.isJavaIdentifierStart(s.charAt(0)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            for (int j=1; j<s.length(); j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                if (!Character.isJavaIdentifierPart(s.charAt(j)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     * From a list of top level trees, return the list of contained class definitions
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        ListBuffer<JCClassDecl> result = new ListBuffer<JCClassDecl>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        for (JCCompilationUnit t : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            for (JCTree def : t.defs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                if (def.getTag() == JCTree.CLASSDEF)
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                    result.append((JCClassDecl)def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
}