2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2011-02-11 01:26:24 -08:00
|
|
|
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
|
2009-05-05 22:40:09 -07:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2009-05-05 22:40:09 -07:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2009-05-05 22:40:09 -07:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
|
2011-03-23 23:02:31 -07:00
|
|
|
package java.lang.invoke;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2010-09-08 18:40:34 -07:00
|
|
|
import java.lang.reflect.*;
|
2011-03-23 23:02:31 -07:00
|
|
|
import sun.invoke.WrapperInstance;
|
|
|
|
import sun.invoke.util.ValueConversions;
|
|
|
|
import sun.invoke.util.VerifyAccess;
|
|
|
|
import sun.invoke.util.Wrapper;
|
2010-01-07 16:16:45 -08:00
|
|
|
import java.util.List;
|
2009-05-05 22:40:09 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import sun.reflect.Reflection;
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandleStatics.*;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
2010-04-30 23:48:23 -07:00
|
|
|
* This class consists exclusively of static methods that operate on or return
|
|
|
|
* method handles. They fall into several categories:
|
2010-01-07 16:16:45 -08:00
|
|
|
* <ul>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <li>Lookup methods which help create method handles for methods and fields.
|
|
|
|
* <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
|
|
|
|
* <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
|
2011-03-23 23:02:31 -07:00
|
|
|
* <li>Wrapper methods which can convert between method handles and interface types.
|
2010-01-07 16:16:45 -08:00
|
|
|
* </ul>
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
|
|
|
* @author John Rose, JSR 292 EG
|
|
|
|
*/
|
|
|
|
public class MethodHandles {
|
|
|
|
|
|
|
|
private MethodHandles() { } // do not instantiate
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
|
2009-05-05 22:40:09 -07:00
|
|
|
static { MethodHandleImpl.initStatics(); }
|
|
|
|
// See IMPL_LOOKUP below.
|
|
|
|
|
|
|
|
//// Method handle creation from ordinary methods.
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Returns a {@link Lookup lookup object} on the caller,
|
2010-04-30 23:48:23 -07:00
|
|
|
* which has the capability to access any method handle that the caller has access to,
|
|
|
|
* including direct method handles to private fields and methods.
|
|
|
|
* This lookup object is a <em>capability</em> which may be delegated to trusted agents.
|
|
|
|
* Do not store it in place where untrusted code can access it.
|
2010-01-07 16:16:45 -08:00
|
|
|
*/
|
2009-05-05 22:40:09 -07:00
|
|
|
public static Lookup lookup() {
|
|
|
|
return new Lookup();
|
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Returns a {@link Lookup lookup object} which is trusted minimally.
|
2010-04-30 23:48:23 -07:00
|
|
|
* It can only be used to create method handles to
|
|
|
|
* publicly accessible fields and methods.
|
2010-09-08 18:40:23 -07:00
|
|
|
* <p>
|
|
|
|
* As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
|
|
|
|
* of this lookup object will be {@link java.lang.Object}.
|
|
|
|
* <p>
|
|
|
|
* The lookup class can be changed to any other class {@code C} using an expression of the form
|
|
|
|
* {@linkplain Lookup#in <code>publicLookup().in(C.class)</code>}.
|
|
|
|
* Since all classes have equal access to public names,
|
|
|
|
* such a change would confer no new access rights.
|
2010-01-07 16:16:45 -08:00
|
|
|
*/
|
|
|
|
public static Lookup publicLookup() {
|
|
|
|
return Lookup.PUBLIC_LOOKUP;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/**
|
2010-04-30 23:48:23 -07:00
|
|
|
* A <em>lookup object</em> is a factory for creating method handles,
|
|
|
|
* when the creation requires access checking.
|
|
|
|
* Method handles do not perform
|
2010-09-08 18:40:23 -07:00
|
|
|
* access checks when they are called, but rather when they are created.
|
2010-04-30 23:48:23 -07:00
|
|
|
* Therefore, method handle access
|
|
|
|
* restrictions must be enforced when a method handle is created.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The caller class against which those restrictions are enforced
|
2010-04-30 23:48:23 -07:00
|
|
|
* is known as the {@linkplain #lookupClass lookup class}.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
|
|
|
* A lookup class which needs to create method handles will call
|
|
|
|
* {@link MethodHandles#lookup MethodHandles.lookup} to create a factory for itself.
|
|
|
|
* When the {@code Lookup} factory object is created, the identity of the lookup class is
|
|
|
|
* determined, and securely stored in the {@code Lookup} object.
|
|
|
|
* The lookup class (or its delegates) may then use factory methods
|
|
|
|
* on the {@code Lookup} object to create method handles for access-checked members.
|
|
|
|
* This includes all methods, constructors, and fields which are allowed to the lookup class,
|
|
|
|
* even private ones.
|
|
|
|
* <p>
|
|
|
|
* The factory methods on a {@code Lookup} object correspond to all major
|
|
|
|
* use cases for methods, constructors, and fields.
|
|
|
|
* Here is a summary of the correspondence between these factory methods and
|
|
|
|
* the behavior the resulting method handles:
|
|
|
|
* <code>
|
|
|
|
* <table border=1 cellpadding=5 summary="lookup method behaviors">
|
|
|
|
* <tr><th>lookup expression</th><th>member</th><th>behavior</th></tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>FT f;</td><td>(T) this.f;</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>static<br>FT f;</td><td>(T) C.f;</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>FT f;</td><td>this.f = x;</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>static<br>FT f;</td><td>C.f = arg;</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>T m(A*);</td><td>(T) this.m(arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>static<br>T m(A*);</td><td>(T) C.m(arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>T m(A*);</td><td>(T) super.m(arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>C(A*);</td><td>(T) new C(arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>(static)?<br>FT f;</td><td>(FT) aField.get(thisOrNull);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>(static)?<br>FT f;</td><td>aField.set(thisOrNull, arg);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>C(A*);</td><td>(C) aConstructor.newInstance(arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
2011-03-23 23:02:31 -07:00
|
|
|
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td>
|
|
|
|
* </tr>
|
|
|
|
* </table>
|
|
|
|
* </code>
|
|
|
|
* Here, the type {@code C} is the class or interface being searched for a member,
|
|
|
|
* documented as a parameter named {@code refc} in the lookup methods.
|
|
|
|
* The method or constructor type {@code MT} is composed from the return type {@code T}
|
|
|
|
* and the sequence of argument types {@code A*}.
|
|
|
|
* Both {@code MT} and the field type {@code FT} are documented as a parameter named {@code type}.
|
|
|
|
* The formal parameter {@code this} stands for the self-reference of type {@code C};
|
|
|
|
* if it is present, it is always the leading argument to the method handle invocation.
|
|
|
|
* The name {@code arg} stands for all the other method handle arguments.
|
|
|
|
* In the code examples for the Core Reflection API, the name {@code thisOrNull}
|
|
|
|
* stands for a null reference if the accessed method or field is static,
|
|
|
|
* and {@code this} otherwise.
|
|
|
|
* The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
|
|
|
|
* for reflective objects corresponding to the given members.
|
|
|
|
* <p>
|
|
|
|
* The equivalence between looked-up method handles and underlying
|
|
|
|
* class members can break down in a few ways:
|
|
|
|
* <ul>
|
|
|
|
* <li>If {@code C} is not symbolically accessible from the lookup class's loader,
|
|
|
|
* the lookup can still succeed, even when there is no equivalent
|
|
|
|
* Java expression or bytecoded constant.
|
|
|
|
* <li>Likewise, if {@code T} or {@code MT}
|
|
|
|
* is not symbolically accessible from the lookup class's loader,
|
|
|
|
* the lookup can still succeed.
|
|
|
|
* For example, lookups for {@code MethodHandle.invokeExact} and
|
|
|
|
* {@code MethodHandle.invokeGeneric} will always succeed, regardless of requested type.
|
2011-02-15 00:16:53 -08:00
|
|
|
* <li>If there is a security manager installed, it can forbid the lookup
|
|
|
|
* on various grounds (<a href="#secmgr">see below</a>).
|
|
|
|
* By contrast, the {@code ldc} instruction is not subject to
|
|
|
|
* security manager checks.
|
2011-02-11 01:26:28 -08:00
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* <h3><a name="access"></a>Access checking</h3>
|
|
|
|
* Access checks are applied in the factory methods of {@code Lookup},
|
|
|
|
* when a method handle is created.
|
|
|
|
* This is a key difference from the Core Reflection API, since
|
|
|
|
* {@link java.lang.reflect.Method#invoke Method.invoke}
|
|
|
|
* performs access checking against every caller, on every call.
|
|
|
|
* <p>
|
|
|
|
* All access checks start from a {@code Lookup} object, which
|
|
|
|
* compares its recorded lookup class against all requests to
|
|
|
|
* create method handles.
|
|
|
|
* A single {@code Lookup} object can be used to create any number
|
2009-05-05 22:40:09 -07:00
|
|
|
* of access-checked method handles, all checked against a single
|
|
|
|
* lookup class.
|
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* A {@code Lookup} object can be shared with other trusted code,
|
|
|
|
* such as a metaobject protocol.
|
|
|
|
* A shared {@code Lookup} object delegates the capability
|
|
|
|
* to create method handles on private members of the lookup class.
|
|
|
|
* Even if privileged code uses the {@code Lookup} object,
|
|
|
|
* the access checking is confined to the privileges of the
|
|
|
|
* original lookup class.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2010-04-30 23:48:23 -07:00
|
|
|
* A lookup can fail, because
|
2009-05-05 22:40:09 -07:00
|
|
|
* the containing class is not accessible to the lookup class, or
|
|
|
|
* because the desired class member is missing, or because the
|
|
|
|
* desired class member is not accessible to the lookup class.
|
2011-02-11 01:26:32 -08:00
|
|
|
* In any of these cases, a {@code ReflectiveOperationException} will be
|
|
|
|
* thrown from the attempted lookup. The exact class will be one of
|
|
|
|
* the following:
|
|
|
|
* <ul>
|
|
|
|
* <li>NoSuchMethodException — if a method is requested but does not exist
|
|
|
|
* <li>NoSuchFieldException — if a field is requested but does not exist
|
|
|
|
* <li>IllegalAccessException — if the member exists but an access check fails
|
|
|
|
* </ul>
|
2010-09-08 18:40:23 -07:00
|
|
|
* <p>
|
2009-05-05 22:40:09 -07:00
|
|
|
* In general, the conditions under which a method handle may be
|
2011-02-11 01:26:28 -08:00
|
|
|
* looked up for a method {@code M} are exactly equivalent to the conditions
|
|
|
|
* under which the lookup class could have compiled and resolved a call to {@code M}.
|
|
|
|
* And the effect of invoking the method handle resulting from the lookup
|
|
|
|
* is exactly equivalent to executing the compiled and resolved call to {@code M}.
|
2011-02-11 01:26:24 -08:00
|
|
|
* The same point is true of fields and constructors.
|
2010-10-30 21:08:23 -07:00
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* In some cases, access between nested classes is obtained by the Java compiler by creating
|
2010-09-08 18:40:23 -07:00
|
|
|
* an wrapper method to access a private method of another class
|
|
|
|
* in the same top-level declaration.
|
2010-10-30 21:08:23 -07:00
|
|
|
* For example, a nested class {@code C.D}
|
2010-09-08 18:40:23 -07:00
|
|
|
* can access private members within other related classes such as
|
2010-10-30 21:08:23 -07:00
|
|
|
* {@code C}, {@code C.D.E}, or {@code C.B},
|
|
|
|
* but the Java compiler may need to generate wrapper methods in
|
|
|
|
* those related classes. In such cases, a {@code Lookup} object on
|
|
|
|
* {@code C.E} would be unable to those private members.
|
|
|
|
* A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
|
|
|
|
* which can transform a lookup on {@code C.E} into one on any of those other
|
|
|
|
* classes, without special elevation of privilege.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
2011-02-15 00:16:53 -08:00
|
|
|
* Although bytecode instructions can only refer to classes in
|
|
|
|
* a related class loader, this API can search for methods in any
|
|
|
|
* class, as long as a reference to its {@code Class} object is
|
|
|
|
* available. Such cross-loader references are also possible with the
|
|
|
|
* Core Reflection API, and are impossible to bytecode instructions
|
|
|
|
* such as {@code invokestatic} or {@code getfield}.
|
|
|
|
* There is a {@linkplain java.lang.SecurityManager security manager API}
|
|
|
|
* to allow applications to check such cross-loader references.
|
|
|
|
* These checks apply to both the {@code MethodHandles.Lookup} API
|
|
|
|
* and the Core Reflection API
|
|
|
|
* (as found on {@link java.lang.Class Class}).
|
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* Access checks only apply to named and reflected methods,
|
|
|
|
* constructors, and fields.
|
|
|
|
* Other method handle creation methods, such as
|
|
|
|
* {@link #convertArguments MethodHandles.convertArguments},
|
|
|
|
* do not require any access checks, and are done
|
|
|
|
* with static methods of {@link MethodHandles},
|
|
|
|
* independently of any {@code Lookup} object.
|
2011-02-15 00:16:53 -08:00
|
|
|
*
|
|
|
|
* <h3>Security manager interactions</h3>
|
|
|
|
* <a name="secmgr"></a>
|
|
|
|
* If a security manager is present, member lookups are subject to
|
|
|
|
* additional checks.
|
|
|
|
* From one to four calls are made to the security manager.
|
|
|
|
* Any of these calls can refuse access by throwing a
|
|
|
|
* {@link java.lang.SecurityException SecurityException}.
|
|
|
|
* Define {@code smgr} as the security manager,
|
|
|
|
* {@code refc} as the containing class in which the member
|
|
|
|
* is being sought, and {@code defc} as the class in which the
|
|
|
|
* member is actually defined.
|
|
|
|
* The calls are made according to the following rules:
|
|
|
|
* <ul>
|
|
|
|
* <li>In all cases, {@link SecurityManager#checkMemberAccess
|
|
|
|
* smgr.checkMemberAccess(refc, Member.PUBLIC)} is called.
|
|
|
|
* <li>If the class loader of the lookup class is not
|
|
|
|
* the same as or an ancestor of the class loader of {@code refc},
|
|
|
|
* then {@link SecurityManager#checkPackageAccess
|
|
|
|
* smgr.checkPackageAccess(refcPkg)} is called,
|
|
|
|
* where {@code refcPkg} is the package of {@code refc}.
|
|
|
|
* <li>If the retrieved member is not public,
|
|
|
|
* {@link SecurityManager#checkMemberAccess
|
|
|
|
* smgr.checkMemberAccess(defc, Member.DECLARED)} is called.
|
|
|
|
* (Note that {@code defc} might be the same as {@code refc}.)
|
|
|
|
* <li>If the retrieved member is not public,
|
|
|
|
* and if {@code defc} and {@code refc} are in different class loaders,
|
|
|
|
* and if the class loader of the lookup class is not
|
|
|
|
* the same as or an ancestor of the class loader of {@code defc},
|
|
|
|
* then {@link SecurityManager#checkPackageAccess
|
|
|
|
* smgr.checkPackageAccess(defcPkg)} is called,
|
|
|
|
* where {@code defcPkg} is the package of {@code defc}.
|
|
|
|
* </ul>
|
|
|
|
* In all cases, the requesting class presented to the security
|
|
|
|
* manager will be the lookup class from the current {@code Lookup} object.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static final
|
|
|
|
class Lookup {
|
2010-04-30 23:48:23 -07:00
|
|
|
/** The class on behalf of whom the lookup is being performed. */
|
2009-05-05 22:40:09 -07:00
|
|
|
private final Class<?> lookupClass;
|
|
|
|
|
2010-12-16 15:59:27 -08:00
|
|
|
/** The allowed sorts of members which may be looked up (PUBLIC, etc.). */
|
2010-04-30 23:48:23 -07:00
|
|
|
private final int allowedModes;
|
|
|
|
|
2010-12-16 15:59:27 -08:00
|
|
|
/** A single-bit mask representing {@code public} access,
|
|
|
|
* which may contribute to the result of {@link #lookupModes lookupModes}.
|
|
|
|
* The value, {@code 0x01}, happens to be the same as the value of the
|
|
|
|
* {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}.
|
|
|
|
*/
|
|
|
|
public static final int PUBLIC = Modifier.PUBLIC;
|
|
|
|
|
|
|
|
/** A single-bit mask representing {@code private} access,
|
|
|
|
* which may contribute to the result of {@link #lookupModes lookupModes}.
|
|
|
|
* The value, {@code 0x02}, happens to be the same as the value of the
|
|
|
|
* {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}.
|
|
|
|
*/
|
|
|
|
public static final int PRIVATE = Modifier.PRIVATE;
|
|
|
|
|
|
|
|
/** A single-bit mask representing {@code protected} access,
|
|
|
|
* which may contribute to the result of {@link #lookupModes lookupModes}.
|
|
|
|
* The value, {@code 0x04}, happens to be the same as the value of the
|
|
|
|
* {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}.
|
|
|
|
*/
|
|
|
|
public static final int PROTECTED = Modifier.PROTECTED;
|
|
|
|
|
|
|
|
/** A single-bit mask representing {@code package} access (default access),
|
|
|
|
* which may contribute to the result of {@link #lookupModes lookupModes}.
|
|
|
|
* The value is {@code 0x08}, which does not correspond meaningfully to
|
|
|
|
* any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
|
|
|
|
*/
|
|
|
|
public static final int PACKAGE = Modifier.STATIC;
|
|
|
|
|
|
|
|
private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE);
|
|
|
|
private static final int TRUSTED = -1;
|
2010-04-30 23:48:23 -07:00
|
|
|
|
|
|
|
private static int fixmods(int mods) {
|
|
|
|
mods &= (ALL_MODES - PACKAGE);
|
|
|
|
return (mods != 0) ? mods : PACKAGE;
|
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:28 -08:00
|
|
|
/** Tells which class is performing the lookup. It is this class against
|
2009-05-05 22:40:09 -07:00
|
|
|
* which checks are performed for visibility and access permissions.
|
|
|
|
* <p>
|
2010-09-08 18:40:23 -07:00
|
|
|
* The class implies a maximum level of access permission,
|
|
|
|
* but the permissions may be additionally limited by the bitmask
|
2011-02-11 01:26:28 -08:00
|
|
|
* {@link #lookupModes lookupModes}, which controls whether non-public members
|
2010-09-08 18:40:23 -07:00
|
|
|
* can be accessed.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public Class<?> lookupClass() {
|
|
|
|
return lookupClass;
|
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
// This is just for calling out to MethodHandleImpl.
|
|
|
|
private Class<?> lookupClassOrNull() {
|
|
|
|
return (allowedModes == TRUSTED) ? null : lookupClass;
|
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:28 -08:00
|
|
|
/** Tells which access-protection classes of members this lookup object can produce.
|
2010-12-16 15:59:27 -08:00
|
|
|
* The result is a bit-mask of the bits
|
|
|
|
* {@linkplain #PUBLIC PUBLIC (0x01)},
|
|
|
|
* {@linkplain #PRIVATE PRIVATE (0x02)},
|
|
|
|
* {@linkplain #PROTECTED PROTECTED (0x04)},
|
|
|
|
* and {@linkplain #PACKAGE PACKAGE (0x08)}.
|
2010-10-30 21:08:23 -07:00
|
|
|
* <p>
|
|
|
|
* A freshly-created lookup object
|
2011-03-23 23:02:31 -07:00
|
|
|
* on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class}
|
2010-10-30 21:08:23 -07:00
|
|
|
* has all possible bits set, since the caller class can access all its own members.
|
|
|
|
* A lookup object on a new lookup class
|
2011-03-23 23:02:31 -07:00
|
|
|
* {@linkplain java.lang.invoke.MethodHandles.Lookup#in created from a previous lookup object}
|
2010-10-30 21:08:23 -07:00
|
|
|
* may have some mode bits set to zero.
|
|
|
|
* The purpose of this is to restrict access via the new lookup object,
|
|
|
|
* so that it can access only names which can be reached by the original
|
|
|
|
* lookup object, and also by the new lookup class.
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2010-09-08 18:40:23 -07:00
|
|
|
public int lookupModes() {
|
2010-04-30 23:48:23 -07:00
|
|
|
return allowedModes & ALL_MODES;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/** Embody the current class (the lookupClass) as a lookup class
|
|
|
|
* for method handle creation.
|
|
|
|
* Must be called by from a method in this package,
|
|
|
|
* which in turn is called by a method not in this package.
|
2010-04-30 23:48:23 -07:00
|
|
|
* <p>
|
2009-05-05 22:40:09 -07:00
|
|
|
* Also, don't make it private, lest javac interpose
|
|
|
|
* an access$N method.
|
|
|
|
*/
|
|
|
|
Lookup() {
|
2010-04-30 23:48:23 -07:00
|
|
|
this(getCallerClassAtEntryPoint(), ALL_MODES);
|
|
|
|
// make sure we haven't accidentally picked up a privileged class:
|
|
|
|
checkUnprivilegedlookupClass(lookupClass);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
Lookup(Class<?> lookupClass) {
|
2010-04-30 23:48:23 -07:00
|
|
|
this(lookupClass, ALL_MODES);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Lookup(Class<?> lookupClass, int allowedModes) {
|
2010-01-07 16:16:45 -08:00
|
|
|
this.lookupClass = lookupClass;
|
2010-04-30 23:48:23 -07:00
|
|
|
this.allowedModes = allowedModes;
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Creates a lookup on the specified new lookup class.
|
2010-04-30 23:48:23 -07:00
|
|
|
* The resulting object will report the specified
|
2010-12-16 15:59:27 -08:00
|
|
|
* class as its own {@link #lookupClass lookupClass}.
|
2010-04-30 23:48:23 -07:00
|
|
|
* <p>
|
|
|
|
* However, the resulting {@code Lookup} object is guaranteed
|
|
|
|
* to have no more access capabilities than the original.
|
2010-10-30 21:08:23 -07:00
|
|
|
* In particular, access capabilities can be lost as follows:<ul>
|
2010-04-30 23:48:23 -07:00
|
|
|
* <li>If the new lookup class differs from the old one,
|
|
|
|
* protected members will not be accessible by virtue of inheritance.
|
2010-10-30 21:08:23 -07:00
|
|
|
* (Protected members may continue to be accessible because of package sharing.)
|
2010-04-30 23:48:23 -07:00
|
|
|
* <li>If the new lookup class is in a different package
|
|
|
|
* than the old one, protected and default (package) members will not be accessible.
|
|
|
|
* <li>If the new lookup class is not within the same package member
|
|
|
|
* as the old one, private members will not be accessible.
|
2010-10-30 21:08:23 -07:00
|
|
|
* <li>If the new lookup class is not accessible to the old lookup class,
|
|
|
|
* then no members, not even public members, will be accessible.
|
|
|
|
* (In all other cases, public members will continue to be accessible.)
|
2010-04-30 23:48:23 -07:00
|
|
|
* </ul>
|
2011-02-11 01:26:28 -08:00
|
|
|
*
|
|
|
|
* @param requestedLookupClass the desired lookup class for the new lookup object
|
|
|
|
* @return a lookup object which reports the desired lookup class
|
|
|
|
* @throws NullPointerException if the argument is null
|
2010-01-07 16:16:45 -08:00
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
public Lookup in(Class<?> requestedLookupClass) {
|
|
|
|
requestedLookupClass.getClass(); // null check
|
|
|
|
if (allowedModes == TRUSTED) // IMPL_LOOKUP can make any lookup at all
|
|
|
|
return new Lookup(requestedLookupClass, ALL_MODES);
|
|
|
|
if (requestedLookupClass == this.lookupClass)
|
|
|
|
return this; // keep same capabilities
|
|
|
|
int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
|
|
|
|
if ((newModes & PACKAGE) != 0
|
|
|
|
&& !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
|
|
|
|
newModes &= ~(PACKAGE|PRIVATE);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2010-10-30 21:08:23 -07:00
|
|
|
// Allow nestmate lookups to be created without special privilege:
|
2010-04-30 23:48:23 -07:00
|
|
|
if ((newModes & PRIVATE) != 0
|
|
|
|
&& !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
|
|
|
|
newModes &= ~PRIVATE;
|
|
|
|
}
|
2010-10-30 21:08:23 -07:00
|
|
|
if (newModes == PUBLIC
|
|
|
|
&& !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass)) {
|
|
|
|
// The requested class it not accessible from the lookup class.
|
|
|
|
// No permissions.
|
|
|
|
newModes = 0;
|
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
checkUnprivilegedlookupClass(requestedLookupClass);
|
|
|
|
return new Lookup(requestedLookupClass, newModes);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-01-07 16:16:45 -08:00
|
|
|
// Make sure outer class is initialized first.
|
2011-03-18 00:03:24 -07:00
|
|
|
static { IMPL_NAMES.getClass(); }
|
2010-01-07 16:16:45 -08:00
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/** Version of lookup which is trusted minimally.
|
|
|
|
* It can only be used to create method handles to
|
|
|
|
* publicly accessible members.
|
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, PUBLIC);
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/** Package-private version of lookup which is trusted. */
|
2010-04-30 23:48:23 -07:00
|
|
|
static final Lookup IMPL_LOOKUP = new Lookup(Object.class, TRUSTED);
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
private static void checkUnprivilegedlookupClass(Class<?> lookupClass) {
|
2009-05-12 13:54:22 -07:00
|
|
|
String name = lookupClass.getName();
|
2011-03-23 23:02:31 -07:00
|
|
|
if (name.startsWith("java.lang.invoke."))
|
2009-05-05 22:40:09 -07:00
|
|
|
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
|
|
|
|
}
|
|
|
|
|
2010-12-16 15:59:27 -08:00
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Displays the name of the class from which lookups are to be made.
|
2010-12-16 15:59:27 -08:00
|
|
|
* (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
|
|
|
|
* If there are restrictions on the access permitted to this lookup,
|
|
|
|
* this is indicated by adding a suffix to the class name, consisting
|
2011-02-11 01:26:28 -08:00
|
|
|
* of a slash and a keyword. The keyword represents the strongest
|
|
|
|
* allowed access, and is chosen as follows:
|
2010-12-16 15:59:27 -08:00
|
|
|
* <ul>
|
|
|
|
* <li>If no access is allowed, the suffix is "/noaccess".
|
|
|
|
* <li>If only public access is allowed, the suffix is "/public".
|
|
|
|
* <li>If only public and package access are allowed, the suffix is "/package".
|
|
|
|
* <li>If only public, package, and private access are allowed, the suffix is "/private".
|
|
|
|
* </ul>
|
|
|
|
* If none of the above cases apply, it is the case that full
|
|
|
|
* access (public, package, private, and protected) is allowed.
|
|
|
|
* In this case, no suffix is added.
|
|
|
|
* This is true only of an object obtained originally from
|
2011-03-23 23:02:31 -07:00
|
|
|
* {@link java.lang.invoke.MethodHandles#lookup MethodHandles.lookup}.
|
|
|
|
* Objects created by {@link java.lang.invoke.MethodHandles.Lookup#in Lookup.in}
|
2010-12-16 15:59:27 -08:00
|
|
|
* always have restricted access, and will display a suffix.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
|
|
|
* (It may seem strange that protected access should be
|
|
|
|
* stronger than private access. Viewed independently from
|
|
|
|
* package access, protected access is the first to be lost,
|
|
|
|
* because it requires a direct subclass relationship between
|
|
|
|
* caller and callee.)
|
|
|
|
* @see #in
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2009-05-05 22:40:09 -07:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2010-04-30 23:48:23 -07:00
|
|
|
String cname = lookupClass.getName();
|
|
|
|
switch (allowedModes) {
|
2011-02-11 01:26:28 -08:00
|
|
|
case 0: // no privileges
|
|
|
|
return cname + "/noaccess";
|
2010-04-30 23:48:23 -07:00
|
|
|
case PUBLIC:
|
2010-12-16 15:59:27 -08:00
|
|
|
return cname + "/public";
|
2010-04-30 23:48:23 -07:00
|
|
|
case PUBLIC|PACKAGE:
|
|
|
|
return cname + "/package";
|
2011-02-11 01:26:28 -08:00
|
|
|
case ALL_MODES & ~PROTECTED:
|
|
|
|
return cname + "/private";
|
2010-04-30 23:48:23 -07:00
|
|
|
case ALL_MODES:
|
|
|
|
return cname;
|
2011-02-11 01:26:28 -08:00
|
|
|
case TRUSTED:
|
|
|
|
return "/trusted"; // internal only; not exported
|
|
|
|
default: // Should not happen, but it's a bitfield...
|
|
|
|
cname = cname + "/" + Integer.toHexString(allowedModes);
|
|
|
|
assert(false) : cname;
|
|
|
|
return cname;
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// call this from an entry point method in Lookup with extraFrames=0.
|
|
|
|
private static Class<?> getCallerClassAtEntryPoint() {
|
|
|
|
final int CALLER_DEPTH = 4;
|
|
|
|
// 0: Reflection.getCC, 1: getCallerClassAtEntryPoint,
|
|
|
|
// 2: Lookup.<init>, 3: MethodHandles.*, 4: caller
|
|
|
|
// Note: This should be the only use of getCallerClass in this file.
|
2010-01-07 16:16:45 -08:00
|
|
|
assert(Reflection.getCallerClass(CALLER_DEPTH-1) == MethodHandles.class);
|
2009-05-05 22:40:09 -07:00
|
|
|
return Reflection.getCallerClass(CALLER_DEPTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle for a static method.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will be that of the method.
|
2010-01-07 16:16:45 -08:00
|
|
|
* (Since static methods do not take receivers, there is no
|
|
|
|
* additional receiver argument inserted into the method handle type,
|
2011-02-11 01:26:28 -08:00
|
|
|
* as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
|
2009-05-05 22:40:09 -07:00
|
|
|
* The method and all its argument types must be accessible to the lookup class.
|
|
|
|
* If the method's class has not yet been initialized, that is done
|
|
|
|
* immediately, before the method handle is returned.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set.
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param refc the class from which the method is accessed
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param name the name of the method
|
|
|
|
* @param type the type of the method
|
|
|
|
* @return the desired method handle
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchMethodException if the method does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the method is not {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public
|
2011-02-11 01:26:32 -08:00
|
|
|
MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName method = resolveOrFail(refc, name, type, true);
|
|
|
|
checkMethod(refc, method, true);
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.findMethod(method, false, lookupClassOrNull());
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle for a virtual method.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will be that of the method,
|
2010-04-30 23:48:23 -07:00
|
|
|
* with the receiver type (usually {@code refc}) prepended.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The method and all its argument types must be accessible to the lookup class.
|
|
|
|
* <p>
|
|
|
|
* When called, the handle will treat the first argument as a receiver
|
|
|
|
* and dispatch on the receiver's type to determine which method
|
|
|
|
* implementation to enter.
|
|
|
|
* (The dispatching action is identical with that performed by an
|
|
|
|
* {@code invokevirtual} or {@code invokeinterface} instruction.)
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
|
|
|
* Because of the general equivalence between {@code invokevirtual}
|
|
|
|
* instructions and method handles produced by {@code findVirtual},
|
|
|
|
* if the class is {@code MethodHandle} and the name string is
|
|
|
|
* {@code invokeExact} or {@code invokeGeneric}, the resulting
|
|
|
|
* method handle is equivalent to one produced by
|
2011-03-23 23:02:31 -07:00
|
|
|
* {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
|
|
|
|
* {@link java.lang.invoke.MethodHandles#genericInvoker MethodHandles.genericInvoker}
|
2011-02-11 01:26:28 -08:00
|
|
|
* with the same {@code type} argument.
|
|
|
|
*
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param name the name of the method
|
|
|
|
* @param type the type of the method, with the receiver argument omitted
|
|
|
|
* @return the desired method handle
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchMethodException if the method does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the method is {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName method = resolveOrFail(refc, name, type, false);
|
|
|
|
checkMethod(refc, method, false);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull());
|
2010-04-30 23:48:23 -07:00
|
|
|
return restrictProtectedReceiver(method, mh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which creates an object and initializes it, using
|
2010-04-30 23:48:23 -07:00
|
|
|
* the constructor of the specified type.
|
|
|
|
* The parameter types of the method handle will be those of the constructor,
|
|
|
|
* while the return type will be a reference to the constructor's class.
|
|
|
|
* The constructor and all its argument types must be accessible to the lookup class.
|
|
|
|
* If the constructor's class has not yet been initialized, that is done
|
|
|
|
* immediately, before the method handle is returned.
|
|
|
|
* <p>
|
|
|
|
* Note: The requested type must have a return type of {@code void}.
|
2011-02-11 01:26:28 -08:00
|
|
|
* This is consistent with the JVM's treatment of constructor type descriptors.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the constructor's variable arity modifier bit ({@code 0x0080}) is set.
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
|
|
|
* @param type the type of the method, with the receiver argument omitted, and a void return type
|
|
|
|
* @return the desired method handle
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchMethodException if the constructor does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
String name = "<init>";
|
|
|
|
MemberName ctor = resolveOrFail(refc, name, type, false, false, lookupClassOrNull());
|
|
|
|
assert(ctor.isConstructor());
|
|
|
|
checkAccess(refc, ctor);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle rawMH = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull());
|
|
|
|
MethodHandle allocMH = MethodHandleImpl.makeAllocator(rawMH);
|
2011-02-11 01:26:24 -08:00
|
|
|
return fixVarargs(allocMH, rawMH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a version of MH which matches matchMH w.r.t. isVarargsCollector. */
|
|
|
|
private static MethodHandle fixVarargs(MethodHandle mh, MethodHandle matchMH) {
|
|
|
|
boolean va1 = mh.isVarargsCollector();
|
|
|
|
boolean va2 = matchMH.isVarargsCollector();
|
|
|
|
if (va1 == va2) {
|
|
|
|
return mh;
|
|
|
|
} else if (va2) {
|
|
|
|
MethodType type = mh.type();
|
|
|
|
int arity = type.parameterCount();
|
|
|
|
return mh.asVarargsCollector(type.parameterType(arity-1));
|
|
|
|
} else {
|
|
|
|
throw new InternalError("already varargs, but template is not: "+mh);
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces an early-bound method handle for a virtual method,
|
2009-05-11 21:09:58 -07:00
|
|
|
* as if called from an {@code invokespecial}
|
2009-05-05 22:40:09 -07:00
|
|
|
* instruction from {@code caller}.
|
2009-05-11 21:09:58 -07:00
|
|
|
* The type of the method handle will be that of the method,
|
2009-05-05 22:40:09 -07:00
|
|
|
* with a suitably restricted receiver type (such as {@code caller}) prepended.
|
2009-05-11 21:09:58 -07:00
|
|
|
* The method and all its argument types must be accessible
|
2009-05-05 22:40:09 -07:00
|
|
|
* to the caller.
|
|
|
|
* <p>
|
|
|
|
* When called, the handle will treat the first argument as a receiver,
|
|
|
|
* but will not dispatch on the receiver's type.
|
|
|
|
* (This direct invocation action is identical with that performed by an
|
|
|
|
* {@code invokespecial} instruction.)
|
|
|
|
* <p>
|
|
|
|
* If the explicitly specified caller class is not identical with the
|
2010-10-30 21:08:23 -07:00
|
|
|
* lookup class, or if this lookup object does not have private access
|
|
|
|
* privileges, the access fails.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set.
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
|
|
|
* @param name the name of the method (which must not be "<init>")
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param type the type of the method, with the receiver argument omitted
|
|
|
|
* @param specialCaller the proposed calling class to perform the {@code invokespecial}
|
|
|
|
* @return the desired method handle
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchMethodException if the method does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
public MethodHandle findSpecial(Class<?> refc, String name, MethodType type,
|
2011-02-11 01:26:32 -08:00
|
|
|
Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
checkSpecialCaller(specialCaller);
|
|
|
|
MemberName method = resolveOrFail(refc, name, type, false, false, specialCaller);
|
|
|
|
checkMethod(refc, method, false);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle mh = MethodHandleImpl.findMethod(method, false, specialCaller);
|
2010-04-30 23:48:23 -07:00
|
|
|
return restrictReceiver(method, mh, specialCaller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving read access to a non-static field.
|
2010-04-30 23:48:23 -07:00
|
|
|
* The type of the method handle will have a return type of the field's
|
|
|
|
* value type.
|
2010-06-08 23:08:56 -07:00
|
|
|
* The method handle's single argument will be the instance containing
|
2010-04-30 23:48:23 -07:00
|
|
|
* the field.
|
|
|
|
* Access checking is performed immediately on behalf of the lookup class.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param name the field's name
|
|
|
|
* @param type the field's type
|
|
|
|
* @return a method handle which can load values from the field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchFieldException if the field does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the field is {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
|
2010-06-08 23:08:56 -07:00
|
|
|
return makeAccessor(refc, name, type, false, false);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving write access to a non-static field.
|
2010-04-30 23:48:23 -07:00
|
|
|
* The type of the method handle will have a void return type.
|
2010-06-08 23:08:56 -07:00
|
|
|
* The method handle will take two arguments, the instance containing
|
2010-04-30 23:48:23 -07:00
|
|
|
* the field, and the value to be stored.
|
2010-06-08 23:08:56 -07:00
|
|
|
* The second argument will be of the field's value type.
|
|
|
|
* Access checking is performed immediately on behalf of the lookup class.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
2010-06-08 23:08:56 -07:00
|
|
|
* @param name the field's name
|
|
|
|
* @param type the field's type
|
|
|
|
* @return a method handle which can store values into the field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchFieldException if the field does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the field is {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-06-08 23:08:56 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
|
2010-06-08 23:08:56 -07:00
|
|
|
return makeAccessor(refc, name, type, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving read access to a static field.
|
2010-06-08 23:08:56 -07:00
|
|
|
* The type of the method handle will have a return type of the field's
|
|
|
|
* value type.
|
|
|
|
* The method handle will take no arguments.
|
|
|
|
* Access checking is performed immediately on behalf of the lookup class.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
2010-06-08 23:08:56 -07:00
|
|
|
* @param name the field's name
|
|
|
|
* @param type the field's type
|
|
|
|
* @return a method handle which can load values from the field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchFieldException if the field does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-06-08 23:08:56 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
|
2010-06-08 23:08:56 -07:00
|
|
|
return makeAccessor(refc, name, type, true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving write access to a static field.
|
2010-06-08 23:08:56 -07:00
|
|
|
* The type of the method handle will have a void return type.
|
|
|
|
* The method handle will take a single
|
|
|
|
* argument, of the field's value type, the value to be stored.
|
2010-04-30 23:48:23 -07:00
|
|
|
* Access checking is performed immediately on behalf of the lookup class.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @param refc the class or interface from which the method is accessed
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param name the field's name
|
|
|
|
* @param type the field's type
|
2010-06-08 23:08:56 -07:00
|
|
|
* @return a method handle which can store values into the field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchFieldException if the field does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
|
2010-06-08 23:08:56 -07:00
|
|
|
return makeAccessor(refc, name, type, true, true);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces an early-bound method handle for a non-static method.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The receiver must have a supertype {@code defc} in which a method
|
|
|
|
* of the given name and type is accessible to the lookup class.
|
|
|
|
* The method and all its argument types must be accessible to the lookup class.
|
2010-01-07 16:16:45 -08:00
|
|
|
* The type of the method handle will be that of the method,
|
|
|
|
* without any insertion of an additional receiver parameter.
|
|
|
|
* The given receiver will be bound into the method handle,
|
|
|
|
* so that every call to the method handle will invoke the
|
|
|
|
* requested method on the given receiver.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2011-02-11 01:26:24 -08:00
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set
|
|
|
|
* <em>and</em> the trailing array argument is not the only argument.
|
|
|
|
* (If the trailing array argument is the only argument,
|
|
|
|
* the given receiver value will be bound to it.)
|
|
|
|
* <p>
|
|
|
|
* This is equivalent to the following code:
|
|
|
|
* <blockquote><pre>
|
|
|
|
MethodHandle mh0 = {@link #findVirtual findVirtual}(defc, name, type);
|
|
|
|
MethodHandle mh1 = mh0.{@link MethodHandle#bindTo bindTo}(receiver);
|
|
|
|
MethodType mt1 = mh1.type();
|
|
|
|
if (mh0.isVarargsCollector() && mt1.parameterCount() > 0) {
|
|
|
|
mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
|
|
|
|
return mh1;
|
|
|
|
* </pre></blockquote>
|
2010-01-07 16:16:45 -08:00
|
|
|
* where {@code defc} is either {@code receiver.getClass()} or a super
|
|
|
|
* type of that class, in which the requested method is accessible
|
|
|
|
* to the lookup class.
|
2011-02-11 01:26:24 -08:00
|
|
|
* (Note that {@code bindTo} does not preserve variable arity.)
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param receiver the object from which the method is accessed
|
|
|
|
* @param name the name of the method
|
|
|
|
* @param type the type of the method, with the receiver argument omitted
|
|
|
|
* @return the desired method handle
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NoSuchMethodException if the method does not exist
|
|
|
|
* @throws IllegalAccessException if access checking fails
|
2011-02-15 00:16:53 -08:00
|
|
|
* @exception SecurityException if a security manager is present and it
|
|
|
|
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
Class<? extends Object> refc = receiver.getClass(); // may get NPE
|
|
|
|
MemberName method = resolveOrFail(refc, name, type, false);
|
|
|
|
checkMethod(refc, method, false);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle dmh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull());
|
|
|
|
MethodHandle bmh = MethodHandleImpl.bindReceiver(dmh, receiver);
|
2009-05-05 22:40:09 -07:00
|
|
|
if (bmh == null)
|
2011-03-18 00:03:24 -07:00
|
|
|
throw method.makeAccessException("no access", this);
|
2011-02-11 01:26:24 -08:00
|
|
|
if (dmh.type().parameterCount() == 0)
|
|
|
|
return dmh; // bound the trailing parameter; no varargs possible
|
|
|
|
return fixVarargs(bmh, dmh);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Makes a direct method handle to <i>m</i>, if the lookup class has permission.
|
2009-05-05 22:40:09 -07:00
|
|
|
* If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
|
|
|
|
* If <i>m</i> is virtual, overriding is respected on every call.
|
|
|
|
* Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
|
|
|
|
* The type of the method handle will be that of the method,
|
|
|
|
* with the receiver type prepended (but only if it is non-static).
|
|
|
|
* If the method's {@code accessible} flag is not set,
|
|
|
|
* access checking is performed immediately on behalf of the lookup class.
|
|
|
|
* If <i>m</i> is not public, do not share the resulting handle with untrusted parties.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set.
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param m the reflected method
|
|
|
|
* @return a method handle which can invoke the reflected method
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws IllegalAccessException if access checking fails
|
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle unreflect(Method m) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName method = new MemberName(m);
|
|
|
|
assert(method.isMethod());
|
|
|
|
if (!m.isAccessible()) checkMethod(method.getDeclaringClass(), method, method.isStatic());
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull());
|
2010-04-30 23:48:23 -07:00
|
|
|
if (!m.isAccessible()) mh = restrictProtectedReceiver(method, mh);
|
|
|
|
return mh;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle for a reflected method.
|
2009-05-05 22:40:09 -07:00
|
|
|
* It will bypass checks for overriding methods on the receiver,
|
2010-04-30 23:48:23 -07:00
|
|
|
* as if by a {@code invokespecial} instruction from within the {@code specialCaller}.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will be that of the method,
|
2010-04-30 23:48:23 -07:00
|
|
|
* with the special caller type prepended (and <em>not</em> the receiver of the method).
|
2009-05-05 22:40:09 -07:00
|
|
|
* If the method's {@code accessible} flag is not set,
|
|
|
|
* access checking is performed immediately on behalf of the lookup class,
|
|
|
|
* as if {@code invokespecial} instruction were being linked.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the method's variable arity modifier bit ({@code 0x0080}) is set.
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param m the reflected method
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param specialCaller the class nominally calling the method
|
2009-05-05 22:40:09 -07:00
|
|
|
* @return a method handle which can invoke the reflected method
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws IllegalAccessException if access checking fails
|
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
checkSpecialCaller(specialCaller);
|
|
|
|
MemberName method = new MemberName(m);
|
|
|
|
assert(method.isMethod());
|
|
|
|
// ignore m.isAccessible: this is a new kind of access
|
|
|
|
checkMethod(m.getDeclaringClass(), method, false);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle mh = MethodHandleImpl.findMethod(method, false, lookupClassOrNull());
|
2010-04-30 23:48:23 -07:00
|
|
|
return restrictReceiver(method, mh, specialCaller);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle for a reflected constructor.
|
2010-01-07 16:16:45 -08:00
|
|
|
* The type of the method handle will be that of the constructor,
|
|
|
|
* with the return type changed to the declaring class.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The method handle will perform a {@code newInstance} operation,
|
|
|
|
* creating a new instance of the constructor's class on the
|
|
|
|
* arguments passed to the method handle.
|
|
|
|
* <p>
|
|
|
|
* If the constructor's {@code accessible} flag is not set,
|
2010-01-07 16:16:45 -08:00
|
|
|
* access checking is performed immediately on behalf of the lookup class.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
|
|
|
* The returned method handle will have
|
|
|
|
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
|
|
|
|
* the constructor's variable arity modifier bit ({@code 0x0080}) is set.
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param c the reflected constructor
|
2009-05-05 22:40:09 -07:00
|
|
|
* @return a method handle which can invoke the reflected constructor
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws IllegalAccessException if access checking fails
|
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle unreflectConstructor(Constructor c) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName ctor = new MemberName(c);
|
|
|
|
assert(ctor.isConstructor());
|
|
|
|
if (!c.isAccessible()) checkAccess(c.getDeclaringClass(), ctor);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle rawCtor = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull());
|
|
|
|
MethodHandle allocator = MethodHandleImpl.makeAllocator(rawCtor);
|
2011-02-11 01:26:24 -08:00
|
|
|
return fixVarargs(allocator, rawCtor);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving read access to a reflected field.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will have a return type of the field's
|
2010-01-07 16:16:45 -08:00
|
|
|
* value type.
|
|
|
|
* If the field is static, the method handle will take no arguments.
|
|
|
|
* Otherwise, its single argument will be the instance containing
|
|
|
|
* the field.
|
2011-03-23 23:02:31 -07:00
|
|
|
* If the field's {@code accessible} flag is not set,
|
2009-05-05 22:40:09 -07:00
|
|
|
* access checking is performed immediately on behalf of the lookup class.
|
|
|
|
* @param f the reflected field
|
|
|
|
* @return a method handle which can load values from the reflected field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws IllegalAccessException if access checking fails
|
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle unreflectGetter(Field f) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
return makeAccessor(f.getDeclaringClass(), new MemberName(f), f.isAccessible(), false);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving write access to a reflected field.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will have a void return type.
|
2010-01-07 16:16:45 -08:00
|
|
|
* If the field is static, the method handle will take a single
|
|
|
|
* argument, of the field's value type, the value to be stored.
|
|
|
|
* Otherwise, the two arguments will be the instance containing
|
|
|
|
* the field, and the value to be stored.
|
2011-03-23 23:02:31 -07:00
|
|
|
* If the field's {@code accessible} flag is not set,
|
2009-05-05 22:40:09 -07:00
|
|
|
* access checking is performed immediately on behalf of the lookup class.
|
|
|
|
* @param f the reflected field
|
|
|
|
* @return a method handle which can store values into the reflected field
|
2011-02-11 01:26:32 -08:00
|
|
|
* @throws IllegalAccessException if access checking fails
|
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:32 -08:00
|
|
|
public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
return makeAccessor(f.getDeclaringClass(), new MemberName(f), f.isAccessible(), true);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
/// Helper methods, all package-private.
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
MemberName resolveOrFail(Class<?> refc, String name, Class<?> type, boolean isStatic) throws NoSuchFieldException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
checkSymbolicClass(refc); // do this before attempting to resolve
|
2011-02-11 01:26:32 -08:00
|
|
|
name.getClass(); type.getClass(); // NPE
|
2010-04-30 23:48:23 -07:00
|
|
|
int mods = (isStatic ? Modifier.STATIC : 0);
|
2011-02-11 01:26:32 -08:00
|
|
|
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull(),
|
|
|
|
NoSuchFieldException.class);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
MemberName resolveOrFail(Class<?> refc, String name, MethodType type, boolean isStatic) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
checkSymbolicClass(refc); // do this before attempting to resolve
|
2011-02-11 01:26:32 -08:00
|
|
|
name.getClass(); type.getClass(); // NPE
|
2010-04-30 23:48:23 -07:00
|
|
|
int mods = (isStatic ? Modifier.STATIC : 0);
|
2011-02-11 01:26:32 -08:00
|
|
|
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull(),
|
|
|
|
NoSuchMethodException.class);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName resolveOrFail(Class<?> refc, String name, MethodType type, boolean isStatic,
|
2011-02-11 01:26:32 -08:00
|
|
|
boolean searchSupers, Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
checkSymbolicClass(refc); // do this before attempting to resolve
|
2011-02-11 01:26:32 -08:00
|
|
|
name.getClass(); type.getClass(); // NPE
|
2010-04-30 23:48:23 -07:00
|
|
|
int mods = (isStatic ? Modifier.STATIC : 0);
|
2011-02-11 01:26:32 -08:00
|
|
|
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), searchSupers, specialCaller,
|
|
|
|
NoSuchMethodException.class);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
void checkSymbolicClass(Class<?> refc) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
Class<?> caller = lookupClassOrNull();
|
|
|
|
if (caller != null && !VerifyAccess.isClassAccessible(refc, caller))
|
2011-03-18 00:03:24 -07:00
|
|
|
throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
void checkMethod(Class<?> refc, MemberName m, boolean wantStatic) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
String message;
|
|
|
|
if (m.isConstructor())
|
|
|
|
message = "expected a method, not a constructor";
|
|
|
|
else if (!m.isMethod())
|
|
|
|
message = "expected a method";
|
|
|
|
else if (wantStatic != m.isStatic())
|
|
|
|
message = wantStatic ? "expected a static method" : "expected a non-static method";
|
|
|
|
else
|
|
|
|
{ checkAccess(refc, m); return; }
|
2011-03-18 00:03:24 -07:00
|
|
|
throw m.makeAccessException(message, this);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
void checkAccess(Class<?> refc, MemberName m) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
int allowedModes = this.allowedModes;
|
|
|
|
if (allowedModes == TRUSTED) return;
|
|
|
|
int mods = m.getModifiers();
|
2010-10-30 21:08:23 -07:00
|
|
|
if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
|
2010-04-30 23:48:23 -07:00
|
|
|
return; // common case
|
|
|
|
int requestedModes = fixmods(mods); // adjust 0 => PACKAGE
|
|
|
|
if ((requestedModes & allowedModes) != 0
|
|
|
|
&& VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
|
|
|
|
mods, lookupClass()))
|
|
|
|
return;
|
|
|
|
if (((requestedModes & ~allowedModes) & PROTECTED) != 0
|
|
|
|
&& VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
|
|
|
|
// Protected members can also be checked as if they were package-private.
|
|
|
|
return;
|
2011-03-18 00:03:24 -07:00
|
|
|
throw m.makeAccessException(accessFailedMessage(refc, m), this);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
String accessFailedMessage(Class<?> refc, MemberName m) {
|
|
|
|
Class<?> defc = m.getDeclaringClass();
|
|
|
|
int mods = m.getModifiers();
|
2011-02-11 01:26:32 -08:00
|
|
|
// check the class first:
|
|
|
|
boolean classOK = (Modifier.isPublic(defc.getModifiers()) &&
|
|
|
|
(defc == refc ||
|
|
|
|
Modifier.isPublic(refc.getModifiers())));
|
|
|
|
if (!classOK && (allowedModes & PACKAGE) != 0) {
|
|
|
|
classOK = (VerifyAccess.isClassAccessible(defc, lookupClass()) &&
|
|
|
|
(defc == refc ||
|
|
|
|
VerifyAccess.isClassAccessible(refc, lookupClass())));
|
|
|
|
}
|
|
|
|
if (!classOK)
|
2010-04-30 23:48:23 -07:00
|
|
|
return "class is not public";
|
|
|
|
if (Modifier.isPublic(mods))
|
|
|
|
return "access to public member failed"; // (how?)
|
|
|
|
if (Modifier.isPrivate(mods))
|
|
|
|
return "member is private";
|
|
|
|
if (Modifier.isProtected(mods))
|
|
|
|
return "member is protected";
|
|
|
|
return "member is private to package";
|
|
|
|
}
|
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
private static final boolean ALLOW_NESTMATE_ACCESS = false;
|
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
void checkSpecialCaller(Class<?> specialCaller) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
if (allowedModes == TRUSTED) return;
|
2010-10-30 21:08:23 -07:00
|
|
|
if ((allowedModes & PRIVATE) == 0
|
|
|
|
|| (specialCaller != lookupClass()
|
|
|
|
&& !(ALLOW_NESTMATE_ACCESS &&
|
|
|
|
VerifyAccess.isSamePackageMember(specialCaller, lookupClass()))))
|
2011-03-18 00:03:24 -07:00
|
|
|
throw new MemberName(specialCaller).
|
|
|
|
makeAccessException("no private access for invokespecial", this);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:32 -08:00
|
|
|
MethodHandle restrictProtectedReceiver(MemberName method, MethodHandle mh) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
// The accessing class only has the right to use a protected member
|
|
|
|
// on itself or a subclass. Enforce that restriction, from JVMS 5.4.4, etc.
|
|
|
|
if (!method.isProtected() || method.isStatic()
|
|
|
|
|| allowedModes == TRUSTED
|
2010-10-30 21:08:23 -07:00
|
|
|
|| method.getDeclaringClass() == lookupClass()
|
|
|
|
|| (ALLOW_NESTMATE_ACCESS &&
|
|
|
|
VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
|
2010-04-30 23:48:23 -07:00
|
|
|
return mh;
|
|
|
|
else
|
|
|
|
return restrictReceiver(method, mh, lookupClass());
|
|
|
|
}
|
2011-02-11 01:26:32 -08:00
|
|
|
MethodHandle restrictReceiver(MemberName method, MethodHandle mh, Class<?> caller) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
assert(!method.isStatic());
|
|
|
|
Class<?> defc = method.getDeclaringClass(); // receiver type of mh is too wide
|
|
|
|
if (defc.isInterface() || !defc.isAssignableFrom(caller)) {
|
2011-03-18 00:03:24 -07:00
|
|
|
throw method.makeAccessException("caller class must be a subclass below the method", caller);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
MethodType rawType = mh.type();
|
|
|
|
if (rawType.parameterType(0) == caller) return mh;
|
|
|
|
MethodType narrowType = rawType.changeParameterType(0, caller);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle narrowMH = MethodHandleImpl.convertArguments(mh, narrowType, rawType, null);
|
2011-02-11 01:26:24 -08:00
|
|
|
return fixVarargs(narrowMH, mh);
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MethodHandle makeAccessor(Class<?> refc, String name, Class<?> type,
|
2011-02-11 01:26:32 -08:00
|
|
|
boolean isStatic, boolean isSetter) throws NoSuchFieldException, IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
MemberName field = resolveOrFail(refc, name, type, isStatic);
|
|
|
|
if (isStatic != field.isStatic())
|
2011-03-18 00:03:24 -07:00
|
|
|
throw field.makeAccessException(isStatic
|
|
|
|
? "expected a static field"
|
|
|
|
: "expected a non-static field", this);
|
2010-04-30 23:48:23 -07:00
|
|
|
return makeAccessor(refc, field, false, isSetter);
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodHandle makeAccessor(Class<?> refc, MemberName field,
|
2011-02-11 01:26:32 -08:00
|
|
|
boolean trusted, boolean isSetter) throws IllegalAccessException {
|
2010-04-30 23:48:23 -07:00
|
|
|
assert(field.isField());
|
|
|
|
if (trusted)
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.accessField(field, isSetter, lookupClassOrNull());
|
2010-04-30 23:48:23 -07:00
|
|
|
checkAccess(refc, field);
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle mh = MethodHandleImpl.accessField(field, isSetter, lookupClassOrNull());
|
2010-04-30 23:48:23 -07:00
|
|
|
return restrictProtectedReceiver(field, mh);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving read access to elements of an array.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will have a return type of the array's
|
|
|
|
* element type. Its first argument will be the array type,
|
|
|
|
* and the second will be {@code int}.
|
|
|
|
* @param arrayClass an array type
|
|
|
|
* @return a method handle which can load values from the given array type
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
* @throws IllegalArgumentException if arrayClass is not an array type
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException {
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.accessArrayElement(arrayClass, false);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle giving write access to elements of an array.
|
2009-05-05 22:40:09 -07:00
|
|
|
* The type of the method handle will have a void return type.
|
|
|
|
* Its last argument will be the array's element type.
|
|
|
|
* The first and second arguments will be the array type and int.
|
|
|
|
* @return a method handle which can store values into the array type
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
* @throws IllegalArgumentException if arrayClass is not an array type
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException {
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.accessArrayElement(arrayClass, true);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// method handle invocation (reflective style)
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which will invoke any method handle of the
|
2010-10-30 21:08:23 -07:00
|
|
|
* given {@code type} on a standard set of {@code Object} type arguments
|
2010-01-07 16:16:45 -08:00
|
|
|
* and a single trailing {@code Object[]} array.
|
|
|
|
* The resulting invoker will be a method handle with the following
|
|
|
|
* arguments:
|
|
|
|
* <ul>
|
|
|
|
* <li>a single {@code MethodHandle} target
|
|
|
|
* <li>zero or more {@code Object} values (counted by {@code objectArgCount})
|
|
|
|
* <li>an {@code Object[]} array containing more arguments
|
|
|
|
* </ul>
|
2010-10-30 21:08:23 -07:00
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* The invoker will behave like a call to {@link MethodHandle#invokeGeneric invokeGeneric} with
|
2010-10-30 21:08:23 -07:00
|
|
|
* the indicated {@code type}.
|
|
|
|
* That is, if the target is exactly of the given {@code type}, it will behave
|
2011-02-11 01:26:28 -08:00
|
|
|
* like {@code invokeExact}; otherwise it behave as if {@link MethodHandle#asType asType}
|
2010-10-30 21:08:23 -07:00
|
|
|
* is used to convert the target to the required {@code type}.
|
|
|
|
* <p>
|
|
|
|
* The type of the returned invoker will not be the given {@code type}, but rather
|
|
|
|
* will have all parameter and return types replaced by {@code Object}, except for
|
|
|
|
* the last parameter type, which will be the array type {@code Object[]}.
|
|
|
|
* <p>
|
|
|
|
* Before invoking its target, the invoker will spread the varargs array, apply
|
|
|
|
* reference casts as necessary, and unbox and widen primitive arguments.
|
2010-01-07 16:16:45 -08:00
|
|
|
* The return value of the invoker will be an {@code Object} reference,
|
|
|
|
* boxing a primitive value if the original type returns a primitive,
|
|
|
|
* and always null if the original type returns void.
|
|
|
|
* <p>
|
|
|
|
* This method is equivalent to the following code (though it may be more efficient):
|
|
|
|
* <p><blockquote><pre>
|
2011-02-11 01:26:28 -08:00
|
|
|
MethodHandle invoker = MethodHandles.genericInvoker(type);
|
2011-02-11 01:26:24 -08:00
|
|
|
int spreadArgCount = type.parameterCount - objectArgCount;
|
|
|
|
invoker = invoker.asSpreader(Object[].class, spreadArgCount);
|
|
|
|
return invoker;
|
2010-01-07 16:16:45 -08:00
|
|
|
* </pre></blockquote>
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
|
|
|
* This method throws no reflective or security exceptions.
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param type the desired target type
|
|
|
|
* @param objectArgCount number of fixed (non-varargs) {@code Object} arguments
|
|
|
|
* @return a method handle suitable for invoking any method handle of the given type
|
|
|
|
*/
|
|
|
|
static public
|
2011-02-11 01:26:24 -08:00
|
|
|
MethodHandle spreadInvoker(MethodType type, int objectArgCount) {
|
2010-01-07 16:16:45 -08:00
|
|
|
if (objectArgCount < 0 || objectArgCount > type.parameterCount())
|
|
|
|
throw new IllegalArgumentException("bad argument count "+objectArgCount);
|
2011-03-18 00:03:24 -07:00
|
|
|
return type.invokers().spreadInvoker(objectArgCount);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a special <em>invoker method handle</em> which can be used to
|
|
|
|
* invoke any method handle of the given type, as if by {@code invokeExact}.
|
|
|
|
* The resulting invoker will have a type which is
|
2009-05-05 22:40:09 -07:00
|
|
|
* exactly equal to the desired type, except that it will accept
|
|
|
|
* an additional leading argument of type {@code MethodHandle}.
|
|
|
|
* <p>
|
2010-01-07 16:16:45 -08:00
|
|
|
* This method is equivalent to the following code (though it may be more efficient):
|
|
|
|
* <p><blockquote><pre>
|
2011-02-11 01:26:28 -08:00
|
|
|
publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)
|
2010-01-07 16:16:45 -08:00
|
|
|
* </pre></blockquote>
|
2011-02-11 01:26:28 -08:00
|
|
|
*
|
|
|
|
* <p style="font-size:smaller;">
|
|
|
|
* <em>Discussion:</em>
|
|
|
|
* Invoker method handles can be useful when working with variable method handles
|
|
|
|
* of unknown types.
|
|
|
|
* For example, to emulate an {@code invokeExact} call to a variable method
|
|
|
|
* handle {@code M}, extract its type {@code T},
|
|
|
|
* look up the invoker method {@code X} for {@code T},
|
|
|
|
* and call the invoker method, as {@code X.invokeGeneric(T, A...)}.
|
|
|
|
* (It would not work to call {@code X.invokeExact}, since the type {@code T}
|
|
|
|
* is unknown.)
|
|
|
|
* If spreading, collecting, or other argument transformations are required,
|
|
|
|
* they can be applied once to the invoker {@code X} and reused on many {@code M}
|
|
|
|
* method handle values, as long as they are compatible with the type of {@code X}.
|
|
|
|
* <p>
|
|
|
|
* <em>(Note: The invoker method is not available via the Core Reflection API.
|
|
|
|
* An attempt to call {@linkplain java.lang.reflect.Method#invoke Method.invoke}
|
|
|
|
* on the declared {@code invokeExact} or {@code invokeGeneric} method will raise an
|
|
|
|
* {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
|
|
|
|
* <p>
|
|
|
|
* This method throws no reflective or security exceptions.
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param type the desired target type
|
|
|
|
* @return a method handle suitable for invoking any method handle of the given type
|
|
|
|
*/
|
|
|
|
static public
|
|
|
|
MethodHandle exactInvoker(MethodType type) {
|
2011-03-18 00:03:24 -07:00
|
|
|
return type.invokers().exactInvoker();
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:26:28 -08:00
|
|
|
/**
|
|
|
|
* Produces a special <em>invoker method handle</em> which can be used to
|
|
|
|
* invoke any method handle of the given type, as if by {@code invokeGeneric}.
|
|
|
|
* The resulting invoker will have a type which is
|
|
|
|
* exactly equal to the desired type, except that it will accept
|
|
|
|
* an additional leading argument of type {@code MethodHandle}.
|
|
|
|
* <p>
|
|
|
|
* Before invoking its target, the invoker will apply reference casts as
|
|
|
|
* necessary and unbox and widen primitive arguments, as if by {@link #convertArguments convertArguments}.
|
|
|
|
* The return value of the invoker will be an {@code Object} reference,
|
|
|
|
* boxing a primitive value if the original type returns a primitive,
|
|
|
|
* and always null if the original type returns void.
|
|
|
|
* <p>
|
|
|
|
* This method is equivalent to the following code (though it may be more efficient):
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
publicLookup().findVirtual(MethodHandle.class, "invokeGeneric", type)
|
|
|
|
* </pre></blockquote>
|
|
|
|
* <p>
|
|
|
|
* This method throws no reflective or security exceptions.
|
|
|
|
* @param type the desired target type
|
|
|
|
* @return a method handle suitable for invoking any method handle convertible to the given type
|
|
|
|
*/
|
|
|
|
static public
|
|
|
|
MethodHandle genericInvoker(MethodType type) {
|
2011-03-18 00:03:24 -07:00
|
|
|
return type.invokers().genericInvoker();
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform value checking, exactly as if for an adapted method handle.
|
|
|
|
* It is assumed that the given value is either null, of type T0,
|
|
|
|
* or (if T0 is primitive) of the wrapper type corresponding to T0.
|
|
|
|
* The following checks and conversions are made:
|
|
|
|
* <ul>
|
|
|
|
* <li>If T0 and T1 are references, then a cast to T1 is applied.
|
|
|
|
* (The types do not need to be related in any particular way.)
|
|
|
|
* <li>If T0 and T1 are primitives, then a widening or narrowing
|
|
|
|
* conversion is applied, if one exists.
|
|
|
|
* <li>If T0 is a primitive and T1 a reference, and
|
|
|
|
* T0 has a wrapper type TW, a boxing conversion to TW is applied,
|
|
|
|
* possibly followed by a reference conversion.
|
|
|
|
* T1 must be TW or a supertype.
|
|
|
|
* <li>If T0 is a reference and T1 a primitive, and
|
|
|
|
* T1 has a wrapper type TW, an unboxing conversion is applied,
|
|
|
|
* possibly preceded by a reference conversion.
|
|
|
|
* T0 must be TW or a supertype.
|
|
|
|
* <li>If T1 is void, the return value is discarded
|
|
|
|
* <li>If T0 is void and T1 a reference, a null value is introduced.
|
|
|
|
* <li>If T0 is void and T1 a primitive, a zero value is introduced.
|
|
|
|
* </ul>
|
|
|
|
* If the value is discarded, null will be returned.
|
|
|
|
* @param valueType
|
|
|
|
* @param value
|
|
|
|
* @return the value, converted if necessary
|
|
|
|
* @throws java.lang.ClassCastException if a cast fails
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
<T0, T1> T1 checkValue(Class<T0> t0, Class<T1> t1, Object value)
|
|
|
|
throws ClassCastException
|
|
|
|
{
|
|
|
|
if (t0 == t1) {
|
|
|
|
// no conversion needed; just reassert the same type
|
|
|
|
if (t0.isPrimitive())
|
|
|
|
return Wrapper.asPrimitiveType(t1).cast(value);
|
|
|
|
else
|
2010-10-30 21:08:23 -07:00
|
|
|
return Wrapper.OBJECT.convert(value, t1);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
boolean prim0 = t0.isPrimitive(), prim1 = t1.isPrimitive();
|
|
|
|
if (!prim0) {
|
|
|
|
// check contract with caller
|
2010-10-30 21:08:23 -07:00
|
|
|
Wrapper.OBJECT.convert(value, t0);
|
2009-05-05 22:40:09 -07:00
|
|
|
if (!prim1) {
|
2010-10-30 21:08:23 -07:00
|
|
|
return Wrapper.OBJECT.convert(value, t1);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
// convert reference to primitive by unboxing
|
|
|
|
Wrapper w1 = Wrapper.forPrimitiveType(t1);
|
2010-10-30 21:08:23 -07:00
|
|
|
return w1.convert(value, t1);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
// check contract with caller:
|
|
|
|
Wrapper.asWrapperType(t0).cast(value);
|
|
|
|
Wrapper w1 = Wrapper.forPrimitiveType(t1);
|
2010-10-30 21:08:23 -07:00
|
|
|
return w1.convert(value, t1);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
Object checkValue(Class<?> T1, Object value)
|
|
|
|
throws ClassCastException
|
|
|
|
{
|
|
|
|
Class<?> T0;
|
|
|
|
if (value == null)
|
|
|
|
T0 = Object.class;
|
|
|
|
else
|
|
|
|
T0 = value.getClass();
|
|
|
|
return checkValue(T0, T1, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// method handle modification (creation from other method handles)
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which adapts the type of the
|
2010-01-07 16:16:45 -08:00
|
|
|
* given method handle to a new type by pairwise argument conversion.
|
|
|
|
* The original type and new type must have the same number of arguments.
|
2010-10-30 21:08:23 -07:00
|
|
|
* The resulting method handle is guaranteed to report a type
|
2010-01-07 16:16:45 -08:00
|
|
|
* which is equal to the desired new type.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
|
|
|
* If the original type and new type are equal, returns target.
|
|
|
|
* <p>
|
|
|
|
* The following conversions are applied as needed both to
|
|
|
|
* arguments and return types. Let T0 and T1 be the differing
|
|
|
|
* new and old parameter types (or old and new return types)
|
|
|
|
* for corresponding values passed by the new and old method types.
|
|
|
|
* Given those types T0, T1, one of the following conversions is applied
|
|
|
|
* if possible:
|
|
|
|
* <ul>
|
2010-10-30 21:08:23 -07:00
|
|
|
* <li>If T0 and T1 are references, then a cast to T1 is applied.
|
2010-01-07 16:16:45 -08:00
|
|
|
* (The types do not need to be related in any particular way.)
|
2010-10-30 21:08:23 -07:00
|
|
|
* <li>If T0 and T1 are primitives, then a Java method invocation
|
|
|
|
* conversion (JLS 5.3) is applied, if one exists.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <li>If T0 is a primitive and T1 a reference, a boxing
|
|
|
|
* conversion is applied if one exists, possibly followed by
|
2010-10-30 21:08:23 -07:00
|
|
|
* a reference conversion to a superclass.
|
2009-05-05 22:40:09 -07:00
|
|
|
* T1 must be a wrapper class or a supertype of one.
|
|
|
|
* <li>If T0 is a reference and T1 a primitive, an unboxing
|
2010-10-30 21:08:23 -07:00
|
|
|
* conversion will be applied at runtime, possibly followed
|
|
|
|
* by a Java method invocation conversion (JLS 5.3)
|
|
|
|
* on the primitive value. (These are the widening conversions.)
|
2009-05-05 22:40:09 -07:00
|
|
|
* T0 must be a wrapper class or a supertype of one.
|
2010-10-30 21:08:23 -07:00
|
|
|
* (In the case where T0 is Object, these are the conversions
|
|
|
|
* allowed by java.lang.reflect.Method.invoke.)
|
2010-01-07 16:16:45 -08:00
|
|
|
* <li>If the return type T1 is void, any returned value is discarded
|
|
|
|
* <li>If the return type T0 is void and T1 a reference, a null value is introduced.
|
|
|
|
* <li>If the return type T0 is void and T1 a primitive, a zero value is introduced.
|
2009-05-05 22:40:09 -07:00
|
|
|
* </ul>
|
|
|
|
* @param target the method handle to invoke after arguments are retyped
|
|
|
|
* @param newType the expected type of the new method handle
|
|
|
|
* @return a method handle which delegates to {@code target} after performing
|
|
|
|
* any necessary argument conversions, and arranges for any
|
|
|
|
* necessary return value conversions
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if either argument is null
|
2010-10-30 21:08:23 -07:00
|
|
|
* @throws WrongMethodTypeException if the conversion cannot be made
|
2010-01-07 16:16:45 -08:00
|
|
|
* @see MethodHandle#asType
|
2010-10-30 21:08:23 -07:00
|
|
|
* @see MethodHandles#explicitCastArguments
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle convertArguments(MethodHandle target, MethodType newType) {
|
|
|
|
MethodType oldType = target.type();
|
|
|
|
if (oldType.equals(newType))
|
|
|
|
return target;
|
2010-10-30 21:02:30 -07:00
|
|
|
MethodHandle res = null;
|
|
|
|
try {
|
2011-03-18 00:03:24 -07:00
|
|
|
res = MethodHandleImpl.convertArguments(target,
|
2010-10-30 21:02:30 -07:00
|
|
|
newType, oldType, null);
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
if (res == null)
|
2010-10-30 21:02:30 -07:00
|
|
|
throw new WrongMethodTypeException("cannot convert to "+newType+": "+target);
|
2009-05-05 22:40:09 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which adapts the type of the
|
2010-10-30 21:08:23 -07:00
|
|
|
* given method handle to a new type by pairwise argument conversion.
|
|
|
|
* The original type and new type must have the same number of arguments.
|
|
|
|
* The resulting method handle is guaranteed to report a type
|
|
|
|
* which is equal to the desired new type.
|
|
|
|
* <p>
|
|
|
|
* If the original type and new type are equal, returns target.
|
|
|
|
* <p>
|
|
|
|
* The same conversions are allowed as for {@link #convertArguments convertArguments},
|
|
|
|
* and some additional conversions are also applied if those conversions fail.
|
|
|
|
* Given types T0, T1, one of the following conversions is applied
|
|
|
|
* in addition, if the conversions specified for {@code convertArguments}
|
|
|
|
* would be insufficient:
|
|
|
|
* <ul>
|
|
|
|
* <li>If T0 and T1 are references, and T1 is an interface type,
|
|
|
|
* then the value of type T0 is passed as a T1 without a cast.
|
|
|
|
* (This treatment of interfaces follows the usage of the bytecode verifier.)
|
|
|
|
* <li>If T0 and T1 are primitives and one is boolean,
|
|
|
|
* the boolean is treated as a one-bit unsigned integer.
|
|
|
|
* (This treatment follows the usage of the bytecode verifier.)
|
|
|
|
* A conversion from another primitive type behaves as if
|
|
|
|
* it first converts to byte, and then masks all but the low bit.
|
|
|
|
* <li>If a primitive value would be converted by {@code convertArguments}
|
|
|
|
* using Java method invocation conversion (JLS 5.3),
|
|
|
|
* Java casting conversion (JLS 5.5) may be used also.
|
|
|
|
* This allows primitives to be narrowed as well as widened.
|
|
|
|
* </ul>
|
|
|
|
* @param target the method handle to invoke after arguments are retyped
|
|
|
|
* @param newType the expected type of the new method handle
|
|
|
|
* @return a method handle which delegates to {@code target} after performing
|
|
|
|
* any necessary argument conversions, and arranges for any
|
|
|
|
* necessary return value conversions
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if either argument is null
|
2010-10-30 21:08:23 -07:00
|
|
|
* @throws WrongMethodTypeException if the conversion cannot be made
|
|
|
|
* @see MethodHandle#asType
|
|
|
|
* @see MethodHandles#convertArguments
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
|
|
|
|
return convertArguments(target, newType); // FIXME!
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FIXME: Reconcile javadoc with 10/22/2010 EG notes on conversion:
|
|
|
|
|
|
|
|
Both converters arrange for their method handles to convert arguments
|
|
|
|
and return values. The conversion rules are the same for arguments
|
|
|
|
and return values, and depend only on source and target types, S and
|
|
|
|
T. The conversions allowed by castConvertArguments are a strict
|
|
|
|
superset of those performed by convertArguments.
|
|
|
|
|
|
|
|
In all cases, if S and T are references, a simple checkcast is done.
|
|
|
|
If neither S nor T is a primitive, no attempt is made to unbox and
|
|
|
|
box. A failed conversion throws ClassCastException.
|
|
|
|
|
|
|
|
If T is void, the value is dropped.
|
|
|
|
|
|
|
|
For compatibility with reflection, if S is void and T is a reference,
|
|
|
|
a null value is produced.
|
|
|
|
|
|
|
|
For compatibility with reflection, if S is a reference and T is a
|
|
|
|
primitive, S is first unboxed and then undergoes primitive conversion.
|
|
|
|
In the case of 'convertArguments', only assignment conversion is
|
|
|
|
performed (no narrowing primitive conversion).
|
|
|
|
|
|
|
|
If S is a primitive, S is boxed, and then the above rules are applied.
|
|
|
|
If S and T are both primitives, the boxing will be undetectable; only
|
|
|
|
the primitive conversions will be apparent to the user. The key point
|
|
|
|
is that if S is a primitive type, the implementation may box it and
|
|
|
|
treat is as Object, without loss of information, or it may use a "fast
|
|
|
|
path" which does not use boxing.
|
|
|
|
|
|
|
|
Notwithstanding the rules above, for compatibility with the verifier,
|
|
|
|
if T is an interface, it is treated as if it were Object. [KEEP THIS?]
|
|
|
|
|
|
|
|
Also, for compatibility with the verifier, a boolean may be undergo
|
|
|
|
widening or narrowing conversion to any other primitive type. [KEEP THIS?]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which adapts the calling sequence of the
|
2009-05-05 22:40:09 -07:00
|
|
|
* given method handle to a new type, by reordering the arguments.
|
2010-10-30 21:08:23 -07:00
|
|
|
* The resulting method handle is guaranteed to report a type
|
2009-05-05 22:40:09 -07:00
|
|
|
* which is equal to the desired new type.
|
|
|
|
* <p>
|
|
|
|
* The given array controls the reordering.
|
|
|
|
* Call {@code #I} the number of incoming parameters (the value
|
|
|
|
* {@code newType.parameterCount()}, and call {@code #O} the number
|
|
|
|
* of outgoing parameters (the value {@code target.type().parameterCount()}).
|
|
|
|
* Then the length of the reordering array must be {@code #O},
|
|
|
|
* and each element must be a non-negative number less than {@code #I}.
|
|
|
|
* For every {@code N} less than {@code #O}, the {@code N}-th
|
|
|
|
* outgoing argument will be taken from the {@code I}-th incoming
|
|
|
|
* argument, where {@code I} is {@code reorder[N]}.
|
|
|
|
* <p>
|
2010-10-30 21:08:23 -07:00
|
|
|
* No argument or return value conversions are applied.
|
|
|
|
* The type of each incoming argument, as determined by {@code newType},
|
|
|
|
* must be identical to the type of the corresponding outgoing argument
|
|
|
|
* or arguments in the target method handle.
|
|
|
|
* The return type of {@code newType} must be identical to the return
|
|
|
|
* type of the original target.
|
|
|
|
* <p>
|
2009-05-05 22:40:09 -07:00
|
|
|
* The reordering array need not specify an actual permutation.
|
|
|
|
* An incoming argument will be duplicated if its index appears
|
|
|
|
* more than once in the array, and an incoming argument will be dropped
|
|
|
|
* if its index does not appear in the array.
|
2010-10-30 21:08:23 -07:00
|
|
|
* As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
|
|
|
|
* incoming arguments which are not mentioned in the reordering array
|
|
|
|
* are may be any type, as determined only by {@code newType}.
|
|
|
|
* <blockquote><pre>
|
|
|
|
MethodType intfn1 = MethodType.methodType(int.class, int.class);
|
|
|
|
MethodType intfn2 = MethodType.methodType(int.class, int.class, int.class);
|
|
|
|
MethodHandle sub = ... {int x, int y => x-y} ...;
|
|
|
|
assert(sub.type().equals(intfn2));
|
|
|
|
MethodHandle sub1 = MethodHandles.permuteArguments(sub, intfn2, 0, 1);
|
|
|
|
MethodHandle rsub = MethodHandles.permuteArguments(sub, intfn2, 1, 0);
|
|
|
|
assert((int)rsub.invokeExact(1, 100) == 99);
|
|
|
|
MethodHandle add = ... {int x, int y => x+y} ...;
|
|
|
|
assert(add.type().equals(intfn2));
|
|
|
|
MethodHandle twice = MethodHandles.permuteArguments(add, intfn1, 0, 0);
|
|
|
|
assert(twice.type().equals(intfn1));
|
|
|
|
assert((int)twice.invokeExact(21) == 42);
|
|
|
|
* </pre></blockquote>
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param target the method handle to invoke after arguments are reordered
|
|
|
|
* @param newType the expected type of the new method handle
|
|
|
|
* @param reorder a string which controls the reordering
|
2010-10-30 21:08:23 -07:00
|
|
|
* @return a method handle which delegates to {@code target} after it
|
|
|
|
* drops unused arguments and moves and/or duplicates the other arguments
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) {
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodType oldType = target.type();
|
|
|
|
checkReorder(reorder, newType, oldType);
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.convertArguments(target,
|
2009-05-05 22:40:09 -07:00
|
|
|
newType, oldType,
|
|
|
|
reorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void checkReorder(int[] reorder, MethodType newType, MethodType oldType) {
|
|
|
|
if (reorder.length == oldType.parameterCount()) {
|
|
|
|
int limit = newType.parameterCount();
|
|
|
|
boolean bad = false;
|
|
|
|
for (int i : reorder) {
|
|
|
|
if (i < 0 || i >= limit) {
|
|
|
|
bad = true; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!bad) return;
|
|
|
|
}
|
|
|
|
throw newIllegalArgumentException("bad reorder array");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-30 21:08:23 -07:00
|
|
|
* Equivalent to the following code:
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
* int spreadPos = newType.parameterCount() - 1;
|
2011-02-11 01:26:28 -08:00
|
|
|
* Class<?> spreadType = newType.parameterType(spreadPos);
|
2010-10-30 21:08:23 -07:00
|
|
|
* int spreadCount = target.type().parameterCount() - spreadPos;
|
|
|
|
* MethodHandle adapter = target.asSpreader(spreadType, spreadCount);
|
|
|
|
* adapter = adapter.asType(newType);
|
|
|
|
* return adapter;
|
|
|
|
* </pre></blockquote>
|
|
|
|
* @param target the method handle to invoke after argument spreading
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param newType the expected type of the new method handle
|
2010-10-30 21:08:23 -07:00
|
|
|
* @return a method handle which spreads its final argument,
|
2009-05-05 22:40:09 -07:00
|
|
|
* before calling the original method handle
|
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
/*non-public*/ static
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodHandle spreadArguments(MethodHandle target, MethodType newType) {
|
|
|
|
MethodType oldType = target.type();
|
|
|
|
int inargs = newType.parameterCount();
|
|
|
|
int outargs = oldType.parameterCount();
|
|
|
|
int spreadPos = inargs - 1;
|
|
|
|
int numSpread = (outargs - spreadPos);
|
|
|
|
MethodHandle res = null;
|
|
|
|
if (spreadPos >= 0 && numSpread >= 0) {
|
2011-03-18 00:03:24 -07:00
|
|
|
res = MethodHandleImpl.spreadArguments(target, newType, spreadPos);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
if (res == null) {
|
|
|
|
throw newIllegalArgumentException("cannot spread "+newType+" to " +oldType);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-30 21:08:23 -07:00
|
|
|
* Equivalent to the following code:
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
* int collectPos = target.type().parameterCount() - 1;
|
2011-02-11 01:26:28 -08:00
|
|
|
* Class<?> collectType = target.type().parameterType(collectPos);
|
2010-10-30 21:08:23 -07:00
|
|
|
* if (!collectType.isArray()) collectType = Object[].class;
|
|
|
|
* int collectCount = newType.parameterCount() - collectPos;
|
|
|
|
* MethodHandle adapter = target.asCollector(collectType, collectCount);
|
|
|
|
* adapter = adapter.asType(newType);
|
|
|
|
* return adapter;
|
|
|
|
* </pre></blockquote>
|
|
|
|
* @param target the method handle to invoke after argument collection
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param newType the expected type of the new method handle
|
2010-10-30 21:08:23 -07:00
|
|
|
* @return a method handle which collects some trailing argument
|
2009-05-05 22:40:09 -07:00
|
|
|
* into an array, before calling the original method handle
|
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
/*non-public*/ static
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodHandle collectArguments(MethodHandle target, MethodType newType) {
|
|
|
|
MethodType oldType = target.type();
|
|
|
|
int inargs = newType.parameterCount();
|
|
|
|
int outargs = oldType.parameterCount();
|
|
|
|
int collectPos = outargs - 1;
|
|
|
|
int numCollect = (inargs - collectPos);
|
|
|
|
if (collectPos < 0 || numCollect < 0)
|
|
|
|
throw newIllegalArgumentException("wrong number of arguments");
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle res = MethodHandleImpl.collectArguments(target, newType, collectPos, null);
|
2010-01-07 16:16:45 -08:00
|
|
|
if (res == null) {
|
|
|
|
throw newIllegalArgumentException("cannot collect from "+newType+" to " +oldType);
|
|
|
|
}
|
|
|
|
return res;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle of the requested return type which returns the given
|
2010-10-30 21:08:23 -07:00
|
|
|
* constant value every time it is invoked.
|
|
|
|
* <p>
|
|
|
|
* Before the method handle is returned, the passed-in value is converted to the requested type.
|
|
|
|
* If the requested type is primitive, widening primitive conversions are attempted,
|
|
|
|
* else reference conversions are attempted.
|
|
|
|
* <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)},
|
|
|
|
* unless the type is {@code void}, in which case it is {@code identity(type)}.
|
|
|
|
* @param type the return type of the desired method handle
|
|
|
|
* @param value the value to return
|
|
|
|
* @return a method handle of the given return type and no arguments, which always returns the given value
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the {@code type} argument is null
|
|
|
|
* @throws ClassCastException if the value cannot be converted to the required return type
|
|
|
|
* @throws IllegalArgumentException if the given type is {@code void.class}
|
2010-10-30 21:08:23 -07:00
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle constant(Class<?> type, Object value) {
|
|
|
|
if (type.isPrimitive()) {
|
2011-02-11 01:26:28 -08:00
|
|
|
if (type == void.class)
|
|
|
|
throw newIllegalArgumentException("void type");
|
2010-10-30 21:08:23 -07:00
|
|
|
Wrapper w = Wrapper.forPrimitiveType(type);
|
|
|
|
return identity(type).bindTo(w.convert(value, type));
|
|
|
|
} else {
|
|
|
|
return identity(type).bindTo(type.cast(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which returns its sole argument when invoked.
|
|
|
|
* <p>The identity function for {@code void} takes no arguments and returns no values.
|
|
|
|
* @param type the type of the sole parameter and return value of the desired method handle
|
|
|
|
* @return a unary method handle which accepts and returns the given type
|
|
|
|
* @throws NullPointerException if the argument is null
|
|
|
|
* @throws IllegalArgumentException if the given type is {@code void.class}
|
2010-10-30 21:08:23 -07:00
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle identity(Class<?> type) {
|
2011-02-11 01:26:28 -08:00
|
|
|
if (type == void.class)
|
|
|
|
throw newIllegalArgumentException("void type");
|
2011-03-18 00:03:24 -07:00
|
|
|
else if (type == Object.class)
|
|
|
|
return ValueConversions.identity();
|
|
|
|
else if (type.isPrimitive())
|
|
|
|
return ValueConversions.identity(Wrapper.forPrimitiveType(type));
|
|
|
|
else
|
|
|
|
return AdapterMethodHandle.makeRetypeRaw(
|
|
|
|
MethodType.methodType(type, type), ValueConversions.identity());
|
2010-10-30 21:08:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which calls the original method handle {@code target},
|
2010-01-07 16:16:45 -08:00
|
|
|
* after inserting the given argument(s) at the given position.
|
|
|
|
* The formal parameters to {@code target} which will be supplied by those
|
|
|
|
* arguments are called <em>bound parameters</em>, because the new method
|
|
|
|
* will contain bindings for those parameters take from {@code values}.
|
|
|
|
* The type of the new method handle will drop the types for the bound
|
|
|
|
* parameters from the original target type, since the new method handle
|
|
|
|
* will no longer require those arguments to be supplied by its callers.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2010-01-07 16:16:45 -08:00
|
|
|
* Each given argument object must match the corresponding bound parameter type.
|
|
|
|
* If a bound parameter type is a primitive, the argument object
|
|
|
|
* must be a wrapper, and will be unboxed to produce the primitive value.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
|
|
|
* The <i>pos</i> may range between zero and <i>N</i> (inclusively),
|
2010-01-07 16:16:45 -08:00
|
|
|
* where <i>N</i> is the number of argument types in resulting method handle
|
|
|
|
* (after bound parameter types are dropped).
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param target the method handle to invoke after the argument is inserted
|
|
|
|
* @param pos where to insert the argument (zero for the first)
|
2010-01-07 16:16:45 -08:00
|
|
|
* @param values the series of arguments to insert
|
2010-10-30 21:08:23 -07:00
|
|
|
* @return a method handle which inserts an additional argument,
|
2009-05-05 22:40:09 -07:00
|
|
|
* before calling the original method handle
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the {@code target} argument or the {@code values} array is null
|
2010-10-30 21:08:23 -07:00
|
|
|
* @see MethodHandle#bindTo
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodHandle insertArguments(MethodHandle target, int pos, Object... values) {
|
|
|
|
int insCount = values.length;
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodType oldType = target.type();
|
|
|
|
int outargs = oldType.parameterCount();
|
2010-01-07 16:16:45 -08:00
|
|
|
int inargs = outargs - insCount;
|
|
|
|
if (inargs < 0)
|
|
|
|
throw newIllegalArgumentException("too many values to insert");
|
|
|
|
if (pos < 0 || pos > inargs)
|
2009-05-05 22:40:09 -07:00
|
|
|
throw newIllegalArgumentException("no argument type to append");
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodHandle result = target;
|
|
|
|
for (int i = 0; i < insCount; i++) {
|
|
|
|
Object value = values[i];
|
|
|
|
Class<?> valueType = oldType.parameterType(pos+i);
|
|
|
|
value = checkValue(valueType, value);
|
|
|
|
if (pos == 0 && !valueType.isPrimitive()) {
|
|
|
|
// At least for now, make bound method handles a special case.
|
2011-03-18 00:03:24 -07:00
|
|
|
MethodHandle bmh = MethodHandleImpl.bindReceiver(result, value);
|
2010-01-07 16:16:45 -08:00
|
|
|
if (bmh != null) {
|
|
|
|
result = bmh;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// else fall through to general adapter machinery
|
|
|
|
}
|
2011-03-18 00:03:24 -07:00
|
|
|
result = MethodHandleImpl.bindArgument(result, pos, value);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
2010-01-07 16:16:45 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which calls the original method handle,
|
2009-05-05 22:40:09 -07:00
|
|
|
* after dropping the given argument(s) at the given position.
|
|
|
|
* The type of the new method handle will insert the given argument
|
|
|
|
* type(s), at that position, into the original handle's type.
|
|
|
|
* <p>
|
2010-01-07 16:16:45 -08:00
|
|
|
* The <i>pos</i> may range between zero and <i>N</i>,
|
2009-05-05 22:40:09 -07:00
|
|
|
* where <i>N</i> is the number of argument types in <i>target</i>,
|
|
|
|
* meaning to drop the first or last argument (respectively),
|
|
|
|
* or an argument somewhere in between.
|
2010-01-07 16:16:45 -08:00
|
|
|
* <p>
|
|
|
|
* <b>Example:</b>
|
|
|
|
* <p><blockquote><pre>
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
import static java.lang.invoke.MethodType.*;
|
2010-12-16 15:59:27 -08:00
|
|
|
...
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
2011-03-23 23:02:31 -07:00
|
|
|
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
|
|
|
|
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
|
|
|
|
assertEquals(bigType, d0.type());
|
|
|
|
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
|
|
|
|
* </pre></blockquote>
|
|
|
|
* <p>
|
|
|
|
* This method is also equivalent to the following code:
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
* {@link #dropArguments(MethodHandle,int,Class...) dropArguments}(target, pos, valueTypes.toArray(new Class[0]))
|
2010-01-07 16:16:45 -08:00
|
|
|
* </pre></blockquote>
|
2010-10-30 21:08:23 -07:00
|
|
|
* @param target the method handle to invoke after the arguments are dropped
|
|
|
|
* @param valueTypes the type(s) of the argument(s) to drop
|
|
|
|
* @param pos position of first argument to drop (zero for the leftmost)
|
|
|
|
* @return a method handle which drops arguments of the given types,
|
2009-05-05 22:40:09 -07:00
|
|
|
* before calling the original method handle
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the {@code target} argument is null,
|
|
|
|
* or if the {@code valueTypes} list or any of its elements is null
|
|
|
|
* @throws IllegalArgumentException if any of the {@code valueTypes} is {@code void.class}
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
|
|
|
|
if (valueTypes.size() == 0) return target;
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodType oldType = target.type();
|
|
|
|
int outargs = oldType.parameterCount();
|
2010-01-07 16:16:45 -08:00
|
|
|
int inargs = outargs + valueTypes.size();
|
2009-05-05 22:40:09 -07:00
|
|
|
if (pos < 0 || pos >= inargs)
|
|
|
|
throw newIllegalArgumentException("no argument type to remove");
|
|
|
|
ArrayList<Class<?>> ptypes =
|
|
|
|
new ArrayList<Class<?>>(oldType.parameterList());
|
2010-01-07 16:16:45 -08:00
|
|
|
ptypes.addAll(pos, valueTypes);
|
|
|
|
MethodType newType = MethodType.methodType(oldType.returnType(), ptypes);
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.dropArguments(target, newType, pos);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which calls the original method handle,
|
2010-10-30 21:08:23 -07:00
|
|
|
* after dropping the given argument(s) at the given position.
|
|
|
|
* The type of the new method handle will insert the given argument
|
|
|
|
* type(s), at that position, into the original handle's type.
|
2011-03-23 23:02:31 -07:00
|
|
|
* <p>
|
|
|
|
* The <i>pos</i> may range between zero and <i>N</i>,
|
|
|
|
* where <i>N</i> is the number of argument types in <i>target</i>,
|
|
|
|
* meaning to drop the first or last argument (respectively),
|
|
|
|
* or an argument somewhere in between.
|
|
|
|
* <p>
|
|
|
|
* <b>Example:</b>
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
import static java.lang.invoke.MethodType.*;
|
|
|
|
...
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
|
|
|
MethodHandle d0 = dropArguments(cat, 0, String.class);
|
|
|
|
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d1 = dropArguments(cat, 1, String.class);
|
|
|
|
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d2 = dropArguments(cat, 2, String.class);
|
|
|
|
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
|
|
|
|
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
|
|
|
|
* </pre></blockquote>
|
|
|
|
* <p>
|
|
|
|
* This method is also equivalent to the following code:
|
|
|
|
* <p><blockquote><pre>
|
2010-10-30 21:08:23 -07:00
|
|
|
* {@link #dropArguments(MethodHandle,int,List) dropArguments}(target, pos, Arrays.asList(valueTypes))
|
2011-03-23 23:02:31 -07:00
|
|
|
* </pre></blockquote>
|
2010-10-30 21:08:23 -07:00
|
|
|
* @param target the method handle to invoke after the arguments are dropped
|
|
|
|
* @param valueTypes the type(s) of the argument(s) to drop
|
|
|
|
* @param pos position of first argument to drop (zero for the leftmost)
|
|
|
|
* @return a method handle which drops arguments of the given types,
|
|
|
|
* before calling the original method handle
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the {@code target} argument is null,
|
|
|
|
* or if the {@code valueTypes} array or any of its elements is null
|
|
|
|
* @throws IllegalArgumentException if any of the {@code valueTypes} is {@code void.class}
|
2010-10-30 21:08:23 -07:00
|
|
|
*/
|
2010-01-07 16:16:45 -08:00
|
|
|
public static
|
|
|
|
MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) {
|
|
|
|
return dropArguments(target, pos, Arrays.asList(valueTypes));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Adapts a target method handle {@code target} by pre-processing
|
2010-01-07 16:16:45 -08:00
|
|
|
* one or more of its arguments, each with its own unary filter function,
|
|
|
|
* and then calling the target with each pre-processed argument
|
|
|
|
* replaced by the result of its corresponding filter function.
|
|
|
|
* <p>
|
|
|
|
* The pre-processing is performed by one or more method handles,
|
2010-10-30 21:08:23 -07:00
|
|
|
* specified in the elements of the {@code filters} array.
|
2011-02-11 01:26:28 -08:00
|
|
|
* Null arguments in the array are ignored, and the corresponding arguments left unchanged.
|
|
|
|
* (If there are no non-null elements in the array, the original target is returned.)
|
2010-10-30 21:08:23 -07:00
|
|
|
* Each filter is applied to the corresponding argument of the adapter.
|
2010-01-07 16:16:45 -08:00
|
|
|
* <p>
|
|
|
|
* If a filter {@code F} applies to the {@code N}th argument of
|
|
|
|
* the method handle, then {@code F} must be a method handle which
|
|
|
|
* takes exactly one argument. The type of {@code F}'s sole argument
|
|
|
|
* replaces the corresponding argument type of the target
|
|
|
|
* in the resulting adapted method handle.
|
|
|
|
* The return type of {@code F} must be identical to the corresponding
|
|
|
|
* parameter type of the target.
|
|
|
|
* <p>
|
2010-10-30 21:08:23 -07:00
|
|
|
* It is an error if there are elements of {@code filters}
|
2010-01-07 16:16:45 -08:00
|
|
|
* which do not correspond to argument positions in the target.
|
2010-10-30 21:08:23 -07:00
|
|
|
* <b>Example:</b>
|
|
|
|
* <p><blockquote><pre>
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
import static java.lang.invoke.MethodType.*;
|
2010-10-30 21:08:23 -07:00
|
|
|
...
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
MethodHandle upcase = lookup().findVirtual(String.class,
|
|
|
|
"toUpperCase", methodType(String.class));
|
2010-12-16 15:59:27 -08:00
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle f0 = filterArguments(cat, 0, upcase);
|
2010-12-16 15:59:27 -08:00
|
|
|
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle f1 = filterArguments(cat, 1, upcase);
|
2010-12-16 15:59:27 -08:00
|
|
|
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
|
2010-12-16 15:59:27 -08:00
|
|
|
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
|
2010-01-07 16:16:45 -08:00
|
|
|
* </pre></blockquote>
|
2011-02-11 01:26:28 -08:00
|
|
|
*
|
2010-01-07 16:16:45 -08:00
|
|
|
* @param target the method handle to invoke after arguments are filtered
|
2010-10-30 21:08:23 -07:00
|
|
|
* @param pos the position of the first argument to filter
|
2010-01-07 16:16:45 -08:00
|
|
|
* @param filters method handles to call initially on filtered arguments
|
|
|
|
* @return method handle which incorporates the specified argument filtering logic
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if the {@code target} argument is null
|
|
|
|
* or if the {@code filters} array is null
|
|
|
|
* @throws IllegalArgumentException if a non-null element of {@code filters}
|
|
|
|
* does not match a corresponding argument type of {@code target} as described above,
|
|
|
|
* or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()}
|
2010-01-07 16:16:45 -08:00
|
|
|
*/
|
|
|
|
public static
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) {
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodType targetType = target.type();
|
|
|
|
MethodHandle adapter = target;
|
|
|
|
MethodType adapterType = targetType;
|
2010-10-30 21:08:23 -07:00
|
|
|
int maxPos = targetType.parameterCount();
|
2011-02-11 01:26:28 -08:00
|
|
|
if (pos + filters.length > maxPos)
|
|
|
|
throw newIllegalArgumentException("too many filters");
|
|
|
|
int curPos = pos-1; // pre-incremented
|
2010-01-07 16:16:45 -08:00
|
|
|
for (MethodHandle filter : filters) {
|
2011-02-11 01:26:28 -08:00
|
|
|
curPos += 1;
|
|
|
|
if (filter == null) continue; // ignore null elements of filters
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodType filterType = filter.type();
|
|
|
|
if (filterType.parameterCount() != 1
|
2010-10-30 21:08:23 -07:00
|
|
|
|| filterType.returnType() != targetType.parameterType(curPos))
|
2010-01-07 16:16:45 -08:00
|
|
|
throw newIllegalArgumentException("target and filter types do not match");
|
2010-10-30 21:08:23 -07:00
|
|
|
adapterType = adapterType.changeParameterType(curPos, filterType.parameterType(0));
|
2011-03-18 00:03:24 -07:00
|
|
|
adapter = MethodHandleImpl.filterArgument(adapter, curPos, filter);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
|
|
|
MethodType midType = adapter.type();
|
|
|
|
if (midType != adapterType)
|
2011-03-18 00:03:24 -07:00
|
|
|
adapter = MethodHandleImpl.convertArguments(adapter, adapterType, midType, null);
|
2010-01-07 16:16:45 -08:00
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
|
2010-12-16 15:59:27 -08:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Adapts a target method handle {@code target} by post-processing
|
2010-10-30 21:08:23 -07:00
|
|
|
* its return value with a unary filter function.
|
|
|
|
* <p>
|
|
|
|
* If a filter {@code F} applies to the return value of
|
|
|
|
* the target method handle, then {@code F} must be a method handle which
|
|
|
|
* takes exactly one argument. The return type of {@code F}
|
|
|
|
* replaces the return type of the target
|
|
|
|
* in the resulting adapted method handle.
|
|
|
|
* The argument type of {@code F} must be identical to the
|
|
|
|
* return type of the target.
|
|
|
|
* <b>Example:</b>
|
|
|
|
* <p><blockquote><pre>
|
2011-03-23 23:02:31 -07:00
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
import static java.lang.invoke.MethodType.*;
|
2010-10-30 21:08:23 -07:00
|
|
|
...
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
MethodHandle length = lookup().findVirtual(String.class,
|
|
|
|
"length", methodType(int.class));
|
|
|
|
System.out.println((String) cat.invokeExact("x", "y")); // xy
|
|
|
|
MethodHandle f0 = filterReturnValue(cat, length);
|
|
|
|
System.out.println((int) f0.invokeExact("x", "y")); // 2
|
|
|
|
* </pre></blockquote>
|
|
|
|
* @param target the method handle to invoke before filtering the return value
|
|
|
|
* @param filter method handle to call on the return value
|
|
|
|
* @return method handle which incorporates the specified return value filtering logic
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if either argument is null
|
|
|
|
* @throws IllegalArgumentException if {@code filter}
|
2010-10-30 21:08:23 -07:00
|
|
|
* does not match the return type of {@code target} as described above
|
2010-10-30 21:02:30 -07:00
|
|
|
*/
|
2010-10-30 21:08:23 -07:00
|
|
|
public static
|
2010-10-30 21:02:30 -07:00
|
|
|
MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
|
|
|
|
MethodType targetType = target.type();
|
|
|
|
MethodType filterType = filter.type();
|
|
|
|
if (filterType.parameterCount() != 1
|
|
|
|
|| filterType.parameterType(0) != targetType.returnType())
|
|
|
|
throw newIllegalArgumentException("target and filter types do not match");
|
2011-02-11 01:26:28 -08:00
|
|
|
// result = fold( lambda(retval, arg...) { filter(retval) },
|
|
|
|
// lambda( arg...) { target(arg...) } )
|
2010-10-30 21:02:30 -07:00
|
|
|
// FIXME: Too many nodes here.
|
2011-02-11 01:26:28 -08:00
|
|
|
MethodHandle returner = dropArguments(filter, 1, targetType.parameterList());
|
|
|
|
return foldArguments(returner, target);
|
2010-10-30 21:02:30 -07:00
|
|
|
}
|
|
|
|
|
2010-01-07 16:16:45 -08:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Adapts a target method handle {@code target} by pre-processing
|
2010-01-07 16:16:45 -08:00
|
|
|
* some of its arguments, and then calling the target with
|
|
|
|
* the result of the pre-processing, plus all original arguments.
|
|
|
|
* <p>
|
|
|
|
* The pre-processing is performed by a second method handle, the {@code combiner}.
|
|
|
|
* The first {@code N} arguments passed to the adapter,
|
|
|
|
* are copied to the combiner, which then produces a result.
|
|
|
|
* (Here, {@code N} is defined as the parameter count of the adapter.)
|
|
|
|
* After this, control passes to the {@code target}, with both the result
|
|
|
|
* of the combiner, and all the original incoming arguments.
|
|
|
|
* <p>
|
|
|
|
* The first argument type of the target must be identical with the
|
|
|
|
* return type of the combiner.
|
|
|
|
* The resulting adapter is the same type as the target, except that the
|
|
|
|
* initial argument type of the target is dropped.
|
|
|
|
* <p>
|
2010-10-30 21:08:23 -07:00
|
|
|
* (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments
|
2010-01-07 16:16:45 -08:00
|
|
|
* that either the {@code combiner} or {@code target} does not wish to receive.
|
|
|
|
* If some of the incoming arguments are destined only for the combiner,
|
2011-02-11 01:26:28 -08:00
|
|
|
* consider using {@link MethodHandle#asCollector asCollector} instead, since those
|
2010-01-07 16:16:45 -08:00
|
|
|
* arguments will not need to be live on the stack on entry to the
|
|
|
|
* target.)
|
|
|
|
* <p>
|
|
|
|
* The first argument of the target must be identical with the
|
|
|
|
* return value of the combiner.
|
|
|
|
* <p> Here is pseudocode for the resulting adapter:
|
|
|
|
* <blockquote><pre>
|
|
|
|
* // there are N arguments in the A sequence
|
|
|
|
* T target(V, A[N]..., B...);
|
|
|
|
* V combiner(A...);
|
|
|
|
* T adapter(A... a, B... b) {
|
|
|
|
* V v = combiner(a...);
|
|
|
|
* return target(v, a..., b...);
|
|
|
|
* }
|
|
|
|
* </pre></blockquote>
|
|
|
|
* @param target the method handle to invoke after arguments are combined
|
|
|
|
* @param combiner method handle to call initially on the incoming arguments
|
|
|
|
* @return method handle which incorporates the specified argument folding logic
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if either argument is null
|
2010-01-07 16:16:45 -08:00
|
|
|
* @throws IllegalArgumentException if the first argument type of
|
|
|
|
* {@code target} is not the same as {@code combiner}'s return type,
|
2010-10-30 21:02:30 -07:00
|
|
|
* or if the following argument types of {@code target}
|
2010-01-07 16:16:45 -08:00
|
|
|
* are not identical with the argument types of {@code combiner}
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) {
|
|
|
|
MethodType targetType = target.type();
|
|
|
|
MethodType combinerType = combiner.type();
|
|
|
|
int foldArgs = combinerType.parameterCount();
|
|
|
|
boolean ok = (targetType.parameterCount() >= 1 + foldArgs);
|
2010-10-30 21:02:30 -07:00
|
|
|
if (ok && !combinerType.parameterList().equals(targetType.parameterList().subList(1, foldArgs+1)))
|
|
|
|
ok = false;
|
|
|
|
if (ok && !combinerType.returnType().equals(targetType.parameterType(0)))
|
|
|
|
ok = false;
|
2010-01-07 16:16:45 -08:00
|
|
|
if (!ok)
|
|
|
|
throw misMatchedTypes("target and combiner types", targetType, combinerType);
|
|
|
|
MethodType newType = targetType.dropParameterTypes(0, 1);
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.foldArguments(target, newType, combiner);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Makes a method handle which adapts a target method handle,
|
2009-05-05 22:40:09 -07:00
|
|
|
* by guarding it with a test, a boolean-valued method handle.
|
|
|
|
* If the guard fails, a fallback handle is called instead.
|
|
|
|
* All three method handles must have the same corresponding
|
|
|
|
* argument and return types, except that the return type
|
2010-01-07 16:16:45 -08:00
|
|
|
* of the test must be boolean, and the test is allowed
|
|
|
|
* to have fewer arguments than the other two method handles.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p> Here is pseudocode for the resulting adapter:
|
|
|
|
* <blockquote><pre>
|
|
|
|
* boolean test(A...);
|
2010-01-07 16:16:45 -08:00
|
|
|
* T target(A...,B...);
|
|
|
|
* T fallback(A...,B...);
|
|
|
|
* T adapter(A... a,B... b) {
|
2009-05-05 22:40:09 -07:00
|
|
|
* if (test(a...))
|
2010-01-07 16:16:45 -08:00
|
|
|
* return target(a..., b...);
|
2009-05-05 22:40:09 -07:00
|
|
|
* else
|
2010-01-07 16:16:45 -08:00
|
|
|
* return fallback(a..., b...);
|
2009-05-05 22:40:09 -07:00
|
|
|
* }
|
|
|
|
* </pre></blockquote>
|
2010-12-16 15:59:27 -08:00
|
|
|
* Note that the test arguments ({@code a...} in the pseudocode) cannot
|
|
|
|
* be modified by execution of the test, and so are passed unchanged
|
|
|
|
* from the caller to the target or fallback as appropriate.
|
2009-05-05 22:40:09 -07:00
|
|
|
* @param test method handle used for test, must return boolean
|
|
|
|
* @param target method handle to call if test passes
|
|
|
|
* @param fallback method handle to call if test fails
|
|
|
|
* @return method handle which incorporates the specified if/then/else logic
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2009-05-05 22:40:09 -07:00
|
|
|
* @throws IllegalArgumentException if {@code test} does not return boolean,
|
|
|
|
* or if all three method types do not match (with the return
|
|
|
|
* type of {@code test} changed to match that of {@code target}).
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle guardWithTest(MethodHandle test,
|
|
|
|
MethodHandle target,
|
|
|
|
MethodHandle fallback) {
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodType gtype = test.type();
|
|
|
|
MethodType ttype = target.type();
|
|
|
|
MethodType ftype = fallback.type();
|
2010-12-16 15:59:27 -08:00
|
|
|
if (!ttype.equals(ftype))
|
2010-01-07 16:16:45 -08:00
|
|
|
throw misMatchedTypes("target and fallback types", ttype, ftype);
|
2010-12-16 15:59:27 -08:00
|
|
|
if (gtype.returnType() != boolean.class)
|
|
|
|
throw newIllegalArgumentException("guard type is not a predicate "+gtype);
|
|
|
|
List<Class<?>> targs = ttype.parameterList();
|
|
|
|
List<Class<?>> gargs = gtype.parameterList();
|
|
|
|
if (!targs.equals(gargs)) {
|
|
|
|
int gpc = gargs.size(), tpc = targs.size();
|
|
|
|
if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs))
|
2010-01-07 16:16:45 -08:00
|
|
|
throw misMatchedTypes("target and test types", ttype, gtype);
|
2010-12-16 15:59:27 -08:00
|
|
|
test = dropArguments(test, gpc, targs.subList(gpc, tpc));
|
|
|
|
gtype = test.type();
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.makeGuardWithTest(test, target, fallback);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-01-07 16:16:45 -08:00
|
|
|
static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) {
|
|
|
|
return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2);
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Makes a method handle which adapts a target method handle,
|
2010-01-07 16:16:45 -08:00
|
|
|
* by running it inside an exception handler.
|
|
|
|
* If the target returns normally, the adapter returns that value.
|
|
|
|
* If an exception matching the specified type is thrown, the fallback
|
|
|
|
* handle is called instead on the exception, plus the original arguments.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2010-12-16 15:59:27 -08:00
|
|
|
* The target and handler must have the same corresponding
|
|
|
|
* argument and return types, except that handler may omit trailing arguments
|
|
|
|
* (similarly to the predicate in {@link #guardWithTest guardWithTest}).
|
|
|
|
* Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p> Here is pseudocode for the resulting adapter:
|
|
|
|
* <blockquote><pre>
|
2010-12-16 15:59:27 -08:00
|
|
|
* T target(A..., B...);
|
2010-01-07 16:16:45 -08:00
|
|
|
* T handler(ExType, A...);
|
2010-12-16 15:59:27 -08:00
|
|
|
* T adapter(A... a, B... b) {
|
2010-01-07 16:16:45 -08:00
|
|
|
* try {
|
2010-12-16 15:59:27 -08:00
|
|
|
* return target(a..., b...);
|
2010-01-07 16:16:45 -08:00
|
|
|
* } catch (ExType ex) {
|
|
|
|
* return handler(ex, a...);
|
|
|
|
* }
|
2009-05-05 22:40:09 -07:00
|
|
|
* }
|
|
|
|
* </pre></blockquote>
|
2010-12-16 15:59:27 -08:00
|
|
|
* Note that the saved arguments ({@code a...} in the pseudocode) cannot
|
|
|
|
* be modified by execution of the target, and so are passed unchanged
|
|
|
|
* from the caller to the handler, if the handler is invoked.
|
|
|
|
* <p>
|
|
|
|
* The target and handler must return the same type, even if the handler
|
|
|
|
* always throws. (This might happen, for instance, because the handler
|
|
|
|
* is simulating a {@code finally} clause).
|
|
|
|
* To create such a throwing handler, compose the handler creation logic
|
|
|
|
* with {@link #throwException throwException},
|
|
|
|
* in order to create a method handle of the correct return type.
|
2010-01-07 16:16:45 -08:00
|
|
|
* @param target method handle to call
|
|
|
|
* @param exType the type of exception which the handler will catch
|
|
|
|
* @param handler method handle to call if a matching exception is thrown
|
|
|
|
* @return method handle which incorporates the specified try/catch logic
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if any argument is null
|
2010-01-07 16:16:45 -08:00
|
|
|
* @throws IllegalArgumentException if {@code handler} does not accept
|
|
|
|
* the given exception type, or if the method handle types do
|
|
|
|
* not match in their return types and their
|
|
|
|
* corresponding parameters
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
public static
|
2010-01-07 16:16:45 -08:00
|
|
|
MethodHandle catchException(MethodHandle target,
|
|
|
|
Class<? extends Throwable> exType,
|
|
|
|
MethodHandle handler) {
|
2010-12-16 15:59:27 -08:00
|
|
|
MethodType ttype = target.type();
|
|
|
|
MethodType htype = handler.type();
|
|
|
|
if (htype.parameterCount() < 1 ||
|
|
|
|
!htype.parameterType(0).isAssignableFrom(exType))
|
|
|
|
throw newIllegalArgumentException("handler does not accept exception type "+exType);
|
|
|
|
if (htype.returnType() != ttype.returnType())
|
|
|
|
throw misMatchedTypes("target and handler return types", ttype, htype);
|
|
|
|
List<Class<?>> targs = ttype.parameterList();
|
|
|
|
List<Class<?>> hargs = htype.parameterList();
|
|
|
|
hargs = hargs.subList(1, hargs.size()); // omit leading parameter from handler
|
|
|
|
if (!targs.equals(hargs)) {
|
|
|
|
int hpc = hargs.size(), tpc = targs.size();
|
|
|
|
if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
|
|
|
|
throw misMatchedTypes("target and handler types", ttype, htype);
|
|
|
|
handler = dropArguments(handler, hpc, hargs.subList(hpc, tpc));
|
|
|
|
htype = handler.type();
|
|
|
|
}
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-01-07 16:16:45 -08:00
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Produces a method handle which will throw exceptions of the given {@code exType}.
|
2010-01-07 16:16:45 -08:00
|
|
|
* The method handle will accept a single argument of {@code exType},
|
|
|
|
* and immediately throw it as an exception.
|
|
|
|
* The method type will nominally specify a return of {@code returnType}.
|
|
|
|
* The return type may be anything convenient: It doesn't matter to the
|
|
|
|
* method handle's behavior, since it will never return normally.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @return method handle which can throw the given exceptions
|
|
|
|
* @throws NullPointerException if either argument is null
|
2010-01-07 16:16:45 -08:00
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) {
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType));
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2010-09-08 18:40:34 -07:00
|
|
|
|
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Produces an instance of the given single-method interface which redirects
|
2010-12-16 15:59:27 -08:00
|
|
|
* its calls to the given method handle.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
2011-03-23 23:02:31 -07:00
|
|
|
* A single-method interface is an interface which declares a unique method.
|
|
|
|
* When determining the unique method of a single-method interface,
|
2011-02-11 01:26:28 -08:00
|
|
|
* the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode})
|
2011-03-23 23:02:31 -07:00
|
|
|
* are disregarded. For example, {@link java.util.Comparator} is a single-method interface,
|
2011-02-11 01:26:28 -08:00
|
|
|
* even though it re-declares the {@code Object.equals} method.
|
|
|
|
* <p>
|
|
|
|
* The type must be public. No additional access checks are performed.
|
2010-09-08 18:40:34 -07:00
|
|
|
* <p>
|
2011-03-23 23:02:31 -07:00
|
|
|
* The resulting instance of the required type will respond to
|
|
|
|
* invocation of the type's single abstract method by calling
|
2010-09-08 18:40:34 -07:00
|
|
|
* the given {@code target} on the incoming arguments,
|
|
|
|
* and returning or throwing whatever the {@code target}
|
|
|
|
* returns or throws. The invocation will be as if by
|
2011-02-11 01:26:24 -08:00
|
|
|
* {@code target.invokeGeneric}.
|
2011-03-23 23:02:31 -07:00
|
|
|
* The target's type will be checked before the
|
2011-02-11 01:26:24 -08:00
|
|
|
* instance is created, as if by a call to {@code asType},
|
|
|
|
* which may result in a {@code WrongMethodTypeException}.
|
2010-09-08 18:40:34 -07:00
|
|
|
* <p>
|
2011-03-23 23:02:31 -07:00
|
|
|
* The wrapper instance will implement the requested interface
|
|
|
|
* and its super-types, but no other single-method interfaces.
|
|
|
|
* This means that the instance will not unexpectedly
|
2011-02-11 01:26:28 -08:00
|
|
|
* pass an {@code instanceof} test for any unrequested type.
|
|
|
|
* <p style="font-size:smaller;">
|
|
|
|
* <em>Implementation Note:</em>
|
2011-03-23 23:02:31 -07:00
|
|
|
* Therefore, each instance must implement a unique single-method interface.
|
2011-02-11 01:26:28 -08:00
|
|
|
* Implementations may not bundle together
|
2011-03-23 23:02:31 -07:00
|
|
|
* multiple single-method interfaces onto single implementation classes
|
2011-02-11 01:26:28 -08:00
|
|
|
* in the style of {@link java.awt.AWTEventMulticaster}.
|
|
|
|
* <p>
|
2010-09-08 18:40:34 -07:00
|
|
|
* The method handle may throw an <em>undeclared exception</em>,
|
|
|
|
* which means any checked exception (or other checked throwable)
|
2011-03-23 23:02:31 -07:00
|
|
|
* not declared by the requested type's single abstract method.
|
2010-12-16 15:59:27 -08:00
|
|
|
* If this happens, the throwable will be wrapped in an instance of
|
|
|
|
* {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException}
|
|
|
|
* and thrown in that wrapped form.
|
2010-09-08 18:40:34 -07:00
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* Like {@link java.lang.Integer#valueOf Integer.valueOf},
|
|
|
|
* {@code asInstance} is a factory method whose results are defined
|
|
|
|
* by their behavior.
|
|
|
|
* It is not guaranteed to return a new instance for every call.
|
2011-02-11 01:26:24 -08:00
|
|
|
* <p>
|
2011-03-23 23:02:31 -07:00
|
|
|
* Because of the possibility of {@linkplain java.lang.reflect.Method#isBridge bridge methods}
|
|
|
|
* and other corner cases, the interface may also have several abstract methods
|
|
|
|
* with the same name but having distinct descriptors (types of returns and parameters).
|
|
|
|
* In this case, all the methods are bound in common to the one given {@code target}.
|
|
|
|
* The type check and effective {@code asType} conversion is applied to each
|
|
|
|
* method type descriptor, and all abstract methods are bound to the {@code target} in common.
|
|
|
|
* Beyond this type check, no further checks are made to determine that the
|
|
|
|
* abstract methods are related in any way.
|
|
|
|
* <p>
|
2011-02-11 01:26:24 -08:00
|
|
|
* Future versions of this API may accept additional types,
|
|
|
|
* such as abstract classes with single abstract methods.
|
2011-02-11 01:26:28 -08:00
|
|
|
* Future versions of this API may also equip wrapper instances
|
|
|
|
* with one or more additional public "marker" interfaces.
|
|
|
|
*
|
2010-09-08 18:40:34 -07:00
|
|
|
* @param target the method handle to invoke from the wrapper
|
2011-03-23 23:02:31 -07:00
|
|
|
* @param smType the desired type of the wrapper, a single-method interface
|
2010-09-08 18:40:34 -07:00
|
|
|
* @return a correctly-typed wrapper for the given {@code target}
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws NullPointerException if either argument is null
|
2011-03-23 23:02:31 -07:00
|
|
|
* @throws IllegalArgumentException if the {@code smType} is not a
|
2011-02-11 01:26:24 -08:00
|
|
|
* valid argument to this method
|
|
|
|
* @throws WrongMethodTypeException if the {@code target} cannot
|
2011-03-23 23:02:31 -07:00
|
|
|
* be converted to the type required by the requested interface
|
2010-09-08 18:40:34 -07:00
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
// Other notes to implementors:
|
|
|
|
// <p>
|
2011-03-23 23:02:31 -07:00
|
|
|
// No stable mapping is promised between the single-method interface and
|
2011-02-11 01:26:28 -08:00
|
|
|
// the implementation class C. Over time, several implementation
|
2011-03-23 23:02:31 -07:00
|
|
|
// classes might be used for the same type.
|
2011-02-11 01:26:28 -08:00
|
|
|
// <p>
|
|
|
|
// If the implementation is able
|
2011-03-23 23:02:31 -07:00
|
|
|
// to prove that a wrapper of the required type
|
2011-02-11 01:26:28 -08:00
|
|
|
// has already been created for a given
|
|
|
|
// method handle, or for another method handle with the
|
|
|
|
// same behavior, the implementation may return that wrapper in place of
|
|
|
|
// a new wrapper.
|
|
|
|
// <p>
|
|
|
|
// This method is designed to apply to common use cases
|
|
|
|
// where a single method handle must interoperate with
|
|
|
|
// an interface that implements a function-like
|
2011-03-23 23:02:31 -07:00
|
|
|
// API. Additional variations, such as single-abstract-method classes with
|
2011-02-11 01:26:28 -08:00
|
|
|
// private constructors, or interfaces with multiple but related
|
|
|
|
// entry points, must be covered by hand-written or automatically
|
|
|
|
// generated adapter classes.
|
|
|
|
//
|
2010-09-08 18:40:34 -07:00
|
|
|
public static
|
2011-03-23 23:02:31 -07:00
|
|
|
<T> T asInstance(final MethodHandle target, final Class<T> smType) {
|
2010-09-08 18:40:34 -07:00
|
|
|
// POC implementation only; violates the above contract several ways
|
2011-03-23 23:02:31 -07:00
|
|
|
final Method sm = getSingleMethod(smType);
|
|
|
|
if (sm == null)
|
|
|
|
throw new IllegalArgumentException("not a single-method interface: "+smType.getName());
|
|
|
|
MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes());
|
|
|
|
MethodHandle checkTarget = target.asType(smMT); // make throw WMT
|
2011-02-11 01:26:24 -08:00
|
|
|
checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class));
|
2011-03-23 23:02:31 -07:00
|
|
|
final MethodHandle vaTarget = checkTarget.asSpreader(Object[].class, smMT.parameterCount());
|
|
|
|
return smType.cast(Proxy.newProxyInstance(
|
|
|
|
smType.getClassLoader(),
|
|
|
|
new Class[]{ smType, WrapperInstance.class },
|
2010-09-08 18:40:34 -07:00
|
|
|
new InvocationHandler() {
|
2010-10-30 21:08:23 -07:00
|
|
|
private Object getArg(String name) {
|
2011-02-11 01:26:28 -08:00
|
|
|
if ((Object)name == "getWrapperInstanceTarget") return target;
|
2011-03-23 23:02:31 -07:00
|
|
|
if ((Object)name == "getWrapperInstanceType") return smType;
|
2010-10-30 21:08:23 -07:00
|
|
|
throw new AssertionError();
|
|
|
|
}
|
2010-09-08 18:40:34 -07:00
|
|
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
2011-02-11 01:26:28 -08:00
|
|
|
if (method.getDeclaringClass() == WrapperInstance.class)
|
2010-10-30 21:08:23 -07:00
|
|
|
return getArg(method.getName());
|
2011-03-23 23:02:31 -07:00
|
|
|
if (method.equals(sm))
|
2011-02-11 01:26:24 -08:00
|
|
|
return vaTarget.invokeExact(args);
|
2010-12-16 15:59:27 -08:00
|
|
|
if (isObjectMethod(method))
|
|
|
|
return callObjectMethod(this, method, args);
|
|
|
|
throw new InternalError();
|
2010-09-08 18:40:34 -07:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Determines if the given object was produced by a call to {@link #asInstance asInstance}.
|
2011-02-11 01:26:28 -08:00
|
|
|
* @param x any reference
|
|
|
|
* @return true if the reference is not null and points to an object produced by {@code asInstance}
|
2010-10-30 21:08:23 -07:00
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
public static
|
|
|
|
boolean isWrapperInstance(Object x) {
|
|
|
|
return x instanceof WrapperInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static WrapperInstance asWrapperInstance(Object x) {
|
|
|
|
try {
|
|
|
|
if (x != null)
|
|
|
|
return (WrapperInstance) x;
|
|
|
|
} catch (ClassCastException ex) {
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("not a wrapper instance");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces or recovers a target method handle which is behaviorally
|
2011-03-23 23:02:31 -07:00
|
|
|
* equivalent to the unique method of this wrapper instance.
|
2011-02-11 01:26:28 -08:00
|
|
|
* The object {@code x} must have been produced by a call to {@link #asInstance asInstance}.
|
|
|
|
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}.
|
|
|
|
* @param x any reference
|
2011-03-23 23:02:31 -07:00
|
|
|
* @return a method handle implementing the unique method
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws IllegalArgumentException if the reference x is not to a wrapper instance
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
MethodHandle wrapperInstanceTarget(Object x) {
|
|
|
|
return asWrapperInstance(x).getWrapperInstanceTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-23 23:02:31 -07:00
|
|
|
* Recovers the unique single-method interface type for which this wrapper instance was created.
|
2011-02-11 01:26:28 -08:00
|
|
|
* The object {@code x} must have been produced by a call to {@link #asInstance asInstance}.
|
|
|
|
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}.
|
|
|
|
* @param x any reference
|
2011-03-23 23:02:31 -07:00
|
|
|
* @return the single-method interface type for which the wrapper was created
|
2011-02-11 01:26:28 -08:00
|
|
|
* @throws IllegalArgumentException if the reference x is not to a wrapper instance
|
|
|
|
*/
|
|
|
|
public static
|
|
|
|
Class<?> wrapperInstanceType(Object x) {
|
|
|
|
return asWrapperInstance(x).getWrapperInstanceType();
|
2010-10-30 21:08:23 -07:00
|
|
|
}
|
|
|
|
|
2010-12-16 15:59:27 -08:00
|
|
|
private static
|
|
|
|
boolean isObjectMethod(Method m) {
|
|
|
|
switch (m.getName()) {
|
|
|
|
case "toString":
|
|
|
|
return (m.getReturnType() == String.class
|
|
|
|
&& m.getParameterTypes().length == 0);
|
|
|
|
case "hashCode":
|
|
|
|
return (m.getReturnType() == int.class
|
|
|
|
&& m.getParameterTypes().length == 0);
|
|
|
|
case "equals":
|
|
|
|
return (m.getReturnType() == boolean.class
|
|
|
|
&& m.getParameterTypes().length == 1
|
|
|
|
&& m.getParameterTypes()[0] == Object.class);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static
|
|
|
|
Object callObjectMethod(Object self, Method m, Object[] args) {
|
|
|
|
assert(isObjectMethod(m)) : m;
|
|
|
|
switch (m.getName()) {
|
|
|
|
case "toString":
|
|
|
|
return self.getClass().getName() + "@" + Integer.toHexString(self.hashCode());
|
|
|
|
case "hashCode":
|
|
|
|
return System.identityHashCode(self);
|
|
|
|
case "equals":
|
|
|
|
return (self == args[0]);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-09-08 18:40:34 -07:00
|
|
|
private static
|
2011-03-23 23:02:31 -07:00
|
|
|
Method getSingleMethod(Class<?> smType) {
|
|
|
|
Method sm = null;
|
|
|
|
for (Method m : smType.getMethods()) {
|
2010-09-08 18:40:34 -07:00
|
|
|
int mod = m.getModifiers();
|
|
|
|
if (Modifier.isAbstract(mod)) {
|
2011-03-23 23:02:31 -07:00
|
|
|
if (sm != null && !isObjectMethod(sm))
|
2010-09-08 18:40:34 -07:00
|
|
|
return null; // too many abstract methods
|
2011-03-23 23:02:31 -07:00
|
|
|
sm = m;
|
2010-09-08 18:40:34 -07:00
|
|
|
}
|
|
|
|
}
|
2011-03-23 23:02:31 -07:00
|
|
|
if (!smType.isInterface() && getSingleConstructor(smType) == null)
|
2010-09-08 18:40:34 -07:00
|
|
|
return null; // wrong kind of constructor
|
2011-03-23 23:02:31 -07:00
|
|
|
return sm;
|
2010-09-08 18:40:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static
|
2011-03-23 23:02:31 -07:00
|
|
|
Constructor getSingleConstructor(Class<?> smType) {
|
|
|
|
for (Constructor c : smType.getDeclaredConstructors()) {
|
2010-09-08 18:40:34 -07:00
|
|
|
if (c.getParameterTypes().length == 0) {
|
|
|
|
int mod = c.getModifiers();
|
|
|
|
if (Modifier.isPublic(mod) || Modifier.isProtected(mod))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-10-30 21:02:30 -07:00
|
|
|
|
|
|
|
/*non-public*/
|
2011-02-11 01:26:24 -08:00
|
|
|
static MethodHandle asVarargsCollector(MethodHandle target, Class<?> arrayType) {
|
2011-03-18 00:03:24 -07:00
|
|
|
return MethodHandleImpl.asVarargsCollector(target, arrayType);
|
2010-10-30 21:02:30 -07:00
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|