test/tools/pack200/InstructionTests.java

Print this page




   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  */
  23 import java.io.File;
  24 import java.nio.charset.Charset;
  25 import java.nio.file.Files;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import static java.nio.file.StandardOpenOption.*;
  29 import java.util.regex.Pattern;
  30 
  31 /*
  32  * @test
  33  * @bug 8003549
  34  * @summary tests class files instruction formats introduced in JSR-335
  35  * @compile -XDignore.symbol.file Utils.java InstructionTests.java
  36  * @run main InstructionTests
  37  * @author ksrini
  38  */
  39 public class InstructionTests {
  40     public static void main(String... args) throws Exception {
  41         testInvokeOpCodes();
  42     }
  43     /*
  44      * the following should produce invokestatic and invokespecial
  45      * on InterfaceMethodRefs vs. MethodRefs, packer/unpacker should work
  46      */
  47     static void testInvokeOpCodes() throws Exception {
  48         List<String> scratch = new ArrayList<>();
  49         final String fname = "A";
  50         String javaFileName = fname + Utils.JAVA_FILE_EXT;
  51         scratch.add("interface IntIterator {");
  52         scratch.add("    default void forEach(){}");
  53         scratch.add("    static void next() {}");
  54         scratch.add("}");
  55         scratch.add("class A implements IntIterator {");
  56         scratch.add("public void forEach(Object o){");
  57         scratch.add("IntIterator.super.forEach();");
  58         scratch.add("IntIterator.next();");

  59         scratch.add("}");
  60         scratch.add("}");
  61         File cwd = new File(".");
  62         File javaFile = new File(cwd, javaFileName);
  63         Files.write(javaFile.toPath(), scratch, Charset.defaultCharset(),
  64                 CREATE, TRUNCATE_EXISTING);
  65 
  66         // make sure we have -g so that we  compare LVT and LNT entries
  67         Utils.compiler("-g", javaFile.getName());
  68 






  69         // jar the file up
  70         File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT);
  71         Utils.jar("cvf", testjarFile.getName(), ".");
  72 
  73         // pack using --repack
  74         File outjarFile = new File(cwd, "out" + Utils.JAR_FILE_EXT);
  75         scratch.clear();
  76         scratch.add(Utils.getPack200Cmd());
  77         scratch.add("-J-ea");
  78         scratch.add("-J-esa");
  79         scratch.add("--repack");
  80         scratch.add(outjarFile.getName());
  81         scratch.add(testjarFile.getName());
  82         List<String> output = Utils.runExec(scratch);
  83         // TODO remove this when we get bc escapes working correctly
  84         // this test anyhow would  fail  at that time
  85         findString("WARNING: Passing.*" + fname + Utils.CLASS_FILE_EXT,
  86                         output);
  87 
  88         Utils.doCompareVerify(testjarFile, outjarFile);
  89     }
  90 
  91     static boolean findString(String str, List<String> list) {
  92         Pattern p = Pattern.compile(str);
  93         for (String x : list) {
  94             if (p.matcher(x).matches())
  95                 return true;
  96         }
  97         throw new RuntimeException("Error: " + str + " not found in output");
  98     }
  99 }


   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  */
  23 import java.io.File;
  24 import java.nio.charset.Charset;
  25 import java.nio.file.Files;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import static java.nio.file.StandardOpenOption.*;

  29 
  30 /*
  31  * @test
  32  * @bug 8003549 8007297
  33  * @summary tests class files instruction formats introduced in JSR-335
  34  * @compile -XDignore.symbol.file Utils.java InstructionTests.java
  35  * @run main InstructionTests
  36  * @author ksrini
  37  */
  38 public class InstructionTests {
  39     public static void main(String... args) throws Exception {
  40         testInvokeOpCodes();
  41     }
  42     /*
  43      * the following should produce invokestatic and invokespecial
  44      * on InterfaceMethodRefs vs. MethodRefs, packer/unpacker should work
  45      */
  46     static void testInvokeOpCodes() throws Exception {
  47         List<String> scratch = new ArrayList<>();
  48         final String fname = "A";
  49         String javaFileName = fname + Utils.JAVA_FILE_EXT;
  50         scratch.add("interface I {");
  51         scratch.add("    default void forEach(){}");
  52         scratch.add("    static void next() {}");
  53         scratch.add("}");
  54         scratch.add("class A implements I {");
  55         scratch.add("    public void forEach(Object o){");
  56         scratch.add("        I.super.forEach();");
  57         scratch.add("        I.next();");
  58         scratch.add("    }");
  59         scratch.add("}");

  60         File cwd = new File(".");
  61         File javaFile = new File(cwd, javaFileName);
  62         Files.write(javaFile.toPath(), scratch, Charset.defaultCharset(),
  63                 CREATE, TRUNCATE_EXISTING);
  64 
  65         // -g to compare LVT and LNT entries
  66         Utils.compiler("-g", javaFile.getName());
  67 
  68         File propsFile = new File("pack.props");
  69         scratch.clear();
  70         scratch.add("com.sun.java.util.jar.pack.class.format.error=error");
  71         scratch.add("pack.unknown.attribute=error");
  72         Files.write(propsFile.toPath(), scratch, Charset.defaultCharset(),
  73                 CREATE, TRUNCATE_EXISTING);
  74         // jar the file up
  75         File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT);
  76         Utils.jar("cvf", testjarFile.getName(), ".");
  77 
  78         Utils.testWithRepack(testjarFile, "--config-file=" + propsFile.getName());















  79     }









  80 }