1 /*
   2  * Copyright (c) 2008, 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 sun.dyn;
  27 
  28 import java.dyn.*;
  29 import sun.dyn.empty.Empty;
  30 
  31 /**
  32  * Construction and caching of often-used invokers.
  33  * @author jrose
  34  */
  35 public class Invokers {
  36     // exact type (sans leading taget MH) for the outgoing call
  37     private final MethodType targetType;
  38 
  39     // exact invoker for the outgoing call
  40     private /*lazy*/ MethodHandle exactInvoker;
  41 
  42     // erased (partially untyped but with primitives) invoker for the outgoing call
  43     private /*lazy*/ MethodHandle erasedInvoker;
  44     /*lazy*/ MethodHandle erasedInvokerWithDrops;  // for InvokeGeneric
  45 
  46     // generic (untyped) invoker for the outgoing call
  47     private /*lazy*/ MethodHandle genericInvoker;
  48 
  49     // generic (untyped) invoker for the outgoing call; accepts a single Object[]
  50     private final /*lazy*/ MethodHandle[] varargsInvokers;
  51 
  52     // invoker for an unbound callsite
  53     private /*lazy*/ MethodHandle uninitializedCallSite;
  54 
  55     /** Compute and cache information common to all collecting adapters
  56      *  that implement members of the erasure-family of the given erased type.
  57      */
  58     public Invokers(Access token, MethodType targetType) {
  59         Access.check(token);
  60         this.targetType = targetType;
  61         this.varargsInvokers = new MethodHandle[targetType.parameterCount()+1];
  62     }
  63 
  64     public static MethodType invokerType(MethodType targetType) {
  65         return targetType.insertParameterTypes(0, MethodHandle.class);
  66     }
  67 
  68     public MethodHandle exactInvoker() {
  69         MethodHandle invoker = exactInvoker;
  70         if (invoker != null)  return invoker;
  71         try {
  72             invoker = MethodHandleImpl.IMPL_LOOKUP.findVirtual(MethodHandle.class, "invoke", targetType);
  73         } catch (NoAccessException ex) {
  74             throw new InternalError("JVM cannot find invoker for "+targetType);
  75         }
  76         assert(invokerType(targetType) == invoker.type());
  77         exactInvoker = invoker;
  78         return invoker;
  79     }
  80 
  81     public MethodHandle genericInvoker() {
  82         MethodHandle invoker1 = exactInvoker();
  83         MethodHandle invoker = genericInvoker;
  84         if (invoker != null)  return invoker;
  85         MethodType genericType = targetType.generic();
  86         invoker = MethodHandles.convertArguments(invoker1, invokerType(genericType));
  87         genericInvoker = invoker;
  88         return invoker;
  89     }
  90 
  91     public MethodHandle erasedInvoker() {
  92         MethodHandle invoker1 = exactInvoker();
  93         MethodHandle invoker = erasedInvoker;
  94         if (invoker != null)  return invoker;
  95         MethodType erasedType = targetType.erase();
  96         if (erasedType == targetType.generic())
  97             invoker = genericInvoker();
  98         else
  99             invoker = MethodHandles.convertArguments(invoker1, invokerType(erasedType));
 100         erasedInvoker = invoker;
 101         return invoker;
 102     }
 103 
 104     public MethodHandle varargsInvoker(int objectArgCount) {
 105         MethodHandle vaInvoker = varargsInvokers[objectArgCount];
 106         if (vaInvoker != null)  return vaInvoker;
 107         MethodHandle gInvoker = genericInvoker();
 108         MethodType vaType = MethodType.genericMethodType(objectArgCount, true);
 109         vaInvoker = MethodHandles.spreadArguments(gInvoker, invokerType(vaType));
 110         varargsInvokers[objectArgCount] = vaInvoker;
 111         return vaInvoker;
 112     }
 113 
 114     private static MethodHandle THROW_UCS = null;
 115 
 116     public MethodHandle uninitializedCallSite() {
 117         MethodHandle invoker = uninitializedCallSite;
 118         if (invoker != null)  return invoker;
 119         if (targetType.parameterCount() > 0) {
 120             MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount());
 121             Invokers invokers0 = MethodTypeImpl.invokers(Access.TOKEN, type0);
 122             invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(),
 123                                                   0, targetType.parameterList());
 124             assert(invoker.type().equals(targetType));
 125             uninitializedCallSite = invoker;
 126             return invoker;
 127         }
 128         if (THROW_UCS == null) {
 129             try {
 130                 THROW_UCS = MethodHandleImpl.IMPL_LOOKUP
 131                     .findStatic(CallSite.class, "uninitializedCallSite",
 132                                 MethodType.methodType(Empty.class));
 133             } catch (NoAccessException ex) {
 134                 throw new RuntimeException(ex);
 135             }
 136         }
 137         invoker = AdapterMethodHandle.makeRetypeRaw(Access.TOKEN, targetType, THROW_UCS);
 138         assert(invoker.type().equals(targetType));
 139         uninitializedCallSite = invoker;
 140         return invoker;
 141     }
 142 
 143     public String toString() {
 144         return "Invokers"+targetType;
 145     }
 146 }