test/tools/javac/classfiles/ClassVersionChecker.java

Wed, 31 Oct 2012 10:21:14 -0700

author
ksrini
date
Wed, 31 Oct 2012 10:21:14 -0700
changeset 1395
9bce0c73583d
parent 1248
55ae94116e89
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8001112: Make -target 8 in javac generate version 52.0 classfile
Reviewed-by: darcy, jjg

jjg@1248 1 /*
jjg@1248 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1248 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1248 4 *
jjg@1248 5 * This code is free software; you can redistribute it and/or modify it
jjg@1248 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1248 7 * published by the Free Software Foundation.
jjg@1248 8 *
jjg@1248 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1248 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1248 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1248 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1248 13 * accompanied this code).
jjg@1248 14 *
jjg@1248 15 * You should have received a copy of the GNU General Public License version
jjg@1248 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1248 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1248 18 *
jjg@1248 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1248 20 * or visit www.oracle.com if you need additional information or have any
jjg@1248 21 * questions.
jjg@1248 22 */
jjg@1248 23
jjg@1248 24 /*
jjg@1248 25 * @test
ksrini@1395 26 * @bug 7157626 8001112
jjg@1248 27 * @summary Test major version for all legal combinations for -source and -target
jjg@1248 28 * @author sgoel
jjg@1248 29 *
jjg@1248 30 */
jjg@1248 31
jjg@1248 32 import java.io.*;
jjg@1248 33 import java.nio.*;
jjg@1248 34 import java.util.*;
jjg@1248 35 import java.util.regex.*;
jjg@1248 36
jjg@1248 37 public class ClassVersionChecker {
jjg@1248 38
jjg@1248 39 int errors;
jjg@1248 40 String[] jdk = {"","1.2","1.3","1.4","1.5","1.6","1.7","1.8"};
jjg@1248 41 File javaFile = null;
jjg@1248 42
jjg@1248 43 public static void main(String[] args) throws Throwable {
jjg@1248 44 new ClassVersionChecker().run();
jjg@1248 45 }
jjg@1248 46
jjg@1248 47 void run() throws Exception {
jjg@1248 48 writeTestFile();
jjg@1248 49 /* Rules applicable for -source and -target combinations
jjg@1248 50 * 1. If both empty, version num is for 1.7
jjg@1248 51 * 2. If source is not empty and target is empty, version is based on source
jjg@1248 52 * 3. If both non-empty, version is based on target
jjg@1248 53 */
jjg@1248 54
jjg@1248 55 /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...)
jjg@1248 56 * ver[0][0] => no -source or -target was given
jjg@1248 57 * -1 => invalid combinations
jjg@1248 58 */
jjg@1248 59 int[][] ver =
ksrini@1395 60 {{52, -1, -1, -1, -1, -1, -1, -1},
ksrini@1395 61 {48, 46, 47, 48, 49, 50, 51, 52},
ksrini@1395 62 {48, 46, 47, 48, 49, 50, 51, 52},
ksrini@1395 63 {48, -1, -1, 48, 49, 50, 51, 52},
ksrini@1395 64 {52, -1, -1, -1, 49, 50, 51, 52},
ksrini@1395 65 {52, -1, -1, -1, -1, 50, 51, 52},
ksrini@1395 66 {52, -1, -1, -1, -1, -1, 51, 52},
ksrini@1395 67 {52, -1, -1, -1, -1, -1, -1, 52}};
jjg@1248 68
jjg@1248 69 // Loop to run all possible combinations of source/target values
jjg@1248 70 for (int i = 0; i< ver.length; i++) {
jjg@1248 71 for (int j = 0 ; j< ver[i].length; j++) {
jjg@1248 72 if(ver[i][j] != -1) {
jjg@1248 73 logMsg("Index values for i = " + i + " j = " + j);
jjg@1248 74 logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]);
jjg@1248 75 test(i,j, ver[i][j]);
jjg@1248 76 }
jjg@1248 77 }
jjg@1248 78 }
jjg@1248 79
jjg@1248 80 if (errors > 0)
jjg@1248 81 throw new Exception(errors + " errors found");
jjg@1248 82 }
jjg@1248 83
jjg@1248 84 void test (int i, int j, int expected) {
jjg@1248 85 File classFile = compileTestFile(i, j, javaFile);
jjg@1248 86 short majorVer = getMajorVersion(classFile);
jjg@1248 87 checkVersion(majorVer, expected);
jjg@1248 88 }
jjg@1248 89
jjg@1248 90 void writeTestFile() throws IOException {
jjg@1248 91 javaFile = new File("Test.java");
jjg@1248 92 try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
jjg@1248 93 out.println("class Test { ");
jjg@1248 94 out.println(" public void foo() { }");
jjg@1248 95 out.println("}");
jjg@1248 96 } catch (IOException ioe) {
jjg@1248 97 error("IOException while creating Test.java" + ioe);
jjg@1248 98 }
jjg@1248 99 }
jjg@1248 100
jjg@1248 101 File compileTestFile(int i , int j, File f) {
jjg@1248 102 int rc = -1;
jjg@1248 103 // Src and target are empty
jjg@1248 104 if (i == 0 && j == 0 ) {
jjg@1248 105 rc = compile("-g", f.getPath());
jjg@1248 106 } else if( j == 0 ) { // target is empty
jjg@1248 107 rc = compile("-source", jdk[i], "-g", f.getPath());
jjg@1248 108 } else {
jjg@1248 109 rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath());
jjg@1248 110 }
jjg@1248 111 if (rc != 0)
jjg@1248 112 throw new Error("compilation failed. rc=" + rc);
jjg@1248 113 String path = f.getPath();
jjg@1248 114 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@1248 115 }
jjg@1248 116
jjg@1248 117 int compile(String... args) {
jjg@1248 118 return com.sun.tools.javac.Main.compile(args);
jjg@1248 119 }
jjg@1248 120
jjg@1248 121 void logMsg (String str) {
jjg@1248 122 System.out.println(str);
jjg@1248 123 }
jjg@1248 124
jjg@1248 125 short getMajorVersion(File f) {
jjg@1248 126 List<String> args = new ArrayList<String>();
jjg@1248 127 short majorVer = 0;
jjg@1248 128 try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
jjg@1248 129 in.readInt();
jjg@1248 130 in.readShort();
jjg@1248 131 majorVer = in.readShort();
jjg@1248 132 System.out.println("major version:" + majorVer);
jjg@1248 133 } catch (IOException e) {
jjg@1248 134 error("IOException while reading Test.class" + e);
jjg@1248 135 }
jjg@1248 136 return majorVer;
jjg@1248 137 }
jjg@1248 138
jjg@1248 139 void checkVersion(short majorVer, int expected) {
jjg@1248 140 if (majorVer != expected ) {
jjg@1248 141 error("versions did not match, Expected: " + expected + "Got: " + majorVer);
jjg@1248 142 }
jjg@1248 143 }
jjg@1248 144
jjg@1248 145 void error(String msg) {
jjg@1248 146 System.out.println("error: " + msg);
jjg@1248 147 errors++;
jjg@1248 148 }
jjg@1248 149 }

mercurial