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