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

     1 /*
     2  * Copyright (c) 2012, 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  */
    24 /*
    25  * @test
    26  * @bug 7157626 8001112
    27  * @summary Test major version for all legal combinations for -source and -target
    28  * @author sgoel
    29  *
    30  */
    32 import java.io.*;
    33 import java.nio.*;
    34 import java.util.*;
    35 import java.util.regex.*;
    37 public class ClassVersionChecker {
    39     int errors;
    40     String[] jdk = {"","1.2","1.3","1.4","1.5","1.6","1.7","1.8"};
    41     File javaFile = null;
    43     public static void main(String[] args) throws Throwable {
    44         new ClassVersionChecker().run();
    45     }
    47     void run() throws Exception {
    48         writeTestFile();
    49         /* Rules applicable for -source and -target combinations
    50          * 1. If both empty, version num is for 1.7
    51          * 2. If source is not empty and target is empty, version is based on source
    52          * 3. If both non-empty, version is based on target
    53          */
    55         /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...)
    56          * ver[0][0] => no -source or -target was given
    57          * -1 => invalid combinations
    58          */
    59         int[][] ver =
    60                 {{52, -1, -1, -1, -1, -1, -1, -1},
    61                  {48, 46, 47, 48, 49, 50, 51, 52},
    62                  {48, 46, 47, 48, 49, 50, 51, 52},
    63                  {48, -1, -1, 48, 49, 50, 51, 52},
    64                  {52, -1, -1, -1, 49, 50, 51, 52},
    65                  {52, -1, -1, -1, -1, 50, 51, 52},
    66                  {52, -1, -1, -1, -1, -1, 51, 52},
    67                  {52, -1, -1, -1, -1, -1, -1, 52}};
    69         // Loop to run all possible combinations of source/target values
    70         for (int i = 0; i< ver.length; i++) {
    71             for (int j = 0 ; j< ver[i].length; j++) {
    72                 if(ver[i][j] != -1) {
    73                     logMsg("Index values for i = " + i + " j = " + j);
    74                     logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]);
    75                     test(i,j, ver[i][j]);
    76                 }
    77             }
    78         }
    80         if (errors > 0)
    81             throw new Exception(errors + " errors found");
    82     }
    84     void test (int i, int j, int expected) {
    85         File classFile = compileTestFile(i, j, javaFile);
    86         short majorVer = getMajorVersion(classFile);
    87         checkVersion(majorVer, expected);
    88     }
    90     void writeTestFile() throws IOException {
    91         javaFile = new File("Test.java");
    92         try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
    93             out.println("class Test { ");
    94             out.println("  public void foo() { }");
    95             out.println("}");
    96         } catch (IOException ioe) {
    97             error("IOException while creating Test.java" + ioe);
    98         }
    99     }
   101     File compileTestFile(int i , int j, File f) {
   102         int rc = -1;
   103         // Src and target are empty
   104         if (i == 0 && j == 0 ) {
   105             rc = compile("-g", f.getPath());
   106         } else if( j == 0 ) {  // target is empty
   107             rc = compile("-source", jdk[i], "-g", f.getPath());
   108         } else {
   109             rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath());
   110         }
   111         if (rc != 0)
   112             throw new Error("compilation failed. rc=" + rc);
   113         String path = f.getPath();
   114         return new File(path.substring(0, path.length() - 5) + ".class");
   115     }
   117     int compile(String... args) {
   118         return com.sun.tools.javac.Main.compile(args);
   119     }
   121     void logMsg (String str) {
   122         System.out.println(str);
   123     }
   125     short getMajorVersion(File f) {
   126         List<String> args = new ArrayList<String>();
   127         short majorVer = 0;
   128         try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
   129             in.readInt();
   130             in.readShort();
   131             majorVer = in.readShort();
   132             System.out.println("major version:" +  majorVer);
   133         } catch (IOException e) {
   134             error("IOException while reading Test.class" + e);
   135         }
   136         return majorVer;
   137     }
   139     void checkVersion(short majorVer, int expected) {
   140         if (majorVer != expected ) {
   141             error("versions did not match, Expected: " + expected + "Got: " + majorVer);
   142         }
   143     }
   145     void error(String msg) {
   146        System.out.println("error: " + msg);
   147        errors++;
   148     }
   149 }

mercurial