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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8020801
  29  * @summary Restriction on reflective call to MethodHandles.lookup method
  30  * @run main java.base/java.lang.LookupTest
  31  * @run main ReflectiveLookupTest
  32  * @run main/othervm -Dsun.reflect.noInflation=true ReflectiveLookupTest
  33  */
  34 
  35 import java.lang.invoke.*;
  36 import java.lang.invoke.MethodHandles.Lookup;
  37 import java.lang.reflect.Method;
  38 
  39 import static java.lang.invoke.MethodType.*;
  40 
  41 /*
  42  * Lookup object can be obtained statically or reflectively.
  43  */
  44 public class ReflectiveLookupTest {
  45     public static void main(String... args) throws Throwable {
  46         // Get a full power lookup
  47         Lookup lookup1 =  MethodHandles.lookup();
  48         MethodHandle mh1 = lookup1.findStatic(lookup1.lookupClass(),
  49                                               "foo",
  50                                               methodType(String.class));
  51         assertEquals((String) mh1.invokeExact(), foo());
  52 
  53         Method lookupMethod =  MethodHandles.class.getMethod("lookup");
  54         System.out.println("reflection method: " + lookupMethod);
  55         if (!lookupMethod.getName().equals("lookup")) {
  56             throw new RuntimeException("Unexpected name: " + lookupMethod.getName());
  57         }
  58 
  59         // Get a full power Lookup reflectively.
  60         Lookup lookup2 = (Lookup) lookupMethod.invoke(null);
  61         assertEquals(lookup1.lookupClass(), lookup2.lookupClass());
  62         assertEquals(lookup1.lookupModes(), lookup2.lookupModes());
  63         MethodHandle mh2 = lookup2.findStatic(lookup2.lookupClass(),
  64                                              "foo",
  65                                               methodType(String.class));
  66         assertEquals((String) mh2.invokeExact(), foo());
  67     }
  68 
  69     static String foo() {
  70         return "foo!";
  71     }
  72 
  73     static void assertEquals(Object o1, Object o2) {
  74         if (!o1.equals(o2)) {
  75             throw new RuntimeException(o1 + " != " + o2);
  76         }
  77     }
  78 }