1 /*
   2  * Copyright (c) 2019, 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 
  24 /*
  25  * @test
  26  * @summary intrinsics: check that javac is generating the expected bytecode
  27  * @modules jdk.jdeps/com.sun.tools.classfile
  28  *          jdk.compiler/com.sun.tools.javac.util
  29  * @run main CheckIndyGeneratedTest
  30  */
  31 
  32 import com.sun.tools.javac.util.Assert;
  33 
  34 import com.sun.tools.classfile.*;
  35 import com.sun.tools.classfile.BootstrapMethods_attribute.*;
  36 import com.sun.tools.classfile.ConstantPool.*;
  37 import java.io.*;
  38 
  39 public class CheckIndyGeneratedTest {
  40     static class CheckIndyGeneratedTestsub {
  41         void test() {
  42             String s = String.format("%s", "Bob");
  43             System.out.println(s);
  44         }
  45     }
  46 
  47     static final String SUBTEST_NAME = CheckIndyGeneratedTestsub.class.getName() + ".class";
  48     static final String TEST_METHOD_NAME = "test";
  49 
  50     public static void main(String... args) throws Exception {
  51         new CheckIndyGeneratedTest().run();
  52     }
  53 
  54     public void run() throws Exception {
  55         String workDir = System.getProperty("test.classes");
  56         File compiledTest = new File(workDir, SUBTEST_NAME);
  57         verifyIndyGenerated(compiledTest);
  58     }
  59 
  60     void verifyIndyGenerated(File f) {
  61         try {
  62             int count = 0;
  63             ClassFile cf = ClassFile.read(f);
  64             Method testMethod = null;
  65             for (Method m : cf.methods) {
  66                 if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
  67                     testMethod = m;
  68                     break;
  69                 }
  70             }
  71             if (testMethod == null) {
  72                 throw new Error("Test method not found");
  73             }
  74             Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
  75             if (testMethod == null) {
  76                 throw new Error("Code attribute for test() method not found");
  77             }
  78             for (Instruction inst : ea.getInstructions()) {
  79                 if (inst.getMnemonic().equals("invokedynamic")) {
  80                     int index1 = inst.getUnsignedByte(1);
  81                     int index2 = inst.getUnsignedByte(2);
  82                     int cpIndex = index1 << 8 | index2;
  83                     CONSTANT_InvokeDynamic_info indyCP = (CONSTANT_InvokeDynamic_info)cf.constant_pool.get(cpIndex);
  84                     System.out.println(indyCP.bootstrap_method_attr_index);
  85                     BootstrapMethods_attribute bsmAttr = (BootstrapMethods_attribute)cf.getAttribute(Attribute.BootstrapMethods);
  86                     BootstrapMethodSpecifier bsms = bsmAttr.bootstrap_method_specifiers[indyCP.bootstrap_method_attr_index];
  87                     CONSTANT_MethodHandle_info mhi = (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsms.bootstrap_method_ref);
  88                     CONSTANT_Methodref_info mri = (CONSTANT_Methodref_info)cf.constant_pool.get(mhi.reference_index);
  89                     Assert.check(mri.getClassName().equals("java/lang/invoke/IntrinsicFactory"));
  90                     CONSTANT_NameAndType_info nti = mri.getNameAndTypeInfo();
  91                     Assert.check(nti.getName().equals("staticStringFormatBootstrap"));
  92                     Assert.check(nti.getType().equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;)Ljava/lang/invoke/CallSite;"));
  93                     return;
  94                 }
  95             }
  96         } catch (Exception e) {
  97             e.printStackTrace();
  98             throw new Error("error reading " + f +": " + e);
  99         }
 100         throw new AssertionError("no invokedynamic instruction found");
 101     }
 102 }