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