nashorn/samples/dynalink/MissingMethodLinkerExporter.java
author kvn
Thu, 31 Mar 2016 09:16:49 -0700
changeset 37276 f61297514617
parent 34549 412a690d4414
child 41422 97eda72f53b6
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     1
/*
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     3
 *
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     6
 * are met:
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     7
 *
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    10
 *
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    14
 *
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    18
 *
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    30
 */
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    31
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    34
import java.lang.invoke.MethodType;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    35
import java.util.ArrayList;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    36
import java.util.List;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    37
import jdk.dynalink.CallSiteDescriptor;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    38
import jdk.dynalink.CompositeOperation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    39
import jdk.dynalink.NamedOperation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    40
import jdk.dynalink.Operation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    41
import jdk.dynalink.StandardOperation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    42
import jdk.dynalink.beans.BeansLinker;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    43
import jdk.dynalink.linker.GuardingDynamicLinker;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    44
import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    45
import jdk.dynalink.linker.GuardedInvocation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    46
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    47
import jdk.dynalink.linker.LinkRequest;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    48
import jdk.dynalink.linker.LinkerServices;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    49
import jdk.dynalink.linker.support.Guards;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    50
import jdk.dynalink.linker.support.Lookup;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    51
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    52
/**
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    53
 * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    54
 * This linker routes missing methods to Smalltalk-style doesNotUnderstand method.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    55
 * Object of any Java class that implements MissingMethodHandler is handled by this linker.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    56
 * For any method call, if a matching Java method is found, it is called. If there is no
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    57
 * method by that name, then MissingMethodHandler.doesNotUnderstand is called.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    58
 */
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    59
public final class MissingMethodLinkerExporter extends GuardingDynamicLinkerExporter {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    60
    static {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    61
        System.out.println("pluggable dynalink missing method linker loaded");
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    62
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    63
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    64
    // represents a MissingMethod - just stores as name and also serves a guard type
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    65
    public static class MissingMethod {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    66
        private final String name;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    67
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    68
        public MissingMethod(String name) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    69
            this.name = name;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    70
        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    71
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    72
        public String getName() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    73
            return name;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    74
        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    75
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    76
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    77
    // MissingMethodHandler.doesNotUnderstand method
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    78
    private static final MethodHandle DOES_NOT_UNDERSTAND;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    79
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    80
    // type of MissingMethodHandler - but "this" and String args are flipped
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    81
    private static final MethodType FLIPPED_DOES_NOT_UNDERSTAND_TYPE;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    82
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    83
    // "is this a MissingMethod?" guard
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    84
    private static final MethodHandle IS_MISSING_METHOD;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    85
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    86
    // MissingMethod object->it's name filter
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    87
    private static final MethodHandle MISSING_METHOD_TO_NAME;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    88
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    89
    static {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    90
        DOES_NOT_UNDERSTAND = Lookup.PUBLIC.findVirtual(
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    91
            MissingMethodHandler.class,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    92
            "doesNotUnderstand",
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    93
            MethodType.methodType(Object.class, String.class, Object[].class));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    94
        FLIPPED_DOES_NOT_UNDERSTAND_TYPE =
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    95
            MethodType.methodType(Object.class, String.class, MissingMethodHandler.class, Object[].class);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    96
        IS_MISSING_METHOD = Guards.isOfClass(MissingMethod.class,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    97
            MethodType.methodType(Boolean.TYPE, Object.class));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    98
        MISSING_METHOD_TO_NAME = Lookup.PUBLIC.findVirtual(MissingMethod.class,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    99
            "getName", MethodType.methodType(String.class));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   100
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   101
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   102
    // locate the first standard operation from the call descriptor
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   103
    private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   104
        final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   105
        if (base instanceof StandardOperation) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   106
            return (StandardOperation)base;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   107
        } else if (base instanceof CompositeOperation) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   108
            final CompositeOperation cop = (CompositeOperation)base;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   109
            for(int i = 0; i < cop.getOperationCount(); ++i) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   110
                final Operation op = cop.getOperation(i);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   111
                if (op instanceof StandardOperation) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   112
                    return (StandardOperation)op;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   113
                }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   114
            }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   115
        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   116
        return null;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   117
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   118
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   119
    @Override
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   120
    public List<GuardingDynamicLinker> get() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   121
        final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   122
        final BeansLinker beansLinker = new BeansLinker();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   123
        linkers.add(new TypeBasedGuardingDynamicLinker() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   124
            // only handles MissingMethodHandler and MissingMethod objects
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   125
            @Override
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   126
            public boolean canLinkType(final Class<?> type) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   127
                return
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   128
                    MissingMethodHandler.class.isAssignableFrom(type) ||
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   129
                    type == MissingMethod.class;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   130
            }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   131
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   132
            @Override
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   133
            public GuardedInvocation getGuardedInvocation(LinkRequest request,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   134
                LinkerServices linkerServices) throws Exception {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   135
                final Object self = request.getReceiver();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   136
                CallSiteDescriptor desc = request.getCallSiteDescriptor();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   137
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   138
                // any method call is done by two steps. Step (1) GET_METHOD and (2) is CALL
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   139
                // For step (1), we check if GET_METHOD can succeed by Java linker, if so
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   140
                // we return that method object. If not, we return a MissingMethod object.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   141
                if (self instanceof MissingMethodHandler) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   142
                    // Check if this is a named GET_METHOD first.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   143
                    boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   144
                    Object name = NamedOperation.getName(desc.getOperation());
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   145
                    if (isGetMethod && name instanceof String) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   146
                        GuardingDynamicLinker javaLinker = beansLinker.getLinkerForClass(self.getClass());
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   147
                        GuardedInvocation inv;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   148
                        try {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   149
                            inv = javaLinker.getGuardedInvocation(request, linkerServices);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   150
                        } catch (final Throwable th) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   151
                            inv = null;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   152
                        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   153
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   154
                        String nameStr = name.toString();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   155
                        if (inv == null) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   156
                            // use "this" for just guard and drop it -- return a constant Method handle
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   157
                            // that returns a newly created MissingMethod object
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   158
                            MethodHandle mh = MethodHandles.constant(Object.class, new MissingMethod(nameStr));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   159
                            inv = new GuardedInvocation(
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   160
                                MethodHandles.dropArguments(mh, 0, Object.class),
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   161
                                Guards.isOfClass(self.getClass(), MethodType.methodType(Boolean.TYPE, Object.class)));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   162
                        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   163
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   164
                        return inv;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   165
                    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   166
                } else if (self instanceof MissingMethod) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   167
                    // This is step (2). We call MissingMethodHandler.doesNotUnderstand here
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   168
                    // Check if this is this a CALL first.
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   169
                    boolean isCall = getFirstStandardOperation(desc) == StandardOperation.CALL;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   170
                    if (isCall) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   171
                        MethodHandle mh = DOES_NOT_UNDERSTAND;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   172
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   173
                        // flip "this" and method name (String)
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   174
                        mh = MethodHandles.permuteArguments(mh, FLIPPED_DOES_NOT_UNDERSTAND_TYPE, 1, 0, 2);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   175
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   176
                        // collect rest of the arguments as vararg
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   177
                        mh = mh.asCollector(Object[].class, desc.getMethodType().parameterCount() - 2);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   178
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   179
                        // convert MissingMethod object to it's name
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   180
                        mh = MethodHandles.filterArguments(mh, 0, MISSING_METHOD_TO_NAME);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   181
                        return new GuardedInvocation(mh, IS_MISSING_METHOD);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   182
                    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   183
                }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   184
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   185
                return null;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   186
            }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   187
        });
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   188
        return linkers;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   189
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   190
}