jdk/src/share/classes/java/dyn/MethodHandleProvider.java
changeset 7051 1c545d70a157
equal deleted inserted replaced
6325:adf468d05745 7051:1c545d70a157
       
     1 /*
       
     2  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package java.dyn;
       
    27 
       
    28 /**
       
    29  * An interface for an object to provide a target {@linkplain MethodHandle method handle} to a {@code invokedynamic} instruction.
       
    30  * There are many function-like objects in various Java APIs.
       
    31  * This interface provides a standard way for such function-like objects to be bound
       
    32  * to a dynamic call site, by providing a view of their behavior in the form of a low-level method handle.
       
    33  * <p>
       
    34  * The type {@link MethodHandle} is a concrete class whose implementation
       
    35  * hierarchy (if any) may be tightly coupled to the underlying JVM implementation.
       
    36  * It cannot also serve as a base type for user-defined functional APIs.
       
    37  * For this reason, {@code MethodHandle} cannot be subclassed to add new
       
    38  * behavior to method handles.  But this interface can be used to provide
       
    39  * a link between a user-defined function and the {@code invokedynamic}
       
    40  * instruction and the method handle API.
       
    41  */
       
    42 public interface MethodHandleProvider {
       
    43     /** Produce a method handle which will serve as a behavioral proxy for the current object.
       
    44      *  The type and invocation behavior of the proxy method handle are user-defined,
       
    45      *  and should have some relation to the intended meaning of the original object itself.
       
    46      *  <p>
       
    47      *  The current object may have a changeable behavior.
       
    48      *  For example, {@link CallSite} has a {@code setTarget} method which changes its invocation.
       
    49      *  In such a case, it is <em>incorrect</em> for {@code asMethodHandle} to return
       
    50      *  a method handle whose behavior may diverge from that of the current object.
       
    51      *  Rather, the returned method handle must stably and permanently access
       
    52      *  the behavior of the current object, even if that behavior is changeable.
       
    53      *  <p>
       
    54      *  The reference identity of the proxy method handle is not guaranteed to
       
    55      *  have any particular relation to the reference identity of the object.
       
    56      *  In particular, several objects with the same intended meaning could
       
    57      *  share a common method handle, or the same object could return different
       
    58      *  method handles at different times.  In the latter case, the different
       
    59      *  method handles should have the same type and invocation behavior,
       
    60      *  and be usable from any thread at any time.
       
    61      *  In particular, if a MethodHandleProvider is bound to an <code>invokedynamic</code>
       
    62      *  call site, the proxy method handle extracted at the time of binding
       
    63      *  will be used for an unlimited time, until the call site is rebound.
       
    64      *  <p>
       
    65      *  The type {@link MethodHandle} itself implements {@code MethodHandleProvider}, and
       
    66      *  for this method simply returns {@code this}.
       
    67      */
       
    68     public MethodHandle asMethodHandle();
       
    69 
       
    70     /** Produce a method handle of a given type which will serve as a behavioral proxy for the current object.
       
    71      *  As for the no-argument version {@link #asMethodHandle()}, the invocation behavior of the
       
    72      *  proxy method handle is user-defined.  But the type must be the given type,
       
    73      *  or else a {@link WrongMethodTypeException} must be thrown.
       
    74      *  <p>
       
    75      *  If the current object somehow represents a variadic or overloaded behavior,
       
    76      *  the method handle returned for a given type might represent only a subset of
       
    77      *  the current object's repertoire of behaviors, which correspond to that type.
       
    78      */
       
    79     public MethodHandle asMethodHandle(MethodType type) throws WrongMethodTypeException;
       
    80 }