1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
84 * - add module support: --add-modules, --module-path, module arg
85 * - load deprecation declarations from a designated class library instead
86 * of the JDK
87 * - load deprecation declarations from a module
88 * - scan a module (but a modular jar can be treated just a like an ordinary jar)
89 * - multi-version jar
90 */
91 public class Main implements DiagnosticListener<JavaFileObject> {
92 final PrintStream out;
93 final PrintStream err;
94 final List<File> bootClassPath = new ArrayList<>();
95 final List<File> classPath = new ArrayList<>();
96 final List<File> systemModules = new ArrayList<>();
97 final List<String> options = new ArrayList<>();
98 final List<String> comments = new ArrayList<>();
99
100 // Valid releases need to match what the compiler supports.
101 // Keep these updated manually until there's a compiler API
102 // that allows querying of supported releases.
103 final Set<String> releasesWithoutForRemoval = Set.of("6", "7", "8");
104 final Set<String> releasesWithForRemoval = Set.of("9");
105
106 final Set<String> validReleases;
107 {
108 Set<String> temp = new HashSet<>(releasesWithoutForRemoval);
109 temp.addAll(releasesWithForRemoval);
110 validReleases = Set.of(temp.toArray(new String[0]));
111 }
112
113 boolean verbose = false;
114 boolean forRemoval = false;
115
116 final JavaCompiler compiler;
117 final StandardJavaFileManager fm;
118
119 List<DeprData> deprList; // non-null after successful load phase
120
121 /**
122 * Processes a collection of class names. Names should fully qualified
123 * names in the form "pkg.pkg.pkg.classname".
124 *
336 Path modules = FileSystems.getFileSystem(URI.create("jrt:/"))
337 .getPath("/modules");
338
339 // names are /modules/<modulename>/pkg/.../Classname.class
340 try (Stream<Path> paths = Files.walk(modules)) {
341 Stream<String> files =
342 paths.filter(p -> p.getNameCount() > 2)
343 .map(p -> p.subpath(1, p.getNameCount()))
344 .map(Path::toString);
345 return doModularFileNames(files);
346 }
347 } else {
348 return doClassNames(classes);
349 }
350 }
351
352 /**
353 * Process classes from a particular JDK release, using only information
354 * in this JDK.
355 *
356 * @param release "6", "7", "8", or "9"
357 * @param classes collection of classes to process, may be empty
358 * @return success value
359 */
360 boolean processRelease(String release, Collection<String> classes) throws IOException {
361 options.addAll(List.of("--release", release));
362
363 if (release.equals("9")) {
364 List<String> rootMods = List.of("java.se", "java.se.ee");
365 TraverseProc proc = new TraverseProc(rootMods);
366 JavaCompiler.CompilationTask task =
367 compiler.getTask(null, fm, this,
368 // options
369 List.of("--add-modules", String.join(",", rootMods)),
370 // classes
371 List.of("java.lang.Object"),
372 null);
373 task.setProcessors(List.of(proc));
374 if (!task.call()) {
375 return false;
376 }
377 Map<PackageElement, List<TypeElement>> types = proc.getPublicTypes();
378 options.add("--add-modules");
379 options.add(String.join(",", rootMods));
380 return doClassNames(
381 types.values().stream()
382 .flatMap(List::stream)
383 .map(TypeElement::toString)
467 * @param diagnostic the tool diagnostic to print
468 */
469 @Override
470 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
471 err.println(diagnostic);
472 }
473
474 /**
475 * Parses arguments and performs the requested processing.
476 *
477 * @param argArray command-line arguments
478 * @return true on success, false on error
479 */
480 boolean run(String... argArray) {
481 Queue<String> args = new ArrayDeque<>(Arrays.asList(argArray));
482 LoadMode loadMode = LoadMode.RELEASE;
483 ScanMode scanMode = ScanMode.ARGS;
484 String dir = null;
485 String jar = null;
486 String jdkHome = null;
487 String release = "9";
488 List<String> loadClasses = new ArrayList<>();
489 String csvFile = null;
490
491 try {
492 while (!args.isEmpty()) {
493 String a = args.element();
494 if (a.startsWith("-")) {
495 args.remove();
496 switch (a) {
497 case "--class-path":
498 classPath.clear();
499 Arrays.stream(args.remove().split(File.pathSeparator))
500 .map(File::new)
501 .forEachOrdered(classPath::add);
502 break;
503 case "--for-removal":
504 forRemoval = true;
505 break;
506 case "--full-version":
507 out.println(System.getProperty("java.vm.version"));
|
1 /*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
84 * - add module support: --add-modules, --module-path, module arg
85 * - load deprecation declarations from a designated class library instead
86 * of the JDK
87 * - load deprecation declarations from a module
88 * - scan a module (but a modular jar can be treated just a like an ordinary jar)
89 * - multi-version jar
90 */
91 public class Main implements DiagnosticListener<JavaFileObject> {
92 final PrintStream out;
93 final PrintStream err;
94 final List<File> bootClassPath = new ArrayList<>();
95 final List<File> classPath = new ArrayList<>();
96 final List<File> systemModules = new ArrayList<>();
97 final List<String> options = new ArrayList<>();
98 final List<String> comments = new ArrayList<>();
99
100 // Valid releases need to match what the compiler supports.
101 // Keep these updated manually until there's a compiler API
102 // that allows querying of supported releases.
103 final Set<String> releasesWithoutForRemoval = Set.of("6", "7", "8");
104 final Set<String> releasesWithForRemoval = Set.of("9", "10");
105
106 final Set<String> validReleases;
107 {
108 Set<String> temp = new HashSet<>(releasesWithoutForRemoval);
109 temp.addAll(releasesWithForRemoval);
110 validReleases = Set.of(temp.toArray(new String[0]));
111 }
112
113 boolean verbose = false;
114 boolean forRemoval = false;
115
116 final JavaCompiler compiler;
117 final StandardJavaFileManager fm;
118
119 List<DeprData> deprList; // non-null after successful load phase
120
121 /**
122 * Processes a collection of class names. Names should fully qualified
123 * names in the form "pkg.pkg.pkg.classname".
124 *
336 Path modules = FileSystems.getFileSystem(URI.create("jrt:/"))
337 .getPath("/modules");
338
339 // names are /modules/<modulename>/pkg/.../Classname.class
340 try (Stream<Path> paths = Files.walk(modules)) {
341 Stream<String> files =
342 paths.filter(p -> p.getNameCount() > 2)
343 .map(p -> p.subpath(1, p.getNameCount()))
344 .map(Path::toString);
345 return doModularFileNames(files);
346 }
347 } else {
348 return doClassNames(classes);
349 }
350 }
351
352 /**
353 * Process classes from a particular JDK release, using only information
354 * in this JDK.
355 *
356 * @param release "6", "7", "8", "9", or "10"
357 * @param classes collection of classes to process, may be empty
358 * @return success value
359 */
360 boolean processRelease(String release, Collection<String> classes) throws IOException {
361 options.addAll(List.of("--release", release));
362
363 if (release.equals("9") || release.equals("10")) {
364 List<String> rootMods = List.of("java.se", "java.se.ee");
365 TraverseProc proc = new TraverseProc(rootMods);
366 JavaCompiler.CompilationTask task =
367 compiler.getTask(null, fm, this,
368 // options
369 List.of("--add-modules", String.join(",", rootMods)),
370 // classes
371 List.of("java.lang.Object"),
372 null);
373 task.setProcessors(List.of(proc));
374 if (!task.call()) {
375 return false;
376 }
377 Map<PackageElement, List<TypeElement>> types = proc.getPublicTypes();
378 options.add("--add-modules");
379 options.add(String.join(",", rootMods));
380 return doClassNames(
381 types.values().stream()
382 .flatMap(List::stream)
383 .map(TypeElement::toString)
467 * @param diagnostic the tool diagnostic to print
468 */
469 @Override
470 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
471 err.println(diagnostic);
472 }
473
474 /**
475 * Parses arguments and performs the requested processing.
476 *
477 * @param argArray command-line arguments
478 * @return true on success, false on error
479 */
480 boolean run(String... argArray) {
481 Queue<String> args = new ArrayDeque<>(Arrays.asList(argArray));
482 LoadMode loadMode = LoadMode.RELEASE;
483 ScanMode scanMode = ScanMode.ARGS;
484 String dir = null;
485 String jar = null;
486 String jdkHome = null;
487 String release = "10";
488 List<String> loadClasses = new ArrayList<>();
489 String csvFile = null;
490
491 try {
492 while (!args.isEmpty()) {
493 String a = args.element();
494 if (a.startsWith("-")) {
495 args.remove();
496 switch (a) {
497 case "--class-path":
498 classPath.clear();
499 Arrays.stream(args.remove().split(File.pathSeparator))
500 .map(File::new)
501 .forEachOrdered(classPath::add);
502 break;
503 case "--for-removal":
504 forRemoval = true;
505 break;
506 case "--full-version":
507 out.println(System.getProperty("java.vm.version"));
|