src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/Main.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 48343 d4329843abf4
child 48942 a6c4b85163c1
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48343
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     4
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    10
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    15
 * accompanied this code).
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    16
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    20
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    23
 * questions.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    24
 */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    25
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    26
package com.sun.tools.jdeprscan;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    27
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    28
import java.io.File;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    29
import java.io.IOException;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    30
import java.io.PrintStream;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    31
import java.net.URI;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    32
import java.nio.charset.StandardCharsets;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    33
import java.nio.file.Files;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    34
import java.nio.file.FileSystems;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    35
import java.nio.file.Path;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    36
import java.nio.file.Paths;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    37
import java.util.ArrayDeque;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    38
import java.util.ArrayList;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    39
import java.util.Arrays;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    40
import java.util.Collection;
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
    41
import java.util.EnumSet;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    42
import java.util.HashSet;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    43
import java.util.List;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    44
import java.util.Map;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    45
import java.util.NoSuchElementException;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    46
import java.util.Set;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    47
import java.util.Queue;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    48
import java.util.stream.Stream;
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
    49
import java.util.stream.StreamSupport;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    50
import java.util.jar.JarEntry;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    51
import java.util.jar.JarFile;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    52
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    53
import javax.tools.Diagnostic;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    54
import javax.tools.DiagnosticListener;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    55
import javax.tools.JavaCompiler;
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
    56
import javax.tools.JavaFileManager;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    57
import javax.tools.JavaFileObject;
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
    58
import javax.tools.JavaFileObject.Kind;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    59
import javax.tools.StandardJavaFileManager;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    60
import javax.tools.StandardLocation;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    61
import javax.tools.ToolProvider;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    62
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    63
import com.sun.tools.javac.file.JavacFileManager;
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
    64
import com.sun.tools.javac.platform.JDKPlatformProvider;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    65
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    66
import com.sun.tools.jdeprscan.scan.Scan;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    67
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    68
import static java.util.stream.Collectors.*;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    69
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    70
import javax.lang.model.element.PackageElement;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    71
import javax.lang.model.element.TypeElement;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    72
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    73
/**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    74
 * Deprecation Scanner tool. Loads API deprecation information from the
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    75
 * JDK image, or optionally, from a jar file or class hierarchy. Then scans
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    76
 * a class library for usages of those APIs.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    77
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    78
 * TODO:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    79
 *  - audit error handling throughout, but mainly in scan package
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    80
 *  - handling of covariant overrides
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    81
 *  - handling of override of method found in multiple superinterfaces
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    82
 *  - convert type/method/field output to Java source like syntax, e.g.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    83
 *      instead of java/lang/Runtime.runFinalizersOnExit(Z)V
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    84
 *      print void java.lang.Runtime.runFinalizersOnExit(boolean)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    85
 *  - more example output in man page
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    86
 *  - more rigorous GNU style option parsing; use joptsimple?
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    87
 *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    88
 * FUTURES:
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40761
diff changeset
    89
 *  - add module support: --add-modules, --module-path, module arg
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    90
 *  - load deprecation declarations from a designated class library instead
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    91
 *    of the JDK
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    92
 *  - load deprecation declarations from a module
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    93
 *  - scan a module (but a modular jar can be treated just a like an ordinary jar)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    94
 *  - multi-version jar
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    95
 */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    96
public class Main implements DiagnosticListener<JavaFileObject> {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    97
    final PrintStream out;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    98
    final PrintStream err;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
    99
    final List<File> bootClassPath = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   100
    final List<File> classPath = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   101
    final List<File> systemModules = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   102
    final List<String> options = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   103
    final List<String> comments = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   104
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   105
    // Valid releases need to match what the compiler supports.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   106
    // Keep these updated manually until there's a compiler API
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   107
    // that allows querying of supported releases.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   108
    final Set<String> releasesWithoutForRemoval = Set.of("6", "7", "8");
48343
d4329843abf4 8173382: Add -source 11 and -target 11 to javac
darcy
parents: 47702
diff changeset
   109
    final Set<String> releasesWithForRemoval = Set.of("9", "10", "11");
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   110
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   111
    final Set<String> validReleases;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   112
    {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   113
        Set<String> temp = new HashSet<>(releasesWithoutForRemoval);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   114
        temp.addAll(releasesWithForRemoval);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   115
        validReleases = Set.of(temp.toArray(new String[0]));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   116
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   117
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   118
    boolean verbose = false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   119
    boolean forRemoval = false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   120
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   121
    final JavaCompiler compiler;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   122
    final StandardJavaFileManager fm;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   123
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   124
    List<DeprData> deprList; // non-null after successful load phase
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   125
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   126
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   127
     * Processes a collection of class names. Names should fully qualified
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   128
     * names in the form "pkg.pkg.pkg.classname".
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   129
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   130
     * @param classNames collection of fully qualified classnames to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   131
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   132
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   133
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   134
    boolean doClassNames(Collection<String> classNames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   135
        if (verbose) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   136
            out.println("List of classes to process:");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   137
            classNames.forEach(out::println);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   138
            out.println("End of class list.");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   139
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   140
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   141
        // TODO: not sure this is necessary...
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   142
        if (fm instanceof JavacFileManager) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   143
            ((JavacFileManager)fm).setSymbolFileEnabled(false);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   144
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   145
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   146
        fm.setLocation(StandardLocation.CLASS_PATH, classPath);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   147
        if (!bootClassPath.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   148
            fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   149
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   150
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   151
        if (!systemModules.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   152
            fm.setLocation(StandardLocation.SYSTEM_MODULES, systemModules);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   153
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   154
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   155
        LoadProc proc = new LoadProc();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   156
        JavaCompiler.CompilationTask task =
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   157
            compiler.getTask(null, fm, this, options, classNames, null);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   158
        task.setProcessors(List.of(proc));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   159
        boolean r = task.call();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   160
        if (r) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   161
            if (forRemoval) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   162
                deprList = proc.getDeprecations().stream()
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   163
                               .filter(DeprData::isForRemoval)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   164
                               .collect(toList());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   165
            } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   166
                deprList = proc.getDeprecations();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   167
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   168
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   169
        return r;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   170
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   171
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   172
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   173
     * Processes a stream of filenames (strings). The strings are in the
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   174
     * form pkg/pkg/pkg/classname.class relative to the root of a package
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   175
     * hierarchy.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   176
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   177
     * @param filenames a Stream of filenames to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   178
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   179
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   180
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   181
    boolean doFileNames(Stream<String> filenames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   182
        return doClassNames(
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   183
            filenames.filter(name -> name.endsWith(".class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   184
                     .filter(name -> !name.endsWith("package-info.class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   185
                     .filter(name -> !name.endsWith("module-info.class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   186
                     .map(s -> s.replaceAll("\\.class$", ""))
40761
e56fe259825c 8164837: fix jdeprscan TestLoad and TestScan failures on Windows
smarks
parents: 40596
diff changeset
   187
                     .map(s -> s.replace(File.separatorChar, '.'))
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   188
                     .collect(toList()));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   189
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   190
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   191
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   192
     * Replaces all but the first occurrence of '/' with '.'. Assumes
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   193
     * that the name is in the format module/pkg/pkg/classname.class.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   194
     * That is, the name should contain at least one '/' character
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   195
     * separating the module name from the package-class name.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   196
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   197
     * @param filename the input filename
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   198
     * @return the modular classname
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   199
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   200
    String convertModularFileName(String filename) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   201
        int slash = filename.indexOf('/');
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   202
        return filename.substring(0, slash)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   203
               + "/"
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   204
               + filename.substring(slash+1).replace('/', '.');
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   205
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   206
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   207
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   208
     * Processes a stream of filenames (strings) including a module prefix.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   209
     * The strings are in the form module/pkg/pkg/pkg/classname.class relative
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   210
     * to the root of a directory containing modules. The strings are processed
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   211
     * into module-qualified class names of the form
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   212
     * "module/pkg.pkg.pkg.classname".
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   213
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   214
     * @param filenames a Stream of filenames to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   215
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   216
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   217
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   218
    boolean doModularFileNames(Stream<String> filenames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   219
        return doClassNames(
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   220
            filenames.filter(name -> name.endsWith(".class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   221
                     .filter(name -> !name.endsWith("package-info.class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   222
                     .filter(name -> !name.endsWith("module-info.class"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   223
                     .map(s -> s.replaceAll("\\.class$", ""))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   224
                     .map(this::convertModularFileName)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   225
                     .collect(toList()));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   226
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   227
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   228
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   229
     * Processes named class files in the given directory. The directory
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   230
     * should be the root of a package hierarchy. If classNames is
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   231
     * empty, walks the directory hierarchy to find all classes.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   232
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   233
     * @param dirname the name of the directory to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   234
     * @param classNames the names of classes to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   235
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   236
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   237
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   238
    boolean processDirectory(String dirname, Collection<String> classNames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   239
        if (!Files.isDirectory(Paths.get(dirname))) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   240
            err.printf("%s: not a directory%n", dirname);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   241
            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   242
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   243
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   244
        classPath.add(0, new File(dirname));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   245
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   246
        if (classNames.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   247
            Path base = Paths.get(dirname);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   248
            int baseCount = base.getNameCount();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   249
            try (Stream<Path> paths = Files.walk(base)) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   250
                Stream<String> files =
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   251
                    paths.filter(p -> p.getNameCount() > baseCount)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   252
                         .map(p -> p.subpath(baseCount, p.getNameCount()))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   253
                         .map(Path::toString);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   254
                return doFileNames(files);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   255
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   256
        } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   257
            return doClassNames(classNames);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   258
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   259
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   260
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   261
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   262
     * Processes all class files in the given jar file.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   263
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   264
     * @param jarname the name of the jar file to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   265
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   266
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   267
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   268
    boolean doJarFile(String jarname) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   269
        try (JarFile jf = new JarFile(jarname)) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   270
            Stream<String> files =
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   271
                jf.stream()
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   272
                  .map(JarEntry::getName);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   273
            return doFileNames(files);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   274
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   275
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   276
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   277
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   278
     * Processes named class files from the given jar file,
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   279
     * or all classes if classNames is empty.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   280
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   281
     * @param jarname the name of the jar file to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   282
     * @param classNames the names of classes to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   283
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   284
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   285
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   286
    boolean processJarFile(String jarname, Collection<String> classNames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   287
        classPath.add(0, new File(jarname));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   288
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   289
        if (classNames.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   290
            return doJarFile(jarname);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   291
        } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   292
            return doClassNames(classNames);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   293
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   294
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   295
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   296
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   297
     * Processes named class files from rt.jar of a JDK version 7 or 8.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   298
     * If classNames is empty, processes all classes.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   299
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   300
     * @param jdkHome the path to the "home" of the JDK to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   301
     * @param classNames the names of classes to process
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   302
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   303
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   304
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   305
    boolean processOldJdk(String jdkHome, Collection<String> classNames) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   306
        String RTJAR = jdkHome + "/jre/lib/rt.jar";
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   307
        String CSJAR = jdkHome + "/jre/lib/charsets.jar";
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   308
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   309
        bootClassPath.add(0, new File(RTJAR));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   310
        bootClassPath.add(1, new File(CSJAR));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   311
        options.add("-source");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   312
        options.add("8");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   313
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   314
        if (classNames.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   315
            return doJarFile(RTJAR);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   316
        } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   317
            return doClassNames(classNames);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   318
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   319
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   320
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   321
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   322
     * Processes listed classes given a JDK 9 home.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   323
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   324
    boolean processJdk9(String jdkHome, Collection<String> classes) throws IOException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   325
        systemModules.add(new File(jdkHome));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   326
        return doClassNames(classes);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   327
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   328
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   329
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   330
     * Processes the class files from the currently running JDK,
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   331
     * using the jrt: filesystem.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   332
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   333
     * @return true for success, false for failure
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   334
     * @throws IOException if an I/O error occurs
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   335
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   336
    boolean processSelf(Collection<String> classes) throws IOException {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40761
diff changeset
   337
        options.add("--add-modules");
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   338
        options.add("java.se.ee,jdk.xml.bind"); // TODO why jdk.xml.bind?
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   339
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   340
        if (classes.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   341
            Path modules = FileSystems.getFileSystem(URI.create("jrt:/"))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   342
                                      .getPath("/modules");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   343
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   344
            // names are /modules/<modulename>/pkg/.../Classname.class
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   345
            try (Stream<Path> paths = Files.walk(modules)) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   346
                Stream<String> files =
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   347
                    paths.filter(p -> p.getNameCount() > 2)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   348
                         .map(p -> p.subpath(1, p.getNameCount()))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   349
                         .map(Path::toString);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   350
                return doModularFileNames(files);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   351
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   352
        } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   353
            return doClassNames(classes);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   354
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   355
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   356
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   357
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   358
     * Process classes from a particular JDK release, using only information
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   359
     * in this JDK.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   360
     *
48343
d4329843abf4 8173382: Add -source 11 and -target 11 to javac
darcy
parents: 47702
diff changeset
   361
     * @param release "6", "7", "8", "9", "10", or "11"
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   362
     * @param classes collection of classes to process, may be empty
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   363
     * @return success value
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   364
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   365
    boolean processRelease(String release, Collection<String> classes) throws IOException {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40761
diff changeset
   366
        options.addAll(List.of("--release", release));
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   367
48343
d4329843abf4 8173382: Add -source 11 and -target 11 to javac
darcy
parents: 47702
diff changeset
   368
        if (release.equals("9") || release.equals("10") ||
d4329843abf4 8173382: Add -source 11 and -target 11 to javac
darcy
parents: 47702
diff changeset
   369
            release.equals("11")) {
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   370
            List<String> rootMods = List.of("java.se", "java.se.ee");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   371
            TraverseProc proc = new TraverseProc(rootMods);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   372
            JavaCompiler.CompilationTask task =
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   373
                compiler.getTask(null, fm, this,
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   374
                                 // options
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40761
diff changeset
   375
                                 List.of("--add-modules", String.join(",", rootMods)),
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   376
                                 // classes
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   377
                                 List.of("java.lang.Object"),
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   378
                                 null);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   379
            task.setProcessors(List.of(proc));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   380
            if (!task.call()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   381
                return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   382
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   383
            Map<PackageElement, List<TypeElement>> types = proc.getPublicTypes();
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40761
diff changeset
   384
            options.add("--add-modules");
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   385
            options.add(String.join(",", rootMods));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   386
            return doClassNames(
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   387
                types.values().stream()
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   388
                     .flatMap(List::stream)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   389
                     .map(TypeElement::toString)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   390
                     .collect(toList()));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   391
        } else {
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   392
            JDKPlatformProvider pp = new JDKPlatformProvider();
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   393
            if (StreamSupport.stream(pp.getSupportedPlatformNames().spliterator(),
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   394
                                 false)
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   395
                             .noneMatch(n -> n.equals(release))) {
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   396
                return false;
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   397
            }
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   398
            JavaFileManager fm = pp.getPlatform(release, "").getFileManager();
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   399
            List<String> classNames = new ArrayList<>();
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   400
            for (JavaFileObject fo : fm.list(StandardLocation.PLATFORM_CLASS_PATH,
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   401
                                             "",
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   402
                                             EnumSet.of(Kind.CLASS),
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   403
                                             true)) {
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   404
                classNames.add(fm.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fo));
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   405
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   406
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   407
            options.add("-Xlint:-options");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   408
47702
cf8310446245 8139607: -release option forces StandardJavaFileManager
jlahoda
parents: 47216
diff changeset
   409
            return doClassNames(classNames);
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   410
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   411
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   412
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   413
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   414
     * An enum denoting the mode in which the tool is running.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   415
     * Different modes correspond to the different process* methods.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   416
     * The exception is UNKNOWN, which indicates that a mode wasn't
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   417
     * specified on the command line, which is an error.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   418
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   419
    static enum LoadMode {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   420
        CLASSES, DIR, JAR, OLD_JDK, JDK9, SELF, RELEASE, LOAD_CSV
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   421
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   422
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   423
    static enum ScanMode {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   424
        ARGS, LIST, PRINT_CSV
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   425
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   426
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   427
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   428
     * A checked exception that's thrown if a command-line syntax error
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   429
     * is detected.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   430
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   431
    static class UsageException extends Exception {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   432
        private static final long serialVersionUID = 3611828659572908743L;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   433
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   434
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   435
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   436
     * Convenience method to throw UsageException if a condition is false.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   437
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   438
     * @param cond the condition that's required to be true
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   439
     * @throws UsageException
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   440
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   441
    void require(boolean cond) throws UsageException {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   442
        if (!cond) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   443
            throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   444
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   445
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   446
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   447
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   448
     * Constructs an instance of the finder tool.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   449
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   450
     * @param out the stream to which the tool's output is sent
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   451
     * @param err the stream to which error messages are sent
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   452
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   453
    Main(PrintStream out, PrintStream err) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   454
        this.out = out;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   455
        this.err = err;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   456
        compiler = ToolProvider.getSystemJavaCompiler();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   457
        fm = compiler.getStandardFileManager(this, null, StandardCharsets.UTF_8);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   458
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   459
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   460
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   461
     * Prints the diagnostic to the err stream.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   462
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   463
     * Specified by the DiagnosticListener interface.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   464
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   465
     * @param diagnostic the tool diagnostic to print
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   466
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   467
    @Override
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   468
    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   469
        err.println(diagnostic);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   470
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   471
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   472
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   473
     * Parses arguments and performs the requested processing.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   474
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   475
     * @param argArray command-line arguments
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   476
     * @return true on success, false on error
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   477
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   478
    boolean run(String... argArray) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   479
        Queue<String> args = new ArrayDeque<>(Arrays.asList(argArray));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   480
        LoadMode loadMode = LoadMode.RELEASE;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   481
        ScanMode scanMode = ScanMode.ARGS;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   482
        String dir = null;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   483
        String jar = null;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   484
        String jdkHome = null;
48343
d4329843abf4 8173382: Add -source 11 and -target 11 to javac
darcy
parents: 47702
diff changeset
   485
        String release = "11";
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   486
        List<String> loadClasses = new ArrayList<>();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   487
        String csvFile = null;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   488
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   489
        try {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   490
            while (!args.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   491
                String a = args.element();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   492
                if (a.startsWith("-")) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   493
                    args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   494
                    switch (a) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   495
                        case "--class-path":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   496
                            classPath.clear();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   497
                            Arrays.stream(args.remove().split(File.pathSeparator))
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   498
                                  .map(File::new)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   499
                                  .forEachOrdered(classPath::add);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   500
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   501
                        case "--for-removal":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   502
                            forRemoval = true;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   503
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   504
                        case "--full-version":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   505
                            out.println(System.getProperty("java.vm.version"));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   506
                            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   507
                        case "--help":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   508
                        case "-h":
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48343
diff changeset
   509
                        case "-?":
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   510
                            out.println(Messages.get("main.usage"));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   511
                            out.println();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   512
                            out.println(Messages.get("main.help"));
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48343
diff changeset
   513
                            return true;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   514
                        case "-l":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   515
                        case "--list":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   516
                            require(scanMode == ScanMode.ARGS);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   517
                            scanMode = ScanMode.LIST;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   518
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   519
                        case "--release":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   520
                            loadMode = LoadMode.RELEASE;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   521
                            release = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   522
                            if (!validReleases.contains(release)) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   523
                                throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   524
                            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   525
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   526
                        case "-v":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   527
                        case "--verbose":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   528
                            verbose = true;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   529
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   530
                        case "--version":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   531
                            out.println(System.getProperty("java.version"));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   532
                            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   533
                        case "--Xcompiler-arg":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   534
                            options.add(args.remove());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   535
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   536
                        case "--Xcsv-comment":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   537
                            comments.add(args.remove());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   538
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   539
                        case "--Xhelp":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   540
                            out.println(Messages.get("main.xhelp"));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   541
                            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   542
                        case "--Xload-class":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   543
                            loadMode = LoadMode.CLASSES;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   544
                            loadClasses.add(args.remove());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   545
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   546
                        case "--Xload-csv":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   547
                            loadMode = LoadMode.LOAD_CSV;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   548
                            csvFile = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   549
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   550
                        case "--Xload-dir":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   551
                            loadMode = LoadMode.DIR;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   552
                            dir = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   553
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   554
                        case "--Xload-jar":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   555
                            loadMode = LoadMode.JAR;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   556
                            jar = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   557
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   558
                        case "--Xload-jdk9":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   559
                            loadMode = LoadMode.JDK9;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   560
                            jdkHome = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   561
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   562
                        case "--Xload-old-jdk":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   563
                            loadMode = LoadMode.OLD_JDK;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   564
                            jdkHome = args.remove();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   565
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   566
                        case "--Xload-self":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   567
                            loadMode = LoadMode.SELF;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   568
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   569
                        case "--Xprint-csv":
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   570
                            require(scanMode == ScanMode.ARGS);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   571
                            scanMode = ScanMode.PRINT_CSV;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   572
                            break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   573
                        default:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   574
                            throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   575
                    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   576
                } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   577
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   578
                }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   579
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   580
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   581
            if ((scanMode == ScanMode.ARGS) == args.isEmpty()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   582
                throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   583
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   584
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   585
            if (    forRemoval && loadMode == LoadMode.RELEASE &&
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   586
                    releasesWithoutForRemoval.contains(release)) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   587
                throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   588
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   589
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   590
            boolean success = false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   591
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   592
            switch (loadMode) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   593
                case CLASSES:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   594
                    success = doClassNames(loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   595
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   596
                case DIR:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   597
                    success = processDirectory(dir, loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   598
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   599
                case JAR:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   600
                    success = processJarFile(jar, loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   601
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   602
                case JDK9:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   603
                    require(!args.isEmpty());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   604
                    success = processJdk9(jdkHome, loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   605
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   606
                case LOAD_CSV:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   607
                    deprList = DeprDB.loadFromFile(csvFile);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   608
                    success = true;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   609
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   610
                case OLD_JDK:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   611
                    success = processOldJdk(jdkHome, loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   612
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   613
                case RELEASE:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   614
                    success = processRelease(release, loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   615
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   616
                case SELF:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   617
                    success = processSelf(loadClasses);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   618
                    break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   619
                default:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   620
                    throw new UsageException();
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   621
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   622
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   623
            if (!success) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   624
                return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   625
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   626
        } catch (NoSuchElementException | UsageException ex) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   627
            err.println(Messages.get("main.usage"));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   628
            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   629
        } catch (IOException ioe) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   630
            if (verbose) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   631
                ioe.printStackTrace(err);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   632
            } else {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   633
                err.println(ioe);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   634
            }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   635
            return false;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   636
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   637
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   638
        // now the scanning phase
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   639
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   640
        boolean scanStatus = true;
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   641
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   642
        switch (scanMode) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   643
            case LIST:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   644
                for (DeprData dd : deprList) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   645
                    if (!forRemoval || dd.isForRemoval()) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   646
                        out.println(Pretty.print(dd));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   647
                    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   648
                }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   649
                break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   650
            case PRINT_CSV:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   651
                out.println("#jdepr1");
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   652
                comments.forEach(s -> out.println("# " + s));
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   653
                for (DeprData dd : deprList) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   654
                    CSV.write(out, dd.kind, dd.typeName, dd.nameSig, dd.since, dd.forRemoval);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   655
                }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   656
                break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   657
            case ARGS:
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   658
                DeprDB db = DeprDB.loadFromList(deprList);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   659
                List<String> cp = classPath.stream()
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   660
                                           .map(File::toString)
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   661
                                           .collect(toList());
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   662
                Scan scan = new Scan(out, err, cp, db, verbose);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   663
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   664
                for (String a : args) {
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   665
                    boolean s;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   666
                    if (a.endsWith(".jar")) {
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   667
                        s = scan.scanJar(a);
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   668
                    } else if (a.endsWith(".class")) {
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   669
                        s = scan.processClassFile(a);
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   670
                    } else if (Files.isDirectory(Paths.get(a))) {
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   671
                        s = scan.scanDir(a);
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   672
                    } else {
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   673
                        s = scan.processClassName(a.replace('.', '/'));
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   674
                    }
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   675
                    scanStatus = scanStatus && s;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   676
                }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   677
                break;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   678
        }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   679
41863
98b049c8b848 8165646: (jdeprscan) adjust tool output to improve clarity
smarks
parents: 41525
diff changeset
   680
        return scanStatus;
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   681
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   682
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   683
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   684
     * Programmatic main entry point: initializes the tool instance to
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   685
     * use stdout and stderr; runs the tool, passing command-line args;
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   686
     * returns an exit status.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   687
     *
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   688
     * @return true on success, false otherwise
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   689
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   690
    public static boolean call(PrintStream out, PrintStream err, String... args) {
41439
ee4857e4fa85 8161338: (jdeprscan) remove JEP 293 non-conforming -cp option
smarks
parents: 40762
diff changeset
   691
        return new Main(out, err).run(args);
40596
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   692
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   693
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   694
    /**
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   695
     * Calls the main entry point and exits the JVM with an exit
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   696
     * status determined by the return status.
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   697
     */
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   698
    public static void main(String[] args) {
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   699
        System.exit(call(System.out, System.err, args) ? 0 : 1);
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   700
    }
43b19b36e8e6 8145464: implement deprecation static analysis tool
smarks
parents:
diff changeset
   701
}