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