< prev index next >

test/langtools/tools/javac/classfiles/ClassVersionChecker.java

Print this page




  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 7157626 8001112 8188870 8173382 8193290 8205619
  27  * @summary Test major version for all legal combinations for -source and -target
  28  * @author sgoel
  29  *
  30  * @modules jdk.compiler
  31  */
  32 
  33 import java.io.*;
  34 import java.nio.*;
  35 import java.util.*;
  36 import java.util.regex.*;
  37 





  38 public class ClassVersionChecker {


























  39 
  40     int errors;
  41     String[] jdk = {"", "1.6", "1.7", "1.8", "1.9", "1.10", "11", "12"};
  42     File javaFile = null;
  43 
  44     public static void main(String[] args) throws Throwable {
  45         new ClassVersionChecker().run();
  46     }
  47 
  48     void run() throws Exception {
  49         writeTestFile();
  50         /* Rules applicable for -source and -target combinations

  51          * 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

  53          * 3. If both non-empty, version is based on target
  54          */
  55 
  56         /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...)
  57          * ver[0][0] => no -source or -target was given
  58          * -1 => invalid combinations
  59          */
  60         int[][] ver =
  61             {{56, -1, -1, -1, -1, -1, -1, -1},
  62              {56, 50, 51, 52, 53, 54, 55, 56},
  63              {56, -1, 51, 52, 53, 54, 55, 56},
  64              {56, -1, -1, 52, 53, 54, 55, 56},
  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                 }
  76             }
  77         }
  78 
  79         if (errors > 0)
  80             throw new Exception(errors + " errors found");
  81     }
  82 
  83     void test (int i, int j, int expected) {
  84         File classFile = compileTestFile(i, j, javaFile);
  85         short majorVer = getMajorVersion(classFile);
  86         checkVersion(majorVer, expected);
  87     }
  88 
  89     void writeTestFile() throws IOException {
  90         javaFile = new File("Test.java");
  91         try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
  92             out.println("class Test { ");
  93             out.println("  public void foo() { }");
  94             out.println("}");
  95         } catch (IOException ioe) {
  96             error("IOException while creating Test.java" + ioe);
  97         }
  98     }
  99 
 100     File compileTestFile(int i , int j, File f) {
 101         int rc = -1;
 102         // Src and target are empty
 103         if (i == 0 && j == 0 ) {
 104             rc = compile("-g", f.getPath());
 105         } else if( j == 0 ) {  // target is empty
 106             rc = compile("-source", jdk[i], "-g", f.getPath());
 107         } else {
 108             rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath());
 109         }
 110         if (rc != 0)
 111             throw new Error("compilation failed. rc=" + rc);
 112         String path = f.getPath();
 113         return new File(path.substring(0, path.length() - 5) + ".class");
 114     }
 115 
 116     int compile(String... args) {
 117         return com.sun.tools.javac.Main.compile(args);
 118     }
 119 
 120     void logMsg (String str) {
 121         System.out.println(str);
 122     }
 123 
 124     short getMajorVersion(File f) {
 125         List<String> args = new ArrayList<String>();
 126         short majorVer = 0;
 127         try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
 128             in.readInt();


  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 7157626 8001112 8188870 8173382 8193290 8205619
  27  * @summary Test major version for all legal combinations for -source and -target
  28  * @author sgoel
  29  *
  30  * @modules jdk.compiler
  31  */
  32 
  33 import java.io.*;
  34 import java.nio.*;
  35 import java.util.*;
  36 import java.util.regex.*;
  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  */
  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     }
  70 
  71     int errors;
  72 
  73     File javaFile = null;
  74 
  75     public static void main(String[] args) throws Throwable {
  76         new ClassVersionChecker().run();
  77     }
  78 
  79     void run() throws Exception {
  80         writeTestFile();
  81         /*
  82          * Rules applicable for -source and -target combinations:
  83          * 1. If both empty, version num is for the current release
  84          * 2. If source is not empty and target is empty, version is
  85          * based on the current release
  86          * 3. If both non-empty, version is based on target
  87          */
  88         test("", "", CURRENT.classFileVer());
  89         for (Version source : Version.values()) {
  90             test(source.release(), "", CURRENT.classFileVer()); // no target
  91             for (Version target : Version.values()) {
  92                 if (target.compareTo(source) < 0)
  93                     continue; // Target < source not a valid set of arguments
  94                 else {
  95                     logMsg("Running for src = " + source + " target = "+ target +
  96                            " expected = " + target.classFileVer());
  97                     test(source.release(), target.release(), target.classFileVer());










  98                 }
  99             }
 100         }
 101 
 102         if (errors > 0)
 103             throw new Exception(errors + " errors found");
 104     }
 105 
 106     void test (String i, String j, int expected) {
 107         File classFile = compileTestFile(i, j, javaFile);
 108         short majorVer = getMajorVersion(classFile);
 109         checkVersion(majorVer, expected);
 110     }
 111 
 112     void writeTestFile() throws IOException {
 113         javaFile = new File("Test.java");
 114         try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
 115             out.println("class Test { ");
 116             out.println("  public void foo() { }");
 117             out.println("}");
 118         } catch (IOException ioe) {
 119             error("IOException while creating Test.java" + ioe);
 120         }
 121     }
 122 
 123     File compileTestFile(String i , String j, File f) {
 124         int rc = -1;
 125         // Src and target are empty
 126         if (i.isEmpty() && j.isEmpty() ) {
 127             rc = compile("-g", f.getPath());
 128         } else if( j.isEmpty()) {  // target is empty
 129             rc = compile("-source", i, "-g", f.getPath());
 130         } else {
 131             rc = compile("-source", i, "-target", j, "-g", f.getPath());
 132         }
 133         if (rc != 0)
 134             throw new Error("compilation failed. rc=" + rc);
 135         String path = f.getPath();
 136         return new File(path.substring(0, path.length() - 5) + ".class");
 137     }
 138 
 139     int compile(String... args) {
 140         return com.sun.tools.javac.Main.compile(args);
 141     }
 142 
 143     void logMsg (String str) {
 144         System.out.println(str);
 145     }
 146 
 147     short getMajorVersion(File f) {
 148         List<String> args = new ArrayList<String>();
 149         short majorVer = 0;
 150         try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
 151             in.readInt();
< prev index next >