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