Home » openjdk-7 » java.lang.invoke » [javadoc | source]

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

Home » openjdk-7 » java.lang.invoke » [javadoc | source]