langtools/test/tools/javac/6668794/badClass/Test.java
author mcimadamore
Fri, 13 Feb 2009 11:57:33 +0000
changeset 2085 4792e12a8ca2
parent 169 ff76730e430e
child 2212 1d3dc0e0ba0c
permissions -rw-r--r--
6769027: Source line should be displayed immediately after the first diagnostic line Summary: Added support for customizing diagnostic output via API/command line flags Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     1
/*
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     4
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    10
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    15
 * accompanied this code).
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    16
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    20
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    23
 * have any questions.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    24
 */
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    25
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    26
/*
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    27
 * @test
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    28
 * @bug 6668794 6668796
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    29
 * @summary javac puts localized text in raw diagnostics
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    30
 *      bad diagnostic "bad class file" given for source files
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    31
 */
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    32
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    33
import java.io.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    34
import java.util.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    35
import javax.tools.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    36
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    37
public class Test {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    39
        new Test().run();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    40
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    41
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    42
    void run() throws Exception {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    43
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    44
        // compile q.A then move it to p.A
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    45
        compile("A.java");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    46
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    47
        File p = new File("p");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    48
        p.mkdirs();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    49
        new File("q/A.class").renameTo(new File("p/A.class"));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    50
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    51
        // compile B against p.A
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    52
        String[] out = compile("B.java");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    53
        if (out.length == 0)
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    54
            throw new Error("no diagnostics generated");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    55
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    56
        String expected = "B.java:6:6: compiler.err.cant.access: p.A, " +
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 169
diff changeset
    57
            "(compiler.misc.bad.class.file.header: A.class, " +
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 169
diff changeset
    58
            "(compiler.misc.class.file.wrong.class: q.A))";
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    59
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    60
        if (!out[0].equals(expected)) {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    61
            System.err.println("expected: " + expected);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    62
            System.err.println("   found: " + out[0]);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    63
            throw new Error("test failed");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    64
        }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    65
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    66
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    67
    String[] compile(String file) {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    68
        String[] options = {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    69
            "-XDrawDiagnostics",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    70
            "-d", ".",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    71
            "-classpath", ".",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    72
            new File(testSrc, file).getPath()
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    73
        };
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    74
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    75
        System.err.println("compile: " + Arrays.asList(options));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    76
        StringWriter sw = new StringWriter();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    77
        PrintWriter out = new PrintWriter(sw);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    78
        int rc = com.sun.tools.javac.Main.compile(options, out);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    79
        out.close();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    80
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    81
        String outText = sw.toString();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    82
        System.err.println(outText);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    83
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    84
        return sw.toString().split("[\\r\\n]+");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    85
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    86
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    87
    File testSrc = new File(System.getProperty("test.src", "."));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    88
}