1 /*
   2  * Copyright (c) 2016, 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 Check that javac emits invokedirect instruction for method calls on values
  27  * @compile -XDenableValueTypes CheckNoInvokeDirect.java
  28  * @run main/othervm -Xverify:none -XX:+EnableValhalla CheckNoInvokeDirect
  29  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
  30  */
  31 
  32 import com.sun.tools.javac.util.Assert;
  33 
  34 import java.io.PrintWriter;
  35 import java.io.StringWriter;
  36 import java.nio.file.Paths;
  37 
  38 public class CheckNoInvokeDirect {
  39 
  40     interface I {
  41         default void foo() {}
  42         default void goo() {}
  43     }
  44 
  45     static final __ByValue class Value implements I {
  46 
  47         static void soo() {}
  48         public void foo() {}
  49         void boo() {}
  50 
  51         void test() {
  52             soo();  // static method.
  53             foo();  // invokedirect, overridden.
  54             goo();  // invokedirect inherited.
  55             boo();  // invokedirect fresh instance method
  56         }
  57     }
  58 
  59     public static void main(String[] args) {
  60         new CheckNoInvokeDirect().run();
  61     }
  62 
  63     void run() {
  64         String [] params = new String [] { "-v",
  65                                             Paths.get(System.getProperty("test.classes"),
  66                                                 "CheckNoInvokeDirect$Value.class").toString() };
  67         runCheck(params, new String [] {
  68 
  69            "#1 = Methodref          #7.#19         // \";Qjava/lang/__Value;\".\"<init>\":()V",
  70            "#2 = Methodref          #6.#20         // \";QCheckNoInvokeDirect$Value;\".soo:()V",
  71            "#3 = Methodref          #6.#21         // \";QCheckNoInvokeDirect$Value;\".foo:()V",
  72            "#4 = Methodref          #6.#22         // \";QCheckNoInvokeDirect$Value;\".goo:()V",
  73            "#5 = Methodref          #6.#23         // \";QCheckNoInvokeDirect$Value;\".boo:()V",
  74 
  75            "0: vload         0",
  76            "2: invokespecial #1                  // Method \";Qjava/lang/__Value;\".\"<init>\":()V",
  77            "5: return",
  78 
  79 
  80            "0: invokestatic  #2                  // Method soo:()V",
  81            "3: vload         0",
  82            "5: invokevirtual #3                  // Method foo:()V",
  83            "8: vload         0",
  84           "10: invokevirtual #4                  // Method goo:()V",
  85           "13: vload         0",
  86           "15: invokevirtual #5                  // Method boo:()V",
  87           "18: return"
  88                          });
  89 
  90      }
  91 
  92      void runCheck(String [] params, String [] expectedOut) {
  93         StringWriter s;
  94         String out;
  95 
  96         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
  97             com.sun.tools.javap.Main.run(params, pw);
  98             out = s.toString();
  99         }
 100         int errors = 0;
 101         for (String eo: expectedOut) {
 102             if (!out.contains(eo)) {
 103                 System.err.println("Match not found for string: " + eo);
 104                 errors++;
 105             }
 106         }
 107          if (errors > 0) {
 108              throw new AssertionError("Unexpected javap output: " + out);
 109          }
 110     }
 111 }