< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandle.java

Print this page
rev 50604 : imported patch jep181-rev1
   1 /*
   2  * Copyright (c) 2008, 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


 233  * Method handle constants are subject to the same link-time access checks
 234  * their corresponding bytecode instructions, and the {@code ldc} instruction
 235  * will throw corresponding linkage errors if the bytecode behaviors would
 236  * throw such errors.
 237  * <p>
 238  * As a corollary of this, access to protected members is restricted
 239  * to receivers only of the accessing class, or one of its subclasses,
 240  * and the accessing class must in turn be a subclass (or package sibling)
 241  * of the protected member's defining class.
 242  * If a method reference refers to a protected non-static method or field
 243  * of a class outside the current package, the receiver argument will
 244  * be narrowed to the type of the accessing class.
 245  * <p>
 246  * When a method handle to a virtual method is invoked, the method is
 247  * always looked up in the receiver (that is, the first argument).
 248  * <p>
 249  * A non-virtual method handle to a specific virtual method implementation
 250  * can also be created.  These do not perform virtual lookup based on
 251  * receiver type.  Such a method handle simulates the effect of
 252  * an {@code invokespecial} instruction to the same method.



 253  *
 254  * <h1>Usage examples</h1>
 255  * Here are some examples of usage:
 256  * <blockquote><pre>{@code
 257 Object x, y; String s; int i;
 258 MethodType mt; MethodHandle mh;
 259 MethodHandles.Lookup lookup = MethodHandles.lookup();
 260 // mt is (char,char)String
 261 mt = MethodType.methodType(String.class, char.class, char.class);
 262 mh = lookup.findVirtual(String.class, "replace", mt);
 263 s = (String) mh.invokeExact("daddy",'d','n');
 264 // invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
 265 assertEquals(s, "nanny");
 266 // weakly typed invocation (using MHs.invoke)
 267 s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
 268 assertEquals(s, "savvy");
 269 // mt is (Object[])List
 270 mt = MethodType.methodType(java.util.List.class, Object[].class);
 271 mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
 272 assert(mh.isVarargsCollector());


   1 /*
   2  * Copyright (c) 2008, 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.  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


 233  * Method handle constants are subject to the same link-time access checks
 234  * their corresponding bytecode instructions, and the {@code ldc} instruction
 235  * will throw corresponding linkage errors if the bytecode behaviors would
 236  * throw such errors.
 237  * <p>
 238  * As a corollary of this, access to protected members is restricted
 239  * to receivers only of the accessing class, or one of its subclasses,
 240  * and the accessing class must in turn be a subclass (or package sibling)
 241  * of the protected member's defining class.
 242  * If a method reference refers to a protected non-static method or field
 243  * of a class outside the current package, the receiver argument will
 244  * be narrowed to the type of the accessing class.
 245  * <p>
 246  * When a method handle to a virtual method is invoked, the method is
 247  * always looked up in the receiver (that is, the first argument).
 248  * <p>
 249  * A non-virtual method handle to a specific virtual method implementation
 250  * can also be created.  These do not perform virtual lookup based on
 251  * receiver type.  Such a method handle simulates the effect of
 252  * an {@code invokespecial} instruction to the same method.
 253  * A non-virtual method handle can also be created to simulate the effect
 254  * of an {@code invokevirtual} or {@code invokeinterface} instruction on
 255  * a private method (as applicable).
 256  *
 257  * <h1>Usage examples</h1>
 258  * Here are some examples of usage:
 259  * <blockquote><pre>{@code
 260 Object x, y; String s; int i;
 261 MethodType mt; MethodHandle mh;
 262 MethodHandles.Lookup lookup = MethodHandles.lookup();
 263 // mt is (char,char)String
 264 mt = MethodType.methodType(String.class, char.class, char.class);
 265 mh = lookup.findVirtual(String.class, "replace", mt);
 266 s = (String) mh.invokeExact("daddy",'d','n');
 267 // invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
 268 assertEquals(s, "nanny");
 269 // weakly typed invocation (using MHs.invoke)
 270 s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
 271 assertEquals(s, "savvy");
 272 // mt is (Object[])List
 273 mt = MethodType.methodType(java.util.List.class, Object[].class);
 274 mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
 275 assert(mh.isVarargsCollector());


< prev index next >