nashorn/samples/dynalink/MissingMethodLinkerExporter.java
author cjplummer
Sat, 06 May 2017 00:05:32 +0000
changeset 46439 6de560f6c1ad
parent 41842 50202a344d28
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.NamedOperation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    39
import jdk.dynalink.NamespaceOperation;
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    40
import jdk.dynalink.Operation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    41
import jdk.dynalink.StandardNamespace;
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    42
import jdk.dynalink.StandardOperation;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    43
import jdk.dynalink.beans.BeansLinker;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    44
import jdk.dynalink.linker.GuardedInvocation;
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    45
import jdk.dynalink.linker.GuardingDynamicLinker;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    46
import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
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;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    49
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    50
import jdk.dynalink.linker.support.Guards;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    51
import jdk.dynalink.linker.support.Lookup;
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
/**
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    54
 * 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
    55
 * 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
    56
 * 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
    57
 * 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
    58
 * 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
    59
 */
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    60
public final class MissingMethodLinkerExporter extends GuardingDynamicLinkerExporter {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    61
    static {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    62
        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
    63
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    64
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    65
    // 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
    66
    public static class MissingMethod {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    67
        private final String name;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    68
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    69
        public MissingMethod(final String name) {
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    70
            this.name = name;
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
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    73
        public String getName() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    74
            return name;
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
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    78
    // MissingMethodHandler.doesNotUnderstand method
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    79
    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
    80
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    81
    // 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
    82
    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
    83
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    84
    // "is this a MissingMethod?" guard
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    85
    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
    86
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    87
    // MissingMethod object->it's name filter
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    88
    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
    89
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    90
    static {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    91
        DOES_NOT_UNDERSTAND = Lookup.PUBLIC.findVirtual(
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    92
            MissingMethodHandler.class,
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    93
            "doesNotUnderstand",
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    94
            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
    95
        FLIPPED_DOES_NOT_UNDERSTAND_TYPE =
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    96
            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
    97
        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
    98
            MethodType.methodType(Boolean.TYPE, Object.class));
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
    99
        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
   100
            "getName", MethodType.methodType(String.class));
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
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   103
    @Override
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   104
    public List<GuardingDynamicLinker> get() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   105
        final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   106
        final BeansLinker beansLinker = new BeansLinker();
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   107
        linkers.add(new TypeBasedGuardingDynamicLinker() {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   108
            // only handles MissingMethodHandler and MissingMethod objects
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   109
            @Override
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   110
            public boolean canLinkType(final Class<?> type) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   111
                return
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   112
                    MissingMethodHandler.class.isAssignableFrom(type) ||
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   113
                    type == MissingMethod.class;
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
            @Override
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   117
            public GuardedInvocation getGuardedInvocation(final LinkRequest request,
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   118
                final LinkerServices linkerServices) throws Exception {
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   119
                final Object self = request.getReceiver();
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   120
                final CallSiteDescriptor desc = request.getCallSiteDescriptor();
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   121
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   122
                // 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
   123
                // 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
   124
                // 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
   125
                if (self instanceof MissingMethodHandler) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   126
                    // Check if this is a named GET_METHOD first.
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   127
                    final Operation namedOp = desc.getOperation();
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   128
                    final Operation namespaceOp = NamedOperation.getBaseOperation(namedOp);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   129
                    final Operation op = NamespaceOperation.getBaseOperation(namespaceOp);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   130
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   131
                    final boolean isGetMethod = op == StandardOperation.GET && StandardNamespace.findFirst(namespaceOp) == StandardNamespace.METHOD;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   132
                    final Object name = NamedOperation.getName(namedOp);
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   133
                    if (isGetMethod && name instanceof String) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   134
                        final GuardingDynamicLinker javaLinker = beansLinker.getLinkerForClass(self.getClass());
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   135
                        GuardedInvocation inv;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   136
                        try {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   137
                            inv = javaLinker.getGuardedInvocation(request, linkerServices);
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   138
                        } catch (final Throwable th) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   139
                            inv = null;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   140
                        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   141
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   142
                        final String nameStr = name.toString();
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   143
                        if (inv == null) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   144
                            // 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
   145
                            // that returns a newly created MissingMethod object
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
   146
                            final MethodHandle mh = MethodHandles.constant(Object.class, new MissingMethod(nameStr));
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   147
                            inv = new GuardedInvocation(
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   148
                                MethodHandles.dropArguments(mh, 0, Object.class),
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   149
                                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
   150
                        }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   151
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   152
                        return inv;
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
                } else if (self instanceof MissingMethod) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   155
                    // 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
   156
                    // Check if this is this a CALL first.
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
   157
                    final boolean isCall = NamedOperation.getBaseOperation(desc.getOperation()) == StandardOperation.CALL;
34457
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   158
                    if (isCall) {
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   159
                        MethodHandle mh = DOES_NOT_UNDERSTAND;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   160
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   161
                        // flip "this" and method name (String)
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   162
                        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
   163
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   164
                        // 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
   165
                        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
   166
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   167
                        // 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
   168
                        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
   169
                        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
   170
                    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   171
                }
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
                return null;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   174
            }
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
        return linkers;
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   177
    }
81a65a2faef3 8144519: Add a sample for pluggable dynalink linker that demonstrats beans linker delegation
sundar
parents:
diff changeset
   178
}