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