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  * @bug 8132879
  27  * @compile CallSites.jasm
  28  * @run main/othervm -Xverify:all -Xbatch -XX:CompileCommand=dontinline,Test::test* LinkageErrors
  29  */
  30 
  31 import java.lang.invoke.*;
  32 
  33 interface I {
  34     void m1(int i);
  35     static void s1() {}
  36 }
  37 
  38 class A implements I {
  39     public void m1(int i) {}
  40 }
  41 
  42 class X {
  43     public        void m1(int i) {}
  44     public final  void f1(int i) {}
  45     public static void s1(int i) {}
  46 }
  47 
  48 public class LinkageErrors {
  49     final static MethodHandles.Lookup L = MethodHandles.lookup();
  50 
  51     static void test(MethodHandle mh) {
  52         try {
  53             mh.invokeExact();
  54             throw new AssertionError("No exception thrown");
  55         } catch (LinkageError e) {
  56             return; // expected
  57         } catch (AssertionError e) {
  58             throw e; // rethrow
  59         } catch (Throwable e) {
  60             throw new AssertionError("Unexpected exception", e);
  61         }
  62     }
  63 
  64     public static void main(String args[]) throws Throwable {
  65         Class<?> test = Class.forName("CallSites");
  66 
  67         // Non-existent method lookups.
  68         MethodHandle testI1 = L.findStatic(test, "testI1", MethodType.methodType(void.class, I.class));
  69         MethodHandle testX1 = L.findStatic(test, "testX1", MethodType.methodType(void.class, X.class));
  70 
  71         MethodHandle testI1_A    = testI1.bindTo(new A());
  72         MethodHandle testI1_null = testI1.bindTo(null);
  73         MethodHandle testX1_X    = testX1.bindTo(new X());
  74         MethodHandle testX1_null = testX1.bindTo(null);
  75 
  76         // invokestatic of instance methods.
  77         MethodHandle testI2 = L.findStatic(test, "testI2", MethodType.methodType(void.class));
  78         MethodHandle testX2 = L.findStatic(test, "testX2", MethodType.methodType(void.class));
  79 
  80         MethodHandle testI3 = L.findStatic(test, "testI3", MethodType.methodType(void.class, I.class));
  81         MethodHandle testX3 = L.findStatic(test, "testX3", MethodType.methodType(void.class, X.class));
  82 
  83         // Virtual invocation of static methods.
  84         MethodHandle testI3_A    = testI3.bindTo(new A());
  85         MethodHandle testI3_null = testI3.bindTo(null);
  86         MethodHandle testX3_X    = testX3.bindTo(new X());
  87         MethodHandle testX3_null = testX3.bindTo(null);
  88 
  89         for (int i = 0; i < 20_000; i++) {
  90             test(testI1_A);
  91             test(testI1_null);
  92             test(testX1_X);
  93             test(testX1_null);
  94 
  95             test(testI2);
  96             test(testX2);
  97 
  98             test(testI3_A);
  99             test(testI3_null);
 100             test(testX3_X);
 101             test(testX3_null);
 102         }
 103 
 104         System.out.println("TEST PASSED");
 105     }
 106 }