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