|
Here is a short list of links related to this Java LambdaMetafactory.java source code file:
Java example source code file (LambdaMetafactory.java)
The LambdaMetafactory.java Java example source code/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * 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 * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. * * 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. */ package java.lang.invoke; import java.io.Serializable; import java.util.Arrays; /** * <p>Methods to facilitate the creation of simple "function objects" that * implement one or more interfaces by delegation to a provided {@link MethodHandle}, * possibly after type adaptation and partial evaluation of arguments. These * methods are typically used as <em>bootstrap methods for {@code invokedynamic} * call sites, to support the <em>lambda expression and method * reference expression</em> features of the Java Programming Language. * * <p>Indirect access to the behavior specified by the provided {@code MethodHandle} * proceeds in order through three phases: * <ul> * <li>Linkage occurs when the methods in this class are invoked. * They take as arguments an interface to be implemented (typically a * <em>functional interface, one with a single abstract method), a * name and signature of a method from that interface to be implemented, a * method handle describing the desired implementation behavior * for that method, and possibly other additional metadata, and produce a * {@link CallSite} whose target can be used to create suitable function * objects. Linkage may involve dynamically loading a new class that * implements the target interface. The {@code CallSite} can be considered a * "factory" for function objects and so these linkage methods are referred * to as "metafactories".</li> * * <li>Capture occurs when the {@code CallSite}'s target is * invoked, typically through an {@code invokedynamic} call site, * producing a function object. This may occur many times for * a single factory {@code CallSite}. Capture may involve allocation of a * new function object, or may return an existing function object. The * behavior {@code MethodHandle} may have additional parameters beyond those * of the specified interface method; these are referred to as <em>captured * parameters</em>, which must be provided as arguments to the * {@code CallSite} target, and which may be early-bound to the behavior * {@code MethodHandle}. The number of captured parameters and their types * are determined during linkage.</li> * * <li>Invocation occurs when an implemented interface method * is invoked on a function object. This may occur many times for a single * function object. The method referenced by the behavior {@code MethodHandle} * is invoked with the captured arguments and any additional arguments * provided on invocation, as if by {@link MethodHandle#invoke(Object...)}.</li> * </ul> * * <p>It is sometimes useful to restrict the set of inputs or results permitted * at invocation. For example, when the generic interface {@code Predicate<T>} * is parameterized as {@code Predicate<String>}, the input must be a * {@code String}, even though the method to implement allows any {@code Object}. * At linkage time, an additional {@link MethodType} parameter describes the * "instantiated" method type; on invocation, the arguments and eventual result * are checked against this {@code MethodType}. * * <p>This class provides two forms of linkage methods: a standard version * ({@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)}) * using an optimized protocol, and an alternate version * {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}). * The alternate version is a generalization of the standard version, providing * additional control over the behavior of the generated function objects via * flags and additional arguments. The alternate version adds the ability to * manage the following attributes of function objects: * * <ul> * <li>Bridging. It is sometimes useful to implement multiple * variations of the method signature, involving argument or return type * adaptation. This occurs when multiple distinct VM signatures for a method * are logically considered to be the same method by the language. The * flag {@code FLAG_BRIDGES} indicates that a list of additional * {@code MethodType}s will be provided, each of which will be implemented * by the resulting function object. These methods will share the same * name and instantiated type.</li> * * <li>Multiple interfaces. If needed, more than one interface * can be implemented by the function object. (These additional interfaces * are typically marker interfaces with no methods.) The flag {@code FLAG_MARKERS} * indicates that a list of additional interfaces will be provided, each of * which should be implemented by the resulting function object.</li> * * <li>Serializability. The generated function objects do not * generally support serialization. If desired, {@code FLAG_SERIALIZABLE} * can be used to indicate that the function objects should be serializable. * Serializable function objects will use, as their serialized form, * instances of the class {@code SerializedLambda}, which requires additional * assistance from the capturing class (the class described by the * {@link MethodHandles.Lookup} parameter {@code caller}); see * {@link SerializedLambda} for details.</li> * </ul> * * <p>Assume the linkage arguments are as follows: * <ul> * <li>{@code invokedType} (describing the {@code CallSite} signature) has * K parameters of types (D1..Dk) and return type Rd;</li> * <li>{@code samMethodType} (describing the implemented method type) has N * parameters, of types (U1..Un) and return type Ru;</li> * <li>{@code implMethod} (the {@code MethodHandle} providing the * implementation has M parameters, of types (A1..Am) and return type Ra * (if the method describes an instance method, the method type of this * method handle already includes an extra first argument corresponding to * the receiver);</li> * <li>{@code instantiatedMethodType} (allowing restrictions on invocation) * has N parameters, of types (T1..Tn) and return type Rt.</li> * </ul> * * <p>Then the following linkage invariants must hold: * <ul> * <li>Rd is an interface * <li>{@code implMethod} is a direct method handle * <li>{@code samMethodType} and {@code instantiatedMethodType} have the same * arity N, and for i=1..N, Ti and Ui are the same type, or Ti and Ui are * both reference types and Ti is a subtype of Ui</li> * <li>Either Rt and Ru are the same type, or both are reference types and * Rt is a subtype of Ru</li> * <li>K + N = M * <li>For i=1..K, Di = Ai * <li>For i=1..N, Ti is adaptable to Aj, where j=i+k * <li>The return type Rt is void, or the return type Ra is not void and is * adaptable to Rt</li> * </ul> * * <p>Further, at capture time, if {@code implMethod} corresponds to an instance * method, and there are any capture arguments ({@code K > 0}), then the first * capture argument (corresponding to the receiver) must be non-null. * * <p>A type Q is considered adaptable to S as follows: * <table summary="adaptable types"> * <tr> | Q | S | Link-time checks | Invocation-time checks | |
---|---|---|---|---|---|
Primitive | Reference | Primitive | Reference |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.