Path: blob/master/test/langtools/tools/lib/toolbox/JavapTask.java
41149 views
/*1* Copyright (c) 2013, 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*/2223package toolbox;2425import java.util.ArrayList;26import java.util.Arrays;27import java.util.HashMap;28import java.util.List;29import java.util.Map;3031/**32* A task to configure and run the disassembler tool, javap.33*/34public class JavapTask extends AbstractTask<JavapTask> {35private String classpath;36private List<String> options;37private List<String> classes;3839/**40* Create a task to execute {@code javap} using {@code CMDLINE} mode.41* @param toolBox the {@code ToolBox} to use42*/43public JavapTask(ToolBox toolBox) {44super(toolBox, Task.Mode.CMDLINE);45}4647/**48* Sets the classpath.49* @param classpath the classpath50* @return this task object51*/52public JavapTask classpath(String classpath) {53this.classpath = classpath;54return this;55}5657/**58* Sets the options.59* @param options the options60* @return this task object61*/62public JavapTask options(String... options) {63this.options = Arrays.asList(options);64return this;65}6667/**68* Sets the classes to be analyzed.69* @param classes the classes70* @return this task object71*/72public JavapTask classes(String... classes) {73this.classes = Arrays.asList(classes);74return this;75}7677/**78* {@inheritDoc}79* @return the name "javap"80*/81@Override82public String name() {83return "javap";84}8586/**87* Calls the javap tool with the arguments as currently configured.88* @return a Result object indicating the outcome of the task89* and the content of any output written to stdout, stderr, or the90* main stream.91* @throws TaskError if the outcome of the task is not as expected.92*/93@Override94public Task.Result run() {95List<String> args = new ArrayList<>();96if (options != null)97args.addAll(options);98if (classpath != null) {99args.add("-classpath");100args.add(classpath);101}102if (classes != null)103args.addAll(classes);104105AbstractTask.WriterOutput direct = new AbstractTask.WriterOutput();106// These are to catch output to System.out and System.err,107// in case these are used instead of the primary streams108AbstractTask.StreamOutput sysOut = new AbstractTask.StreamOutput(System.out, System::setOut);109AbstractTask.StreamOutput sysErr = new AbstractTask.StreamOutput(System.err, System::setErr);110111int rc;112Map<Task.OutputKind, String> outputMap = new HashMap<>();113try {114rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), direct.pw);115} finally {116outputMap.put(Task.OutputKind.STDOUT, sysOut.close());117outputMap.put(Task.OutputKind.STDERR, sysErr.close());118outputMap.put(Task.OutputKind.DIRECT, direct.close());119}120return checkExit(new Task.Result(toolBox, this, rc, outputMap));121}122}123124125