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