test/langtools/tools/javac/classfiles/ClassVersionChecker.java
changeset 50935 dfd59db382c6
parent 50892 a5557f24b4d4
child 50949 999f09bf3464
equal deleted inserted replaced
50934:ab998c2bd38f 50935:dfd59db382c6
    33 import java.io.*;
    33 import java.io.*;
    34 import java.nio.*;
    34 import java.nio.*;
    35 import java.util.*;
    35 import java.util.*;
    36 import java.util.regex.*;
    36 import java.util.regex.*;
    37 
    37 
       
    38 /*
       
    39  * If not explicitly specified the latest source and latest target
       
    40  * values are the defaults. If explicitly specified, the target value
       
    41  * has to be greater than or equal to the source value.
       
    42  */
    38 public class ClassVersionChecker {
    43 public class ClassVersionChecker {
       
    44     private static enum Version {
       
    45         SIX("6", 50),
       
    46         SEVEN("7", 51),
       
    47         EIGHT("8", 52),
       
    48         NINE("9", 53),
       
    49         TEN("10", 54),
       
    50         ELEVEN("11", 55),
       
    51         TWELVE("12", 56);
       
    52 
       
    53         private Version(String release, int classFileVer) {
       
    54             this.release = release;
       
    55             this.classFileVer = classFileVer;
       
    56         }
       
    57         private final String release;
       
    58         private final int classFileVer;
       
    59 
       
    60         String release() {return release;}
       
    61         int classFileVer() {return classFileVer;}
       
    62     }
       
    63 
       
    64     static final Version CURRENT;
       
    65     static {
       
    66         Version[] versions = Version.values();
       
    67         int index = versions.length;
       
    68         CURRENT = versions[index - 1];
       
    69     }
    39 
    70 
    40     int errors;
    71     int errors;
    41     String[] jdk = {"", "1.6", "1.7", "1.8", "1.9", "1.10", "11", "12"};
    72 
    42     File javaFile = null;
    73     File javaFile = null;
    43 
    74 
    44     public static void main(String[] args) throws Throwable {
    75     public static void main(String[] args) throws Throwable {
    45         new ClassVersionChecker().run();
    76         new ClassVersionChecker().run();
    46     }
    77     }
    47 
    78 
    48     void run() throws Exception {
    79     void run() throws Exception {
    49         writeTestFile();
    80         writeTestFile();
    50         /* Rules applicable for -source and -target combinations
    81         /*
       
    82          * Rules applicable for -source and -target combinations:
    51          * 1. If both empty, version num is for the current release
    83          * 1. If both empty, version num is for the current release
    52          * 2. If source is not empty and target is empty, version is based on source
    84          * 2. If source is not empty and target is empty, version is
       
    85          * based on the current release
    53          * 3. If both non-empty, version is based on target
    86          * 3. If both non-empty, version is based on target
    54          */
    87          */
    55 
    88         test("", "", CURRENT.classFileVer());
    56         /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...)
    89         for (Version source : Version.values()) {
    57          * ver[0][0] => no -source or -target was given
    90             test(source.release(), "", CURRENT.classFileVer()); // no target
    58          * -1 => invalid combinations
    91             for (Version target : Version.values()) {
    59          */
    92                 if (target.compareTo(source) < 0)
    60         int[][] ver =
    93                     continue; // Target < source not a valid set of arguments
    61             {{56, -1, -1, -1, -1, -1, -1, -1},
    94                 else {
    62              {56, 50, 51, 52, 53, 54, 55, 56},
    95                     logMsg("Running for src = " + source + " target = "+ target +
    63              {56, -1, 51, 52, 53, 54, 55, 56},
    96                            " expected = " + target.classFileVer());
    64              {56, -1, -1, 52, 53, 54, 55, 56},
    97                     test(source.release(), target.release(), target.classFileVer());
    65              {56, -1, -1, -1, 53, 54, 55, 56},
       
    66              {56, -1, -1, -1, -1, 54, 55, 56}};
       
    67 
       
    68         // Loop to run all possible combinations of source/target values
       
    69         for (int i = 0; i< ver.length; i++) {
       
    70             for (int j = 0 ; j< ver[i].length; j++) {
       
    71                 if(ver[i][j] != -1) {
       
    72                     logMsg("Index values for i = " + i + " j = " + j);
       
    73                     logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]);
       
    74                     test(i,j, ver[i][j]);
       
    75                 }
    98                 }
    76             }
    99             }
    77         }
   100         }
    78 
   101 
    79         if (errors > 0)
   102         if (errors > 0)
    80             throw new Exception(errors + " errors found");
   103             throw new Exception(errors + " errors found");
    81     }
   104     }
    82 
   105 
    83     void test (int i, int j, int expected) {
   106     void test(String i, String j, int expected) {
    84         File classFile = compileTestFile(i, j, javaFile);
   107         File classFile = compileTestFile(i, j, javaFile);
    85         short majorVer = getMajorVersion(classFile);
   108         short majorVer = getMajorVersion(classFile);
    86         checkVersion(majorVer, expected);
   109         checkVersion(majorVer, expected);
    87     }
   110     }
    88 
   111 
    95         } catch (IOException ioe) {
   118         } catch (IOException ioe) {
    96             error("IOException while creating Test.java" + ioe);
   119             error("IOException while creating Test.java" + ioe);
    97         }
   120         }
    98     }
   121     }
    99 
   122 
   100     File compileTestFile(int i , int j, File f) {
   123     File compileTestFile(String i, String j, File f) {
   101         int rc = -1;
   124         int rc = -1;
   102         // Src and target are empty
   125         // Src and target are empty
   103         if (i == 0 && j == 0 ) {
   126         if (i.isEmpty() && j.isEmpty() ) {
   104             rc = compile("-g", f.getPath());
   127             rc = compile("-g", f.getPath());
   105         } else if( j == 0 ) {  // target is empty
   128         } else if( j.isEmpty()) {  // target is empty
   106             rc = compile("-source", jdk[i], "-g", f.getPath());
   129             rc = compile("-source", i, "-g", f.getPath());
   107         } else {
   130         } else {
   108             rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath());
   131             rc = compile("-source", i, "-target", j, "-g", f.getPath());
   109         }
   132         }
   110         if (rc != 0)
   133         if (rc != 0)
   111             throw new Error("compilation failed. rc=" + rc);
   134             throw new Error("compilation failed. rc=" + rc);
   112         String path = f.getPath();
   135         String path = f.getPath();
   113         return new File(path.substring(0, path.length() - 5) + ".class");
   136         return new File(path.substring(0, path.length() - 5) + ".class");