test/tools/javac/classfiles/ClassVersionChecker.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial