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