Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java
32285 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @bug 800633426* @summary javap: JavapTask constructor breaks with null pointer exception if27* parameter options is null28*/2930import java.io.File;31import java.util.Arrays;32import java.io.PrintWriter;33import java.io.StringWriter;34import java.util.List;35import java.util.Locale;36import javax.tools.Diagnostic;37import javax.tools.DiagnosticCollector;38import javax.tools.JavaFileManager;39import javax.tools.JavaFileObject;40import com.sun.tools.javap.JavapFileManager;41import com.sun.tools.javap.JavapTask;4243public class JavapTaskCtorFailWithNPE {4445//we will also check the output just to confirm that we get the expected one46private static final String expOutput =47"Compiled from \"JavapTaskCtorFailWithNPE.java\"\n" +48"public class JavapTaskCtorFailWithNPE {\n" +49" public JavapTaskCtorFailWithNPE();\n" +50" public static void main(java.lang.String[]);\n" +51"}\n";5253public static void main(String[] args) {54new JavapTaskCtorFailWithNPE().run();55}5657private void run() {58File classToCheck = new File(System.getProperty("test.classes"),59getClass().getSimpleName() + ".class");6061DiagnosticCollector<JavaFileObject> dc =62new DiagnosticCollector<JavaFileObject>();63StringWriter sw = new StringWriter();64PrintWriter pw = new PrintWriter(sw);65JavaFileManager fm = JavapFileManager.create(dc, pw);66JavapTask t = new JavapTask(pw, fm, dc, null,67Arrays.asList(classToCheck.getPath()));68if (t.run() != 0)69throw new Error("javap failed unexpectedly");7071List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();72for (Diagnostic<? extends JavaFileObject> d: diags) {73if (d.getKind() == Diagnostic.Kind.ERROR)74throw new AssertionError(d.getMessage(Locale.ENGLISH));75}76String lineSep = System.getProperty("line.separator");77String out = sw.toString().replace(lineSep, "\n");78if (!out.equals(expOutput)) {79throw new AssertionError("The output is not equal to the one expected");80}81}8283}848586