jdk/src/share/classes/sun/dyn/InvokeGeneric.java
changeset 8854 376ac0a67571
parent 8853 6aa795396cc8
parent 8835 71d383b902e9
child 8855 46acf76a5339
equal deleted inserted replaced
8853:6aa795396cc8 8854:376ac0a67571
     1 /*
       
     2  * Copyright (c) 2009, 2011, 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 sun.dyn;
       
    27 
       
    28 import java.dyn.*;
       
    29 import java.lang.reflect.*;
       
    30 import sun.dyn.util.*;
       
    31 import static sun.dyn.MethodTypeImpl.invokers;
       
    32 
       
    33 /**
       
    34  * Adapters which manage MethodHanndle.invokeGeneric calls.
       
    35  * The JVM calls one of these when the exact type match fails.
       
    36  * @author jrose
       
    37  */
       
    38 class InvokeGeneric {
       
    39     // erased type for the call, which originates from an invokeGeneric site
       
    40     private final MethodType erasedCallerType;
       
    41     // an invoker of type (MT, MH; A...) -> R
       
    42     private final MethodHandle initialInvoker;
       
    43 
       
    44     /** Compute and cache information for this adapter, so that it can
       
    45      *  call out to targets of the erasure-family of the given erased type.
       
    46      */
       
    47     private InvokeGeneric(MethodType erasedCallerType) throws ReflectiveOperationException {
       
    48         this.erasedCallerType = erasedCallerType;
       
    49         this.initialInvoker = makeInitialInvoker();
       
    50         assert initialInvoker.type().equals(erasedCallerType
       
    51                                             .insertParameterTypes(0, MethodType.class, MethodHandle.class))
       
    52             : initialInvoker.type();
       
    53     }
       
    54 
       
    55     private static MethodHandles.Lookup lookup() {
       
    56         return MethodHandleImpl.IMPL_LOOKUP;
       
    57     }
       
    58 
       
    59     /** Return the adapter information for this type's erasure. */
       
    60     static MethodHandle genericInvokerOf(MethodType type) {
       
    61         MethodTypeImpl form = MethodTypeImpl.of(type);
       
    62         MethodHandle genericInvoker = form.genericInvoker;
       
    63         if (genericInvoker == null) {
       
    64             try {
       
    65                 InvokeGeneric gen = new InvokeGeneric(form.erasedType());
       
    66                 form.genericInvoker = genericInvoker = gen.initialInvoker;
       
    67             } catch (ReflectiveOperationException ex) {
       
    68                 throw new RuntimeException(ex);
       
    69             }
       
    70         }
       
    71         return genericInvoker;
       
    72     }
       
    73 
       
    74     private MethodHandle makeInitialInvoker() throws ReflectiveOperationException {
       
    75         // postDispatch = #(MH'; MT, MH; A...){MH'(MT, MH; A)}
       
    76         MethodHandle postDispatch = makePostDispatchInvoker();
       
    77         MethodHandle invoker;
       
    78         if (returnConversionPossible()) {
       
    79             invoker = MethodHandles.foldArguments(postDispatch,
       
    80                                                   dispatcher("dispatchWithConversion"));
       
    81         } else {
       
    82             invoker = MethodHandles.foldArguments(postDispatch, dispatcher("dispatch"));
       
    83         }
       
    84         return invoker;
       
    85     }
       
    86 
       
    87     private static final Class<?>[] EXTRA_ARGS = { MethodType.class, MethodHandle.class };
       
    88     private MethodHandle makePostDispatchInvoker() {
       
    89         // Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...).
       
    90         MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS);
       
    91         return invokers(invokerType).exactInvoker();
       
    92     }
       
    93     private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) {
       
    94         assert(targetInvoker.type().parameterType(0) == MethodHandle.class);
       
    95         return MethodHandles.dropArguments(targetInvoker, 1, EXTRA_ARGS);
       
    96     }
       
    97 
       
    98     private MethodHandle dispatcher(String dispatchName) throws ReflectiveOperationException {
       
    99         return lookup().bind(this, dispatchName,
       
   100                              MethodType.methodType(MethodHandle.class,
       
   101                                                    MethodType.class, MethodHandle.class));
       
   102     }
       
   103 
       
   104     static final boolean USE_AS_TYPE_PATH = true;
       
   105 
       
   106     /** Return a method handle to invoke on the callerType, target, and remaining arguments.
       
   107      *  The method handle must finish the call.
       
   108      *  This is the first look at the caller type and target.
       
   109      */
       
   110     private MethodHandle dispatch(MethodType callerType, MethodHandle target) {
       
   111         MethodType targetType = target.type();
       
   112         if (USE_AS_TYPE_PATH || target.isVarargsCollector()) {
       
   113             MethodHandle newTarget = target.asType(callerType);
       
   114             targetType = callerType;
       
   115             Invokers invokers = MethodTypeImpl.invokers(Access.TOKEN, targetType);
       
   116             MethodHandle invoker = invokers.erasedInvokerWithDrops;
       
   117             if (invoker == null) {
       
   118                 invokers.erasedInvokerWithDrops = invoker =
       
   119                     dropDispatchArguments(invokers.erasedInvoker());
       
   120             }
       
   121             return invoker.bindTo(newTarget);
       
   122         }
       
   123         throw new RuntimeException("NYI");
       
   124     }
       
   125 
       
   126     private MethodHandle dispatchWithConversion(MethodType callerType, MethodHandle target) {
       
   127         MethodHandle finisher = dispatch(callerType, target);
       
   128         if (returnConversionNeeded(callerType, target))
       
   129             finisher = addReturnConversion(finisher, callerType.returnType());  //FIXME: slow
       
   130         return finisher;
       
   131     }
       
   132 
       
   133     private boolean returnConversionPossible() {
       
   134         Class<?> needType = erasedCallerType.returnType();
       
   135         return !needType.isPrimitive();
       
   136     }
       
   137     private boolean returnConversionNeeded(MethodType callerType, MethodHandle target) {
       
   138         Class<?> needType = callerType.returnType();
       
   139         if (needType == erasedCallerType.returnType())
       
   140             return false;  // no conversions possible, since must be primitive or Object
       
   141         Class<?> haveType = target.type().returnType();
       
   142         if (VerifyType.isNullConversion(haveType, needType))
       
   143             return false;
       
   144         return true;
       
   145     }
       
   146     private MethodHandle addReturnConversion(MethodHandle target, Class<?> type) {
       
   147         if (true) throw new RuntimeException("NYI");
       
   148         // FIXME: This is slow because it creates a closure node on every call that requires a return cast.
       
   149         MethodType targetType = target.type();
       
   150         MethodHandle caster = ValueConversions.identity(type);
       
   151         caster = caster.asType(MethodType.methodType(type, targetType.returnType()));
       
   152         // Drop irrelevant arguments, because we only care about the return value:
       
   153         caster = MethodHandles.dropArguments(caster, 1, targetType.parameterList());
       
   154         MethodHandle result = MethodHandles.foldArguments(caster, target);
       
   155         return result.asType(target.type());
       
   156     }
       
   157 
       
   158     public String toString() {
       
   159         return "InvokeGeneric"+erasedCallerType;
       
   160     }
       
   161 }