Versions.java revision 3388:b42e6d7b1b99
1/*
2 * Copyright (c) 2014, 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 4981566 5028634 5094412 6304984 7025786 7025789 8001112 8028545 8000961 8030610
27 * @summary Check interpretation of -target and -source options
28 * @modules java.compiler
29 *          jdk.compiler
30 * @run main Versions
31 */
32
33import java.io.*;
34import java.nio.*;
35import java.nio.channels.*;
36
37import javax.tools.JavaCompiler;
38import javax.tools.ToolProvider;
39import javax.tools.JavaFileObject;
40import javax.tools.StandardJavaFileManager;
41import java.util.List;
42import java.util.ArrayList;
43import java.util.Arrays;
44
45
46public class Versions {
47
48    protected JavaCompiler javacompiler;
49    protected int failedCases;
50
51    public Versions() throws IOException {
52        javacompiler = ToolProvider.getSystemJavaCompiler();
53        genSourceFiles();
54        failedCases = 0;
55    }
56
57    public static void main(String... args) throws IOException {
58        Versions versions = new Versions();
59        versions.run();
60    }
61
62    void run() {
63
64        String TC = "";
65        System.out.println("Version.java: Starting");
66
67        check("53.0");
68        check("53.0", "-source 1.6");
69        check("53.0", "-source 1.7");
70        check("53.0", "-source 1.8");
71        check("53.0", "-source 1.9");
72
73        check_source_target("50.0", "6", "6");
74        check_source_target("51.0", "6", "7");
75        check_source_target("51.0", "7", "7");
76        check_source_target("52.0", "6", "8");
77        check_source_target("52.0", "7", "8");
78        check_source_target("52.0", "8", "8");
79        check_source_target("53.0", "6", "9");
80        check_source_target("53.0", "7", "9");
81        check_source_target("53.0", "8", "9");
82        check_source_target("53.0", "9", "9");
83
84        checksrc16("-source 1.6");
85        checksrc16("-source 6");
86        checksrc16("-source 1.6", "-target 1.6");
87        checksrc16("-source 6", "-target 6");
88        checksrc17("-source 1.7");
89        checksrc17("-source 7");
90        checksrc17("-source 1.7", "-target 1.7");
91        checksrc17("-source 7", "-target 7");
92        checksrc18("-source 1.8");
93        checksrc18("-source 8");
94        checksrc18("-source 1.8", "-target 1.8");
95        checksrc18("-source 8", "-target 8");
96        checksrc19();
97        checksrc19("-source 1.9");
98        checksrc19("-source 9");
99        checksrc19("-source 1.9", "-target 1.9");
100        checksrc19("-source 9", "-target 9");
101        checksrc19("-target 1.9");
102        checksrc19("-target 9");
103
104        fail("-source 7", "-target 1.6", "Base.java");
105        fail("-source 8", "-target 1.6", "Base.java");
106        fail("-source 8", "-target 1.7", "Base.java");
107        fail("-source 9", "-target 1.7", "Base.java");
108        fail("-source 9", "-target 1.8", "Base.java");
109
110        fail("-source 1.5", "-target 1.5", "Base.java");
111        fail("-source 1.4", "-target 1.4", "Base.java");
112        fail("-source 1.3", "-target 1.3", "Base.java");
113        fail("-source 1.2", "-target 1.2", "Base.java");
114
115        if (failedCases > 0) {
116            System.err.println("failedCases = " + String.valueOf(failedCases));
117            throw new Error("Test failed");
118        }
119
120    }
121
122    protected void printargs(String fname,String... args) {
123        System.out.printf("test: %s", fname);
124        for (String onearg : args) {
125            System.out.printf(" %s", onearg);
126        }
127        System.out.printf("\n", fname);
128    }
129
130    protected void check_source_target(String... args) {
131        printargs("check_source_target", args);
132        check_target(args[0], args[1], args[2]);
133        check_target(args[0], "1." + args[1], args[2]);
134    }
135
136    protected void check_target(String... args) {
137        check(args[0], "-source " + args[1], "-target " + args[2]);
138        check(args[0], "-source " + args[1], "-target 1." + args[2]);
139    }
140
141    protected void check(String major, String... args) {
142        printargs("check", args);
143        List<String> jcargs = new ArrayList<String>();
144        jcargs.add("-Xlint:-options");
145
146        // add in args conforming to List requrements of JavaCompiler
147        for (String onearg : args) {
148            String[] fields = onearg.split(" ");
149            for (String onefield : fields) {
150                jcargs.add(onefield);
151            }
152        }
153
154        boolean creturn = compile("Base.java", jcargs);
155        if (!creturn) {
156            // compilation errors note and return.. assume no class file
157            System.err.println("check: Compilation Failed");
158            System.err.println("\t classVersion:\t" + major);
159            System.err.println("\t arguments:\t" + jcargs);
160            failedCases++;
161
162        } else if (!checkClassFileVersion("Base.class", major)) {
163            failedCases++;
164        }
165    }
166
167    protected void checksrc16(String... args) {
168        printargs("checksrc16", args);
169        int asize = args.length;
170        String[] newargs = new String[asize + 1];
171        System.arraycopy(args, 0, newargs, 0, asize);
172        newargs[asize] = "Base.java";
173        pass(newargs);
174        newargs[asize] = "New17.java";
175        fail(newargs);
176    }
177
178    protected void checksrc17(String... args) {
179        printargs("checksrc17", args);
180        int asize = args.length;
181        String[] newargs = new String[asize+1];
182        System.arraycopy(args, 0, newargs,0 , asize);
183        newargs[asize] = "New17.java";
184        pass(newargs);
185        newargs[asize] = "New18.java";
186        fail(newargs);
187    }
188
189    protected void checksrc18(String... args) {
190        printargs("checksrc18", args);
191        int asize = args.length;
192        String[] newargs = new String[asize+1];
193        System.arraycopy(args, 0, newargs,0 , asize);
194        newargs[asize] = "New17.java";
195        pass(newargs);
196        newargs[asize] = "New18.java";
197        pass(newargs);
198    }
199
200    protected void checksrc19(String... args) {
201        printargs("checksrc19", args);
202        checksrc18(args);
203    }
204
205    protected void pass(String... args) {
206        printargs("pass", args);
207
208        List<String> jcargs = new ArrayList<String>();
209        jcargs.add("-Xlint:-options");
210
211        // add in args conforming to List requrements of JavaCompiler
212        for (String onearg : args) {
213            String[] fields = onearg.split(" ");
214            for (String onefield : fields) {
215                jcargs.add(onefield);
216            }
217        }
218
219        // empty list is error
220        if (jcargs.isEmpty()) {
221            System.err.println("error: test error in pass() - No arguments");
222            System.err.println("\t arguments:\t" + jcargs);
223            failedCases++;
224            return;
225        }
226
227        // the last argument is the filename *.java
228        String filename = jcargs.get(jcargs.size() - 1);
229        jcargs.remove(jcargs.size() - 1);
230
231        boolean creturn = compile(filename, jcargs);
232        // expect a compilation failure, failure if otherwise
233        if (!creturn) {
234            System.err.println("pass: Compilation erroneously failed");
235            System.err.println("\t arguments:\t" + jcargs);
236            System.err.println("\t file     :\t" + filename);
237            failedCases++;
238
239        }
240
241    }
242
243    protected void fail(String... args) {
244        printargs("fail", args);
245
246        List<String> jcargs = new ArrayList<String>();
247        jcargs.add("-Xlint:-options");
248
249        // add in args conforming to List requrements of JavaCompiler
250        for (String onearg : args) {
251            String[] fields = onearg.split(" ");
252            for (String onefield : fields) {
253                jcargs.add(onefield);
254            }
255        }
256
257        // empty list is error
258        if (jcargs.isEmpty()) {
259            System.err.println("error: test error in fail()- No arguments");
260            System.err.println("\t arguments:\t" + jcargs);
261            failedCases++;
262            return;
263        }
264
265        // the last argument is the filename *.java
266        String filename = jcargs.get(jcargs.size() - 1);
267        jcargs.remove(jcargs.size() - 1);
268
269        boolean creturn = compile(filename, jcargs);
270        // expect a compilation failure, failure if otherwise
271        if (creturn) {
272            System.err.println("fail: Compilation erroneously succeeded");
273            System.err.println("\t arguments:\t" + jcargs);
274            System.err.println("\t file     :\t" + filename);
275            failedCases++;
276        }
277    }
278
279    protected boolean compile(String sourceFile, List<String>options) {
280        JavaCompiler.CompilationTask jctask;
281        try (StandardJavaFileManager fm = javacompiler.getStandardFileManager(null, null, null)) {
282            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(sourceFile);
283
284            jctask = javacompiler.getTask(
285                null,    // Writer
286                fm,      // JavaFileManager
287                null,    // DiagnosticListener
288                options, // Iterable<String>
289                null,    // Iterable<String> classes
290                files);  // Iterable<? extends JavaFileObject>
291
292            try {
293                return jctask.call();
294            } catch (IllegalStateException e) {
295                System.err.println(e);
296                return false;
297            }
298        } catch (IOException e) {
299            throw new Error(e);
300        }
301    }
302
303    protected void writeSourceFile(String fileName, String body) throws IOException{
304        try (Writer fw = new FileWriter(fileName)) {
305            fw.write(body);
306        }
307    }
308
309    protected void genSourceFiles() throws IOException{
310        /* Create a file that executes with all supported versions. */
311        writeSourceFile("Base.java","public class Base { }\n");
312
313        /*
314         * Create a file with a new feature in 1.7, not in 1.6 : "<>"
315         */
316        writeSourceFile("New17.java",
317            "import java.util.List;\n" +
318            "import java.util.ArrayList;\n" +
319            "class New17 { List<String> s = new ArrayList<>(); }\n"
320        );
321
322        /*
323         * Create a file with a new feature in 1.8, not in 1.7 : lambda
324         */
325        writeSourceFile("New18.java",
326            "public class New18 { \n" +
327            "    void m() { \n" +
328            "    new Thread(() -> { }); \n" +
329            "    } \n" +
330            "} \n"
331        );
332
333    }
334
335    protected boolean checkClassFileVersion
336        (String filename,String classVersionNumber) {
337        ByteBuffer bb = ByteBuffer.allocate(1024);
338        try (FileChannel fc = new FileInputStream(filename).getChannel()) {
339            bb.clear();
340            if (fc.read(bb) < 0)
341                throw new IOException("Could not read from file : " + filename);
342            bb.flip();
343            int minor = bb.getShort(4);
344            int major = bb.getShort(6);
345            String fileVersion = major + "." + minor;
346            if (fileVersion.equals(classVersionNumber)) {
347                return true;
348            } else {
349                System.err.println("checkClassFileVersion : Failed");
350                System.err.println("\tclassfile version mismatch");
351                System.err.println("\texpected : " + classVersionNumber);
352                System.err.println("\tfound    : " + fileVersion);
353                return false;
354            }
355        }
356        catch (IOException e) {
357            System.err.println("checkClassFileVersion : Failed");
358            System.err.println("\terror :\t" + e.getMessage());
359            System.err.println("\tfile:\tfilename");
360        }
361        return false;
362    }
363}
364
365