1 /*
   2  * Copyright (c) 2012, 2018, 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 7157626 8001112 8188870 8173382 8193290 8205619
  27  * @summary Test major version for all legal combinations for -source and -target
  28  * @author sgoel
  29  *
  30  * @modules jdk.compiler
  31  */
  32 
  33 import java.io.*;
  34 import java.nio.*;
  35 import java.util.*;
  36 import java.util.regex.*;
  37 
  38 public class ClassVersionChecker {
  39     private static enum Version {
  40         SIX("6", 50),
  41         SEVEN("7", 51),
  42         EIGHT("8", 52),
  43         NINE("9", 53),
  44         TEN("10", 54),
  45         ELEVEN("11", 55),
  46         TWELVE("12", 56);
  47 
  48         private Version(String release, int classFileVer) {
  49             this.release = release;
  50             this.classFileVer = classFileVer;
  51         }
  52         private final String release;
  53         private final int classFileVer;
  54 
  55         String release() {return release;}
  56         int classFileVer() {return classFileVer;}
  57     }
  58 
  59     static final Version CURRENT;
  60     static {
  61         Version[] versions = Version.values();
  62         int index = versions.length;
  63         CURRENT = versions[index - 1];
  64     }
  65 
  66     int errors;
  67 
  68     File javaFile = null;
  69 
  70     public static void main(String[] args) throws Throwable {
  71         new ClassVersionChecker().run();
  72     }
  73 
  74     void run() throws Exception {
  75         writeTestFile();
  76         /*
  77          * Rules applicable for -source and -target combinations:
  78          * 1. If both empty, version num is for the current release
  79          * 2. If source is not empty and target is empty, version is based on the current release
  80          * 3. If both non-empty, version is based on target
  81          */
  82         test("", "", CURRENT.classFileVer());
  83         for (Version source : Version.values()) {
  84             test(source.release(), "", CURRENT.classFileVer()); // no target
  85             for (Version target : Version.values()) {
  86                 if (target.compareTo(source) < 0)
  87                     continue; // Target < source not a valid set of arguments
  88                 else {
  89                     logMsg("Running for src = " + source + " target = "+ target +
  90                            " expected = " + target.classFileVer());
  91                     test(source.release(), target.release(), target.classFileVer());
  92                 }
  93             }
  94         }
  95 
  96         if (errors > 0)
  97             throw new Exception(errors + " errors found");
  98     }
  99 
 100     void test (String i, String j, int expected) {
 101         File classFile = compileTestFile(i, j, javaFile);
 102         short majorVer = getMajorVersion(classFile);
 103         checkVersion(majorVer, expected);
 104     }
 105 
 106     void writeTestFile() throws IOException {
 107         javaFile = new File("Test.java");
 108         try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
 109             out.println("class Test { ");
 110             out.println("  public void foo() { }");
 111             out.println("}");
 112         } catch (IOException ioe) {
 113             error("IOException while creating Test.java" + ioe);
 114         }
 115     }
 116 
 117     File compileTestFile(String i , String j, File f) {
 118         int rc = -1;
 119         // Src and target are empty
 120         if (i.isEmpty() && j.isEmpty() ) {
 121             rc = compile("-g", f.getPath());
 122         } else if( j.isEmpty()) {  // target is empty
 123             rc = compile("-source", i, "-g", f.getPath());
 124         } else {
 125             rc = compile("-source", i, "-target", j, "-g", f.getPath());
 126         }
 127         if (rc != 0)
 128             throw new Error("compilation failed. rc=" + rc);
 129         String path = f.getPath();
 130         return new File(path.substring(0, path.length() - 5) + ".class");
 131     }
 132 
 133     int compile(String... args) {
 134         return com.sun.tools.javac.Main.compile(args);
 135     }
 136 
 137     void logMsg (String str) {
 138         System.out.println(str);
 139     }
 140 
 141     short getMajorVersion(File f) {
 142         List<String> args = new ArrayList<String>();
 143         short majorVer = 0;
 144         try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
 145             in.readInt();
 146             in.readShort();
 147             majorVer = in.readShort();
 148             System.out.println("major version:" +  majorVer);
 149         } catch (IOException e) {
 150             error("IOException while reading Test.class" + e);
 151         }
 152         return majorVer;
 153     }
 154 
 155     void checkVersion(short majorVer, int expected) {
 156         if (majorVer != expected ) {
 157             error("versions did not match, Expected: " + expected + "Got: " + majorVer);
 158         }
 159     }
 160 
 161     void error(String msg) {
 162        System.out.println("error: " + msg);
 163        errors++;
 164     }
 165 }