InvokeGeneric.java 7.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.dyn;

import java.dyn.*;
import java.lang.reflect.*;
import sun.dyn.util.*;
31
import static sun.dyn.MethodTypeImpl.invokers;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/**
 * Adapters which manage MethodHanndle.invokeGeneric calls.
 * The JVM calls one of these when the exact type match fails.
 * @author jrose
 */
class InvokeGeneric {
    // erased type for the call, which originates from an invokeGeneric site
    private final MethodType erasedCallerType;
    // an invoker of type (MT, MH; A...) -> R
    private final MethodHandle initialInvoker;

    /** Compute and cache information for this adapter, so that it can
     *  call out to targets of the erasure-family of the given erased type.
     */
    private InvokeGeneric(MethodType erasedCallerType) throws NoAccessException {
        this.erasedCallerType = erasedCallerType;
        this.initialInvoker = makeInitialInvoker();
        assert initialInvoker.type().equals(erasedCallerType
                                            .insertParameterTypes(0, MethodType.class, MethodHandle.class))
            : initialInvoker.type();
    }

    private static MethodHandles.Lookup lookup() {
        return MethodHandleImpl.IMPL_LOOKUP;
    }

    /** Return the adapter information for this type's erasure. */
    static MethodHandle genericInvokerOf(MethodType type) {
        MethodTypeImpl form = MethodTypeImpl.of(type);
        MethodHandle genericInvoker = form.genericInvoker;
        if (genericInvoker == null) {
            try {
                InvokeGeneric gen = new InvokeGeneric(form.erasedType());
                form.genericInvoker = genericInvoker = gen.initialInvoker;
            } catch (NoAccessException ex) {
                throw new RuntimeException(ex);
            }
        }
        return genericInvoker;
    }

    private MethodHandle makeInitialInvoker() throws NoAccessException {
        // postDispatch = #(MH'; MT, MH; A...){MH'(MT, MH; A)}
        MethodHandle postDispatch = makePostDispatchInvoker();
        MethodHandle invoker;
        if (returnConversionPossible()) {
            invoker = MethodHandles.foldArguments(postDispatch,
                                                  dispatcher("dispatchWithConversion"));
        } else {
            invoker = MethodHandles.foldArguments(postDispatch, dispatcher("dispatch"));
        }
        return invoker;
    }

    private static final Class<?>[] EXTRA_ARGS = { MethodType.class, MethodHandle.class };
    private MethodHandle makePostDispatchInvoker() {
        // Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...).
        MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS);
91
        return invokers(invokerType).exactInvoker();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }
    private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) {
        assert(targetInvoker.type().parameterType(0) == MethodHandle.class);
        return MethodHandles.dropArguments(targetInvoker, 1, EXTRA_ARGS);
    }

    private MethodHandle dispatcher(String dispatchName) throws NoAccessException {
        return lookup().bind(this, dispatchName,
                             MethodType.methodType(MethodHandle.class,
                                                   MethodType.class, MethodHandle.class));
    }

    static final boolean USE_AS_TYPE_PATH = true;

    /** Return a method handle to invoke on the callerType, target, and remaining arguments.
     *  The method handle must finish the call.
     *  This is the first look at the caller type and target.
     */
    private MethodHandle dispatch(MethodType callerType, MethodHandle target) {
        MethodType targetType = target.type();
112
        if (USE_AS_TYPE_PATH || target.isVarargsCollector()) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            MethodHandle newTarget = target.asType(callerType);
            targetType = callerType;
            Invokers invokers = MethodTypeImpl.invokers(Access.TOKEN, targetType);
            MethodHandle invoker = invokers.erasedInvokerWithDrops;
            if (invoker == null) {
                invokers.erasedInvokerWithDrops = invoker =
                    dropDispatchArguments(invokers.erasedInvoker());
            }
            return invoker.bindTo(newTarget);
        }
        throw new RuntimeException("NYI");
    }

    private MethodHandle dispatchWithConversion(MethodType callerType, MethodHandle target) {
        MethodHandle finisher = dispatch(callerType, target);
        if (returnConversionNeeded(callerType, target))
            finisher = addReturnConversion(finisher, callerType.returnType());  //FIXME: slow
        return finisher;
    }

    private boolean returnConversionPossible() {
        Class<?> needType = erasedCallerType.returnType();
        return !needType.isPrimitive();
    }
    private boolean returnConversionNeeded(MethodType callerType, MethodHandle target) {
        Class<?> needType = callerType.returnType();
        if (needType == erasedCallerType.returnType())
            return false;  // no conversions possible, since must be primitive or Object
        Class<?> haveType = target.type().returnType();
        if (VerifyType.isNullConversion(haveType, needType))
            return false;
        return true;
    }
    private MethodHandle addReturnConversion(MethodHandle target, Class<?> type) {
        if (true) throw new RuntimeException("NYI");
        // FIXME: This is slow because it creates a closure node on every call that requires a return cast.
        MethodType targetType = target.type();
        MethodHandle caster = ValueConversions.identity(type);
        caster = caster.asType(MethodType.methodType(type, targetType.returnType()));
        // Drop irrelevant arguments, because we only care about the return value:
        caster = MethodHandles.dropArguments(caster, 1, targetType.parameterList());
        MethodHandle result = MethodHandles.foldArguments(caster, target);
        return result.asType(target.type());
    }

    public String toString() {
        return "InvokeGeneric"+erasedCallerType;
    }
}