Path: blob/master/test/langtools/tools/jdeps/lib/JdepsUtil.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.tools.jdeps.Analyzer;24import com.sun.tools.jdeps.DepsAnalyzer;25import com.sun.tools.jdeps.InverseDepsAnalyzer;26import com.sun.tools.jdeps.JdepsConfiguration;27import com.sun.tools.jdeps.JdepsFilter;28import com.sun.tools.jdeps.JdepsWriter;29import com.sun.tools.jdeps.ModuleAnalyzer;3031import java.io.Closeable;32import java.io.File;33import java.io.IOException;34import java.io.PrintStream;35import java.io.PrintWriter;36import java.io.StringWriter;37import java.io.UncheckedIOException;38import java.lang.module.ModuleDescriptor;39import java.nio.file.Files;40import java.nio.file.Path;41import java.util.HashSet;42import java.util.Set;43import java.util.jar.JarEntry;44import java.util.jar.JarOutputStream;45import java.util.regex.Pattern;46import java.util.stream.Stream;4748/**49* Utilities to run jdeps command50*/51public final class JdepsUtil {52public static Command newCommand(String cmd) {53return new Command(cmd);54}5556public static class Command implements Closeable {5758final StringWriter sw = new StringWriter();59final PrintWriter pw = new PrintWriter(sw);60final JdepsFilter.Builder filter = new JdepsFilter.Builder().filter(true, true);61final JdepsConfiguration.Builder builder = new JdepsConfiguration.Builder();62final Set<String> requires = new HashSet<>();6364JdepsConfiguration configuration;65Analyzer.Type verbose = Analyzer.Type.PACKAGE;66boolean apiOnly = false;6768public Command(String cmd) {69System.err.println("============ ");70System.err.println(cmd);71}7273public Command verbose(String verbose) {74switch (verbose) {75case "-verbose":76this.verbose = Analyzer.Type.VERBOSE;77filter.filter(false, false);78break;79case "-verbose:package":80this.verbose = Analyzer.Type.PACKAGE;81break;82case "-verbose:class":83this.verbose = Analyzer.Type.CLASS;84break;85case "-summary":86this.verbose = Analyzer.Type.SUMMARY;87break;88default:89throw new IllegalArgumentException(verbose);90}91return this;92}9394public Command filter(String value) {95switch (value) {96case "-filter:package":97filter.filter(true, false);98break;99case "-filter:archive":100case "-filter:module":101filter.filter(false, true);102break;103default:104throw new IllegalArgumentException(value);105}106return this;107}108109public Command addClassPath(String classpath) {110builder.addClassPath(classpath);111return this;112}113114public Command addRoot(Path path) {115builder.addRoot(path);116return this;117}118119public Command appModulePath(String modulePath) {120builder.appModulePath(modulePath);121return this;122}123124public Command addmods(Set<String> mods) {125builder.addmods(mods);126return this;127}128129public Command requires(Set<String> mods) {130requires.addAll(mods);131return this;132}133134public Command matchPackages(Set<String> pkgs) {135filter.packages(pkgs);136return this;137}138139public Command regex(String regex) {140filter.regex(Pattern.compile(regex));141return this;142}143144public Command include(String regex) {145filter.includePattern(Pattern.compile(regex));146return this;147}148149public Command apiOnly() {150this.apiOnly = true;151return this;152}153154public JdepsConfiguration configuration() throws IOException {155if (configuration == null) {156this.configuration = builder.build();157requires.forEach(name -> {158ModuleDescriptor md = configuration.findModuleDescriptor(name).get();159filter.requires(name, md.packages());160});161}162return configuration;163}164165private JdepsWriter writer() {166return JdepsWriter.newSimpleWriter(pw, verbose);167}168169public DepsAnalyzer getDepsAnalyzer() throws IOException {170return new DepsAnalyzer(configuration(), filter.build(), writer(),171verbose, apiOnly);172}173174public ModuleAnalyzer getModuleAnalyzer(Set<String> mods) throws IOException {175// if --check is set, add to the root set and all modules are observable176addmods(mods);177builder.addmods(Set.of("ALL-SYSTEM", "ALL-MODULE-PATH"));178return new ModuleAnalyzer(configuration(), pw, mods);179}180181public InverseDepsAnalyzer getInverseDepsAnalyzer() throws IOException {182return new InverseDepsAnalyzer(configuration(), filter.build(), writer(),183verbose, false);184}185186public void dumpOutput(PrintStream out) {187out.println(sw.toString());188}189190@Override191public void close() throws IOException {192configuration.close();193}194}195196/**197* Create a jar file using the list of files provided.198*/199public static void createJar(Path jarfile, Path root, Stream<Path> files)200throws IOException {201Path dir = jarfile.getParent();202if (dir != null && Files.notExists(dir)) {203Files.createDirectories(dir);204}205try (JarOutputStream target = new JarOutputStream(206Files.newOutputStream(jarfile))) {207files.forEach(file -> add(root.relativize(file), file, target));208}209}210211private static void add(Path path, Path source, JarOutputStream target) {212try {213String name = path.toString().replace(File.separatorChar, '/');214JarEntry entry = new JarEntry(name);215entry.setTime(source.toFile().lastModified());216target.putNextEntry(entry);217Files.copy(source, target);218target.closeEntry();219} catch (IOException e) {220throw new UncheckedIOException(e);221}222}223}224225226