1 /*
   2  * Copyright (c) 2018, 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 8046171
  27  * @summary Test that private Lookup works for both nestmate-enabled classes
  28  * and legacy classes
  29  * @compile TestPrivateLookup.java
  30  * @run main TestPrivateLookup
  31  * @compile -source 10 -target 10 TestPrivateLookup.java
  32  * @run main TestPrivateLookup noNestmates
  33  */
  34 
  35 // Need the explicit first @compile above so that jtreg forces recompilation
  36 // with latest version. Otherwise compile-on-demand sees the JDK 10 version
  37 // and assumes it is up to date and then runs the test using that version -
  38 // which fails.
  39 
  40 import java.lang.invoke.*;
  41 import static java.lang.invoke.MethodHandles.*;
  42 import static java.lang.invoke.MethodType.*;
  43 
  44 public class TestPrivateLookup {
  45 
  46     static boolean compiledForNestmates;
  47 
  48     static class C {
  49         static class D {
  50             private void m() { }
  51         }
  52 
  53         static void test() throws Throwable {
  54             MethodType M_T = MethodType.methodType(void.class);
  55             // Direct lookup from C
  56             Lookup l = lookup();
  57             try {
  58                 MethodHandle mh = l.findVirtual(D.class, "m", M_T);
  59                 if (compiledForNestmates) {
  60                     System.out.println("Lookup of D.m from C succeeded as expected with nestmates");
  61                 }
  62                 else {
  63                     throw new Error("Unexpected success when not compiled for nestmates!");
  64                 }
  65             }
  66             catch (IllegalAccessException iae) {
  67                 if (!compiledForNestmates) {
  68                     System.out.println("Lookup of D.m from C failed as expected without nestmates");
  69                 }
  70                 else {
  71                     throw new Error("Unexpected failure with nestmates", iae);
  72                 }
  73             }
  74             // switch lookup class to D
  75             l = l.in(D.class);
  76             try {
  77                 MethodHandle mh = l.findVirtual(D.class, "m", M_T);
  78                 System.out.println("Lookup of D.m from D succeeded as expected" +
  79                                    " with" + (compiledForNestmates ? "" : "out") +
  80                                    " nestmates");
  81             }
  82             catch (IllegalAccessException iae) {
  83                 throw new Error("Lookup of D.m from D failed", iae);
  84             }
  85         }
  86     }
  87 
  88     public static void main(String[] args) throws Throwable {
  89 
  90         // If there's no nesthost attribute A.getNestHost() == A
  91         compiledForNestmates = C.D.class.getNestHost() == TestPrivateLookup.class;
  92         // sanity check
  93         boolean expectingNestmates = args.length == 0;
  94 
  95         if (compiledForNestmates && !expectingNestmates) {
  96             throw new Error("Test is being run incorrectly: " +
  97                             "nestmates are being used but not expected");
  98         }
  99         if (expectingNestmates && !compiledForNestmates) {
 100             throw new Error("Test is being run incorrectly: " +
 101                             "nestmates are expected but not being used");
 102         }
 103         System.out.println("Testing with" + (expectingNestmates ? "" : "out") +
 104                            " nestmates");
 105 
 106         C.test();
 107     }
 108 }