Main.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6627364 6627366
27 * @summary Synthesize important classes if they are missing from the (boot)classpath
28 * @modules jdk.compiler
29 */
30
31import java.io.*;
32import java.util.*;
33
34public class Main
35{
36    File testSrc = new File(System.getProperty("test.src"));
37
38    public static void main(String[] args) throws Exception {
39        new Main().run();
40    }
41
42    public void run() throws Exception {
43
44        // compile with standard bootclasspath
45        compile(true, "Test.java");
46
47        // compile with various missing system classes
48
49        List<String> base_files = Arrays.asList(
50            "Boolean.java",
51            "Byte.java",
52            "Character.java",
53            "Integer.java",
54            "Long.java",
55            "Number.java",
56            "Object.java",
57            "Short.java",
58            "Void.java"
59        );
60
61        List<String> extra_files = Arrays.asList(
62            "Double.java",
63            "Float.java",
64            "Cloneable.java",
65            "Serializable.java"
66        );
67
68        List<String> files = new ArrayList<String>();
69        files.addAll(base_files);
70        files.add("Test.java");
71
72        compile(false, files);
73
74        for (String f: extra_files) {
75            files = new ArrayList<String>();
76            files.addAll(base_files);
77            files.addAll(extra_files);
78            files.remove(f);
79            files.add("Test.java");
80            compile(false, files);
81        }
82
83        if (errors > 0)
84            throw new Exception(errors + " errors occurred");
85    }
86
87    void compile(boolean stdBootClassPath, String... files) {
88        compile(stdBootClassPath, Arrays.asList(files));
89    }
90
91    void compile(boolean stdBootClassPath, List<String> files) {
92        File empty = new File("empty");
93        empty.mkdirs();
94
95        List<String> args = new ArrayList<String>();
96        args.add("-classpath");
97        args.add("empty");
98
99        if (stdBootClassPath) {
100            args.add("-Xmodule:java.base");
101        } else {
102            args.add("-system");
103            args.add("none");
104            files.add("module-info.java");
105        }
106
107
108        args.add("-d");
109        args.add(".");
110
111        // files to compile are in a separate directory from test to avoid
112        // confusing jtreg
113        File src = new File(testSrc, "src");
114        for (String f: files)
115            args.add(new File(src, f).getPath());
116
117        System.out.println("Compile: " + args);
118        StringWriter out = new StringWriter();
119        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]),
120                                                  new PrintWriter(out));
121        System.out.println(out.toString());
122        System.out.println("result: " + rc);
123        System.out.println();
124
125        if (rc != 0)
126            errors++;
127    }
128
129    private int errors;
130}
131
132
133