< prev index next >

test/jdk/java/lang/invoke/lambda/LambdaAsm.java

Print this page

  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 
 24 /*
 25  * @test
 26  * @bug 8027232
 27  * @summary ensures that j.l.i.InvokerByteCodeGenerator and ASM visitMethodInsn
 28  * generate  bytecodes with correct constant pool references
 29  * @modules java.base/jdk.internal.org.objectweb.asm
 30  *          jdk.jdeps/com.sun.tools.classfile
 31  *          jdk.zipfs
 32  * @compile -XDignore.symbol.file LambdaAsm.java LUtils.java
 33  * @run main/othervm LambdaAsm


 34  */
 35 import com.sun.tools.classfile.Attribute;
 36 import com.sun.tools.classfile.ClassFile;
 37 import com.sun.tools.classfile.Code_attribute;
 38 import com.sun.tools.classfile.ConstantPool;
 39 import com.sun.tools.classfile.ConstantPool.CPInfo;
 40 import com.sun.tools.classfile.Instruction;
 41 import com.sun.tools.classfile.Method;
 42 import java.io.ByteArrayInputStream;
 43 import java.io.File;


 44 import java.util.ArrayList;
 45 import java.nio.file.DirectoryStream;
 46 import java.nio.file.Path;
 47 import jdk.internal.org.objectweb.asm.ClassWriter;
 48 import jdk.internal.org.objectweb.asm.MethodVisitor;


 49 
 50 import static java.nio.file.Files.*;
 51 import static jdk.internal.org.objectweb.asm.Opcodes.*;

 52 
 53 public class LambdaAsm {
 54     static final Path DUMP_LAMBDA_PROXY_CLASS_FILES = Path.of("DUMP_LAMBDA_PROXY_CLASS_FILES");


 55 
 56     static final File TestFile = new File("A.java");
 57 
 58     static void init() {
 59         emitCode();
 60         LUtils.compile(TestFile.getName());
 61         LUtils.TestResult tr = LUtils.doExec(LUtils.JAVA_CMD.getAbsolutePath(),
 62                 "-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true",
 63                 "-cp", ".", "A");
 64         if (tr.exitValue != 0) {
 65             System.out.println("Error: " + tr.toString());
 66             throw new RuntimeException("could not create proxy classes");
 67         }
 68     }
 69 
 70     static void emitCode() {
 71         ArrayList<String> scratch = new ArrayList<>();
 72         scratch.add("import java.util.function.*;");
 73         scratch.add("class A {");
 74         scratch.add("   interface I {");
 75         scratch.add("       default Supplier<Integer> a() { return () -> 1; }");
 76         scratch.add("       default Supplier<Integer> b(int i) { return () -> i; }");
 77         scratch.add("       default Supplier<Integer> c(int i) { return () -> m(i); }");
 78         scratch.add("       int m(int i);");
 79         scratch.add("       static Integer d() { return 0; }");
 80         scratch.add("   }");
 81         scratch.add("   static class C implements I {");
 82         scratch.add("       public int m(int i) { return i;}");
 83         scratch.add("   }");
 84         scratch.add("   public static void main(String[] args) {");
 85         scratch.add("       I i = new C();");
 86         scratch.add("       i.a();");
 87         scratch.add("       i.b(1);");
 88         scratch.add("       i.c(1);");
 89         scratch.add("       I.d();");
 90         scratch.add("   }");
 91         scratch.add("}");
 92         LUtils.createFile(TestFile, scratch);



 93     }
 94 
 95     static void checkMethod(String cname, String mname, ConstantPool cp,
 96             Code_attribute code) throws ConstantPool.InvalidIndex {
 97         for (Instruction i : code.getInstructions()) {
 98             String iname = i.getMnemonic();
 99             if ("invokespecial".equals(iname)
100                     || "invokestatic".equals(iname)) {
101                 int idx = i.getByte(2);
102                 System.out.println("Verifying " + cname + ":" + mname +
103                         " instruction:" + iname + " index @" + idx);
104                 CPInfo cpinfo = cp.get(idx);
105                 if (cpinfo instanceof ConstantPool.CONSTANT_Methodref_info) {
106                     throw new RuntimeException("unexpected CP type expected "
107                             + "InterfaceMethodRef, got MethodRef, " + cname
108                             + ", " + mname);
109                 }
110             }
111         }
112     }

  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 
 24 /*
 25  * @test
 26  * @bug 8027232
 27  * @library /test/lib/

 28  * @modules java.base/jdk.internal.org.objectweb.asm
 29  *          jdk.jdeps/com.sun.tools.classfile
 30  *          jdk.zipfs
 31  * @compile LambdaAsm.java
 32  * @run main/othervm LambdaAsm
 33  * @summary ensures that j.l.i.InvokerByteCodeGenerator and ASM visitMethodInsn
 34  * generate  bytecodes with correct constant pool references
 35  */
 36 import com.sun.tools.classfile.Attribute;
 37 import com.sun.tools.classfile.ClassFile;
 38 import com.sun.tools.classfile.Code_attribute;
 39 import com.sun.tools.classfile.ConstantPool;
 40 import com.sun.tools.classfile.ConstantPool.CPInfo;
 41 import com.sun.tools.classfile.Instruction;
 42 import com.sun.tools.classfile.Method;
 43 import java.io.ByteArrayInputStream;
 44 import java.io.IOException;
 45 import java.nio.charset.Charset;
 46 import java.nio.file.Files;
 47 import java.util.ArrayList;
 48 import java.nio.file.DirectoryStream;
 49 import java.nio.file.Path;
 50 import jdk.internal.org.objectweb.asm.ClassWriter;
 51 import jdk.internal.org.objectweb.asm.MethodVisitor;
 52 import jdk.test.lib.compiler.CompilerUtils;
 53 import jdk.test.lib.process.OutputAnalyzer;
 54 
 55 import static java.nio.file.Files.*;
 56 import static jdk.internal.org.objectweb.asm.Opcodes.*;
 57 import static jdk.test.lib.process.ProcessTools.*;
 58 
 59 public class LambdaAsm {
 60     static final Path DUMP_LAMBDA_PROXY_CLASS_FILES = Path.of("DUMP_LAMBDA_PROXY_CLASS_FILES");
 61     static final Path SRC = Path.of("src");
 62     static final Path CLASSES = Path.of("classes");
 63 
 64     static void init() throws Exception {


 65         emitCode();
 66         CompilerUtils.compile(SRC, CLASSES);
 67         OutputAnalyzer outputAnalyzer = executeProcess(createTestJavaProcessBuilder(
 68                 "-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true",
 69                 "-cp", CLASSES.toString(), "A"));
 70         outputAnalyzer.shouldHaveExitValue(0);



 71     }
 72 
 73     static void emitCode() throws IOException {
 74         ArrayList<String> scratch = new ArrayList<>();
 75         scratch.add("import java.util.function.*;");
 76         scratch.add("class A {");
 77         scratch.add("   interface I {");
 78         scratch.add("       default Supplier<Integer> a() { return () -> 1; }");
 79         scratch.add("       default Supplier<Integer> b(int i) { return () -> i; }");
 80         scratch.add("       default Supplier<Integer> c(int i) { return () -> m(i); }");
 81         scratch.add("       int m(int i);");
 82         scratch.add("       static Integer d() { return 0; }");
 83         scratch.add("   }");
 84         scratch.add("   static class C implements I {");
 85         scratch.add("       public int m(int i) { return i;}");
 86         scratch.add("   }");
 87         scratch.add("   public static void main(String[] args) {");
 88         scratch.add("       I i = new C();");
 89         scratch.add("       i.a();");
 90         scratch.add("       i.b(1);");
 91         scratch.add("       i.c(1);");
 92         scratch.add("       I.d();");
 93         scratch.add("   }");
 94         scratch.add("}");
 95 
 96         Path testFile = SRC.resolve("A.java");
 97         Files.createDirectories(SRC);
 98         Files.write(testFile, scratch, Charset.defaultCharset());
 99     }
100 
101     static void checkMethod(String cname, String mname, ConstantPool cp,
102             Code_attribute code) throws ConstantPool.InvalidIndex {
103         for (Instruction i : code.getInstructions()) {
104             String iname = i.getMnemonic();
105             if ("invokespecial".equals(iname)
106                     || "invokestatic".equals(iname)) {
107                 int idx = i.getByte(2);
108                 System.out.println("Verifying " + cname + ":" + mname +
109                         " instruction:" + iname + " index @" + idx);
110                 CPInfo cpinfo = cp.get(idx);
111                 if (cpinfo instanceof ConstantPool.CONSTANT_Methodref_info) {
112                     throw new RuntimeException("unexpected CP type expected "
113                             + "InterfaceMethodRef, got MethodRef, " + cname
114                             + ", " + mname);
115                 }
116             }
117         }
118     }
< prev index next >