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