1 /*
   2  * Copyright (c) 2013, 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  */
  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 }