langtools/test/tools/javac/api/ToolProvider/ToolProviderTest2.java
author jjg
Thu, 26 Aug 2010 15:17:17 -0700
changeset 6577 96c6451389fc
child 22149 c7e024d637bf
permissions -rw-r--r--
6604599: ToolProvider should be less compiler-specific Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6577
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     1
/*
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     4
 *
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     8
 *
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    13
 * accompanied this code).
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    14
 *
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    18
 *
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    21
 * questions.
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    22
 */
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    23
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    24
/*
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    25
 * @test
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    26
 * @bug 6604599
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    27
 * @summary ToolProvider should be less compiler-specific
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    28
 */
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    29
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    30
import java.io.*;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    31
import javax.tools.*;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    32
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    33
// control for ToolProviderTest1 -- verify that using ToolProvider to
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    34
// access the compiler does trigger loading com.sun.tools.javac.*
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    35
public class ToolProviderTest2 {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    36
    public static void main(String... args) throws Exception {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    37
        if (args.length > 0) {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    38
            System.err.println(ToolProvider.getSystemJavaCompiler());
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    39
            return;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    40
        }
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    41
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    42
        new ToolProviderTest2().run();
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    43
    }
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    44
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    45
    void run() throws Exception {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    46
        File javaHome = new File(System.getProperty("java.home"));
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    47
        if (javaHome.getName().equals("jre"))
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    48
            javaHome = javaHome.getParentFile();
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    49
        File javaExe = new File(new File(javaHome, "bin"), "java");
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    50
        String classpath = System.getProperty("java.class.path");
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    51
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    52
        String[] cmd = {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    53
            javaExe.getPath(),
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    54
            "-verbose:class",
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    55
            "-classpath", classpath,
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    56
            ToolProviderTest2.class.getName(),
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    57
            "javax.tools.ToolProvider"
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    58
        };
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    59
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    60
        ProcessBuilder pb = new ProcessBuilder(cmd).redirectErrorStream(true);
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    61
        Process p = pb.start();
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    62
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    63
        String line;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    64
        boolean found = false;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    65
        while ((line = r.readLine()) != null) {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    66
            System.err.println(line);
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    67
            if (line.contains("com.sun.tools.javac."))
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    68
                found = true;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    69
        }
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    70
        int rc = p.waitFor();
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    71
        if (rc != 0)
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    72
            error("Unexpected exit code: " + rc);
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    73
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    74
        if (!found)
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    75
            System.err.println("expected class name not found");
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    76
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    77
        if (errors > 0)
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    78
            throw new Exception(errors + " errors occurred");
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    79
    }
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    80
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    81
    void error(String msg) {
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    82
        System.err.println(msg);
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    83
        errors++;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    84
    }
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    85
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    86
    int errors;
96c6451389fc 6604599: ToolProvider should be less compiler-specific
jjg
parents:
diff changeset
    87
}