1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/classfiles/ClassVersionChecker.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7157626 8001112 1.30 + * @summary Test major version for all legal combinations for -source and -target 1.31 + * @author sgoel 1.32 + * 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.nio.*; 1.37 +import java.util.*; 1.38 +import java.util.regex.*; 1.39 + 1.40 +public class ClassVersionChecker { 1.41 + 1.42 + int errors; 1.43 + String[] jdk = {"","1.2","1.3","1.4","1.5","1.6","1.7","1.8"}; 1.44 + File javaFile = null; 1.45 + 1.46 + public static void main(String[] args) throws Throwable { 1.47 + new ClassVersionChecker().run(); 1.48 + } 1.49 + 1.50 + void run() throws Exception { 1.51 + writeTestFile(); 1.52 + /* Rules applicable for -source and -target combinations 1.53 + * 1. If both empty, version num is for 1.7 1.54 + * 2. If source is not empty and target is empty, version is based on source 1.55 + * 3. If both non-empty, version is based on target 1.56 + */ 1.57 + 1.58 + /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...) 1.59 + * ver[0][0] => no -source or -target was given 1.60 + * -1 => invalid combinations 1.61 + */ 1.62 + int[][] ver = 1.63 + {{52, -1, -1, -1, -1, -1, -1, -1}, 1.64 + {48, 46, 47, 48, 49, 50, 51, 52}, 1.65 + {48, 46, 47, 48, 49, 50, 51, 52}, 1.66 + {48, -1, -1, 48, 49, 50, 51, 52}, 1.67 + {52, -1, -1, -1, 49, 50, 51, 52}, 1.68 + {52, -1, -1, -1, -1, 50, 51, 52}, 1.69 + {52, -1, -1, -1, -1, -1, 51, 52}, 1.70 + {52, -1, -1, -1, -1, -1, -1, 52}}; 1.71 + 1.72 + // Loop to run all possible combinations of source/target values 1.73 + for (int i = 0; i< ver.length; i++) { 1.74 + for (int j = 0 ; j< ver[i].length; j++) { 1.75 + if(ver[i][j] != -1) { 1.76 + logMsg("Index values for i = " + i + " j = " + j); 1.77 + logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]); 1.78 + test(i,j, ver[i][j]); 1.79 + } 1.80 + } 1.81 + } 1.82 + 1.83 + if (errors > 0) 1.84 + throw new Exception(errors + " errors found"); 1.85 + } 1.86 + 1.87 + void test (int i, int j, int expected) { 1.88 + File classFile = compileTestFile(i, j, javaFile); 1.89 + short majorVer = getMajorVersion(classFile); 1.90 + checkVersion(majorVer, expected); 1.91 + } 1.92 + 1.93 + void writeTestFile() throws IOException { 1.94 + javaFile = new File("Test.java"); 1.95 + try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) { 1.96 + out.println("class Test { "); 1.97 + out.println(" public void foo() { }"); 1.98 + out.println("}"); 1.99 + } catch (IOException ioe) { 1.100 + error("IOException while creating Test.java" + ioe); 1.101 + } 1.102 + } 1.103 + 1.104 + File compileTestFile(int i , int j, File f) { 1.105 + int rc = -1; 1.106 + // Src and target are empty 1.107 + if (i == 0 && j == 0 ) { 1.108 + rc = compile("-g", f.getPath()); 1.109 + } else if( j == 0 ) { // target is empty 1.110 + rc = compile("-source", jdk[i], "-g", f.getPath()); 1.111 + } else { 1.112 + rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath()); 1.113 + } 1.114 + if (rc != 0) 1.115 + throw new Error("compilation failed. rc=" + rc); 1.116 + String path = f.getPath(); 1.117 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.118 + } 1.119 + 1.120 + int compile(String... args) { 1.121 + return com.sun.tools.javac.Main.compile(args); 1.122 + } 1.123 + 1.124 + void logMsg (String str) { 1.125 + System.out.println(str); 1.126 + } 1.127 + 1.128 + short getMajorVersion(File f) { 1.129 + List<String> args = new ArrayList<String>(); 1.130 + short majorVer = 0; 1.131 + try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) { 1.132 + in.readInt(); 1.133 + in.readShort(); 1.134 + majorVer = in.readShort(); 1.135 + System.out.println("major version:" + majorVer); 1.136 + } catch (IOException e) { 1.137 + error("IOException while reading Test.class" + e); 1.138 + } 1.139 + return majorVer; 1.140 + } 1.141 + 1.142 + void checkVersion(short majorVer, int expected) { 1.143 + if (majorVer != expected ) { 1.144 + error("versions did not match, Expected: " + expected + "Got: " + majorVer); 1.145 + } 1.146 + } 1.147 + 1.148 + void error(String msg) { 1.149 + System.out.println("error: " + msg); 1.150 + errors++; 1.151 + } 1.152 +}