1 /*
   2  * Copyright (c) 2014, 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 java.util.Arrays;
  29 import static java.lang.invoke.LambdaForm.*;
  30 import static java.lang.invoke.LambdaForm.Kind.*;
  31 import static java.lang.invoke.MethodHandleStatics.*;
  32 
  33 /**
  34  * A method handle whose invocation behavior is determined by a target.
  35  * The delegating MH itself can hold extra "intentions" beyond the simple behavior.
  36  * @author jrose
  37  */
  38 /*non-public*/
  39 abstract class DelegatingMethodHandle extends MethodHandle {
  40     protected DelegatingMethodHandle(MethodHandle target) {
  41         this(target.type(), target);
  42     }
  43 
  44     protected DelegatingMethodHandle(MethodType type, MethodHandle target) {
  45         super(type, chooseDelegatingForm(target));
  46     }
  47 
  48     protected DelegatingMethodHandle(MethodType type, LambdaForm form) {
  49         super(type, form);
  50     }
  51 
  52     /** Define this to extract the delegated target which supplies the invocation behavior. */
  53     protected abstract MethodHandle getTarget();
  54 
  55     @Override
  56     abstract MethodHandle asTypeUncached(MethodType newType);
  57 
  58     @Override
  59     MemberName internalMemberName() {
  60         return getTarget().internalMemberName();
  61     }
  62 
  63     @Override
  64     boolean isInvokeSpecial() {
  65         return getTarget().isInvokeSpecial();
  66     }
  67 
  68     @Override
  69     Class<?> internalCallerClass() {
  70         return getTarget().internalCallerClass();
  71     }
  72 
  73     @Override
  74     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
  75         // FIXME: rethink 'copyWith' protocol; it is too low-level for use on all MHs
  76         throw newIllegalArgumentException("do not use this");
  77     }
  78 
  79     @Override
  80     String internalProperties() {
  81         return "\n& Class="+getClass().getSimpleName()+
  82                "\n& Target="+getTarget().debugString();
  83     }
  84 
  85     @Override
  86     BoundMethodHandle rebind() {
  87         return getTarget().rebind();
  88     }
  89 
  90     private static LambdaForm chooseDelegatingForm(MethodHandle target) {
  91         if (target instanceof SimpleMethodHandle)
  92             return target.internalForm();  // no need for an indirection
  93         return makeReinvokerForm(target, MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, NF_getTarget);
  94     }
  95 
  96     static LambdaForm makeReinvokerForm(MethodHandle target,
  97                                         int whichCache,
  98                                         Object constraint,
  99                                         NamedFunction getTargetFn) {
 100         // No pre-action needed.
 101         return makeReinvokerForm(target, whichCache, constraint, true, getTargetFn, null);
 102     }
 103     /** Create a LF which simply reinvokes a target of the given basic type. */
 104     static LambdaForm makeReinvokerForm(MethodHandle target,
 105                                         int whichCache,
 106                                         Object constraint,
 107                                         boolean forceInline,
 108                                         NamedFunction getTargetFn,
 109                                         NamedFunction preActionFn) {
 110         MethodType mtype = target.type().basicType();
 111         Kind kind = whichKind(whichCache);
 112         boolean customized = (whichCache < 0 ||
 113                 mtype.parameterSlotCount() > MethodType.MAX_MH_INVOKER_ARITY);
 114         boolean hasPreAction = (preActionFn != null);
 115         LambdaForm form;
 116         if (!customized) {
 117             form = mtype.form().cachedLambdaForm(whichCache);
 118             if (form != null)  return form;
 119         }
 120         final int THIS_DMH    = 0;
 121         final int ARG_BASE    = 1;
 122         final int ARG_LIMIT   = ARG_BASE + mtype.parameterCount();
 123         int nameCursor = ARG_LIMIT;
 124         final int PRE_ACTION   = hasPreAction ? nameCursor++ : -1;
 125         final int NEXT_MH     = customized ? -1 : nameCursor++;
 126         final int REINVOKE    = nameCursor++;
 127         LambdaForm.Name[] names = LambdaForm.arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 128         assert(names.length == nameCursor);
 129         names[THIS_DMH] = names[THIS_DMH].withConstraint(constraint);
 130         Object[] targetArgs;
 131         if (hasPreAction) {
 132             names[PRE_ACTION] = new LambdaForm.Name(preActionFn, names[THIS_DMH]);
 133         }
 134         if (customized) {
 135             targetArgs = Arrays.copyOfRange(names, ARG_BASE, ARG_LIMIT, Object[].class);
 136             names[REINVOKE] = new LambdaForm.Name(target, targetArgs);  // the invoker is the target itself
 137         } else {
 138             names[NEXT_MH] = new LambdaForm.Name(getTargetFn, names[THIS_DMH]);
 139             targetArgs = Arrays.copyOfRange(names, THIS_DMH, ARG_LIMIT, Object[].class);
 140             targetArgs[0] = names[NEXT_MH];  // overwrite this MH with next MH
 141             names[REINVOKE] = new LambdaForm.Name(mtype, targetArgs);
 142         }
 143         form = new LambdaForm(ARG_LIMIT, names, forceInline, kind);
 144         if (!customized) {
 145             form = mtype.form().setCachedLambdaForm(whichCache, form);
 146         }
 147         return form;
 148     }
 149 
 150     private static Kind whichKind(int whichCache) {
 151         switch(whichCache) {
 152             case MethodTypeForm.LF_REBIND:   return BOUND_REINVOKER;
 153             case MethodTypeForm.LF_DELEGATE: return DELEGATE;
 154             default:                         return REINVOKER;
 155         }
 156     }
 157 
 158     static final NamedFunction NF_getTarget;
 159     static {
 160         try {
 161             NF_getTarget = new NamedFunction(DelegatingMethodHandle.class
 162                                              .getDeclaredMethod("getTarget"));
 163         } catch (ReflectiveOperationException ex) {
 164             throw newInternalError(ex);
 165         }
 166         // The Holder class will contain pre-generated DelegatingMethodHandles resolved
 167         // speculatively using MemberName.getFactory().resolveOrNull. However, that
 168         // doesn't initialize the class, which subtly breaks inlining etc. By forcing
 169         // initialization of the Holder class we avoid these issues.
 170         UNSAFE.ensureClassInitialized(Holder.class);
 171     }
 172 
 173     /* Placeholder class for DelegatingMethodHandles generated ahead of time */
 174     final class Holder {}
 175 }