1 /*
   2  * Copyright (c) 2017, 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 // We compile two versions of the nested StaticIface which contains a call
  25 // to a private interface method. Both versions are compiled to use
  26 // invokeinterface, but one is modified to declare a lower classfile version,
  27 // which makes the use of invokeinterface illegal in that context.
  28 // We run the test twice, each time using a different version of the
  29 // StaticIface class.
  30 
  31 /*
  32  * @test
  33  * @bug 8046171
  34  * @summary Test use of invokeinterface versus invokespecial for private
  35  *          interface method invocation
  36  * @compile TestInvokeInterface.java
  37  * @compile StaticIfaceGood.jcod
  38  * @run main TestInvokeInterface pass
  39  * @compile StaticIfaceError.jcod
  40  * @run main TestInvokeInterface fail
  41  */
  42 
  43 public class TestInvokeInterface {
  44 
  45     static interface StaticIface {
  46 
  47         private void priv_invoke() {
  48             System.out.println("StaticIface::priv_invoke");
  49         }
  50 
  51         default void access_priv(StaticIface o) {
  52             o.priv_invoke();
  53         }
  54     }
  55 
  56     public static void main(String[] args) {
  57         boolean shouldFail = args[0].equals("fail");
  58         String icce = "IncompatibleClassChangeError: private interface method requires invokespecial";
  59         StaticIface intf = new StaticIface() {};
  60         try {
  61             intf.access_priv(new StaticIface(){});
  62             if (shouldFail)
  63                 throw new Error("Do not get expected exception: " + icce);
  64             else
  65                 System.out.println("Invocation succeeded as expected");
  66         }
  67         catch (IncompatibleClassChangeError e) {
  68             if (shouldFail && e.toString().contains(icce))
  69                 System.out.println("Got expected exception: " + e);
  70             else
  71                 throw new Error("Unexpected cause of exception: " + e);
  72         }
  73     }
  74 }