langtools/test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 18388 7d67f9206d8f
child 30846 2b3f379840f0
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15716
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 18388
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
15716
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     4
 *
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     8
 *
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    13
 * accompanied this code).
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    14
 *
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    18
 *
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    21
 * questions.
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    22
 */
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    23
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    24
/*
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    25
 * @test
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    26
 * @bug 8006334
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    27
 * @summary javap: JavapTask constructor breaks with null pointer exception if
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    28
 * parameter options is null
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 18388
diff changeset
    29
 * @modules jdk.compiler
15716
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    30
 */
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    31
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    32
import java.io.File;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    33
import java.util.Arrays;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    34
import java.io.PrintWriter;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    35
import java.io.StringWriter;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    36
import java.util.List;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    37
import java.util.Locale;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    38
import javax.tools.Diagnostic;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    39
import javax.tools.DiagnosticCollector;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    40
import javax.tools.JavaFileManager;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    41
import javax.tools.JavaFileObject;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    42
import com.sun.tools.javap.JavapFileManager;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    43
import com.sun.tools.javap.JavapTask;
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    44
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    45
public class JavapTaskCtorFailWithNPE {
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    46
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    47
    //we will also check the output just to confirm that we get the expected one
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    48
    private static final String expOutput =
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    49
        "Compiled from \"JavapTaskCtorFailWithNPE.java\"\n" +
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    50
        "public class JavapTaskCtorFailWithNPE {\n" +
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    51
        "  public JavapTaskCtorFailWithNPE();\n" +
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    52
        "  public static void main(java.lang.String[]);\n" +
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    53
        "}\n";
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    54
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    55
    public static void main(String[] args) {
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    56
        new JavapTaskCtorFailWithNPE().run();
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    57
    }
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    58
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    59
    private void run() {
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    60
        File classToCheck = new File(System.getProperty("test.classes"),
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    61
            getClass().getSimpleName() + ".class");
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    62
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    63
        DiagnosticCollector<JavaFileObject> dc =
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    64
                new DiagnosticCollector<JavaFileObject>();
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    65
        StringWriter sw = new StringWriter();
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    66
        PrintWriter pw = new PrintWriter(sw);
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    67
        JavaFileManager fm = JavapFileManager.create(dc, pw);
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    68
        JavapTask t = new JavapTask(pw, fm, dc, null,
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    69
                Arrays.asList(classToCheck.getPath()));
18388
7d67f9206d8f 8007907: javap, method com.sun.tools.javap.Main.run returns 0 even in case of class not found error
vromero
parents: 15716
diff changeset
    70
        if (t.run() != 0)
15716
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    71
            throw new Error("javap failed unexpectedly");
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    72
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    73
        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    74
        for (Diagnostic<? extends JavaFileObject> d: diags) {
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    75
            if (d.getKind() == Diagnostic.Kind.ERROR)
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    76
                throw new AssertionError(d.getMessage(Locale.ENGLISH));
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    77
        }
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    78
        String lineSep = System.getProperty("line.separator");
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    79
        String out = sw.toString().replace(lineSep, "\n");
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    80
        if (!out.equals(expOutput)) {
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    81
            throw new AssertionError("The output is not equal to the one expected");
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    82
        }
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    83
    }
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    84
d1f59adb0d83 8006334: javap, JavapTask constructor breaks with null pointer exception if parameter options is null
vromero
parents:
diff changeset
    85
}