src/jdk.dynalink/share/classes/jdk/dynalink/beans/BeanLinker.java
author jlaskey
Thu, 21 Jun 2018 08:58:59 -0300
changeset 50695 36ca515343e0
parent 48458 7f57c5908c57
permissions -rw-r--r--
8203637: Fix Sources Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     1
/*
38484
51ee10c31371 8157225: adopt method handle for array length getter in BeanLinker
mhaupt
parents: 34979
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     4
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    10
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    15
 * accompanied this code).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    16
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    20
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    23
 * questions.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    24
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    25
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    26
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    30
 * file, and Oracle licenses the original version of this file under the BSD
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    31
 * license:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    32
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    33
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    34
   Copyright 2009-2013 Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    35
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    36
   Redistribution and use in source and binary forms, with or without
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    37
   modification, are permitted provided that the following conditions are
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    38
   met:
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    39
   * Redistributions of source code must retain the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    40
     notice, this list of conditions and the following disclaimer.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    41
   * Redistributions in binary form must reproduce the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    42
     notice, this list of conditions and the following disclaimer in the
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    43
     documentation and/or other materials provided with the distribution.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    44
   * Neither the name of the copyright holder nor the names of
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    45
     contributors may be used to endorse or promote products derived from
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    46
     this software without specific prior written permission.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    48
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    49
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    50
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    51
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    52
   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    53
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    54
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    55
   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    56
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    57
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 48458
diff changeset
    58
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    61
package jdk.dynalink.beans;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    63
import java.lang.invoke.MethodHandle;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
import java.lang.invoke.MethodHandles;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
import java.lang.invoke.MethodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    66
import java.lang.reflect.Array;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    67
import java.util.Collection;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
    68
import java.util.Collections;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    69
import java.util.List;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    70
import java.util.Map;
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
    71
import java.util.function.Function;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    72
import jdk.dynalink.CallSiteDescriptor;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    73
import jdk.dynalink.Namespace;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    74
import jdk.dynalink.Operation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    75
import jdk.dynalink.StandardNamespace;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    76
import jdk.dynalink.StandardOperation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    77
import jdk.dynalink.beans.GuardedInvocationComponent.ValidationType;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    78
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    79
import jdk.dynalink.linker.LinkerServices;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    80
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    81
import jdk.dynalink.linker.support.Guards;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    82
import jdk.dynalink.linker.support.Lookup;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    83
import jdk.dynalink.linker.support.TypeUtilities;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    84
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
/**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    86
 * A class that provides linking capabilities for a single POJO class. Normally not used directly, but managed by
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
    87
 * {@link BeansLinker}. Most of the functionality is provided by the {@link AbstractJavaLinker} superclass; this
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
    88
 * class adds length and element operations for arrays and collections.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicLinker {
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
    91
    BeanLinker(final Class<?> clazz) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
        super(clazz, Guards.getClassGuard(clazz), Guards.getInstanceOfGuard(clazz));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
        if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
            // Some languages won't have a notion of manipulating collections. Exposing "length" on arrays as an
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    95
            // explicit property is beneficial for them.
48458
7f57c5908c57 8157251: BeanLinker relinks array length operations for array types
hannesw
parents: 48354
diff changeset
    96
            setPropertyGetter("length", GET_ARRAY_LENGTH, ValidationType.IS_ARRAY);
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    97
        } else if(Collection.class.isAssignableFrom(clazz)) {
23770
cdedd86695a0 8039387: Nashorn supports indexed access of List elements, but length property is not supported
sundar
parents: 16245
diff changeset
    98
            setPropertyGetter("length", GET_COLLECTION_LENGTH, ValidationType.INSTANCE_OF);
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    99
        } else if(Map.class.isAssignableFrom(clazz)) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   100
            setPropertyGetter("length", GET_MAP_LENGTH, ValidationType.INSTANCE_OF);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   102
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   104
    @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   105
    public boolean canLinkType(final Class<?> type) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
        return type == clazz;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   107
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
    @Override
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
    FacetIntrospector createFacetIntrospector() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
        return new BeanIntrospector(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
    @Override
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   115
    protected GuardedInvocationComponent getGuardedInvocationComponent(final ComponentLinkRequest req) throws Exception {
48354
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   116
        if (req.namespaces.isEmpty()) {
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   117
            return null;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
        }
48354
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   119
        final Namespace ns = req.namespaces.get(0);
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   120
        if (ns == StandardNamespace.ELEMENT) {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   121
            final Operation op = req.baseOperation;
48354
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   122
            if (op == StandardOperation.GET) {
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   123
                return getElementGetter(req.popNamespace());
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   124
            } else if (op == StandardOperation.SET) {
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   125
                return getElementSetter(req.popNamespace());
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   126
            } else if (op == StandardOperation.REMOVE) {
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   127
                return getElementRemover(req.popNamespace());
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   128
            }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
        }
48354
c96d4c720995 8193371: Use Dynalink REMOVE operation in Nashorn
attila
parents: 48244
diff changeset
   130
        return super.getGuardedInvocationComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
33688
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   133
    @Override
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   134
    SingleDynamicMethod getConstructorMethod(final String signature) {
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   135
        return null;
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   136
    }
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   137
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   138
    private static final MethodHandle GET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "get",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   139
            MethodType.methodType(Object.class, int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   140
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   141
    private static final MethodHandle GET_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "get",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   142
            MethodType.methodType(Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   143
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   144
    private static final MethodHandle LIST_GUARD = Guards.getInstanceOfGuard(List.class);
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   145
    private static final MethodHandle MAP_GUARD = Guards.getInstanceOfGuard(Map.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   146
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   147
    private static final MethodHandle NULL_GETTER_1;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   148
    private static final MethodHandle NULL_GETTER_2;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   149
    static {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   150
        final MethodHandle constantNull = MethodHandles.constant(Object.class, null);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   151
        NULL_GETTER_1 = dropObjectArguments(constantNull, 1);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   152
        NULL_GETTER_2 = dropObjectArguments(constantNull, 2);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   153
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   154
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   155
    private static MethodHandle dropObjectArguments(final MethodHandle m, final int n) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   156
        return MethodHandles.dropArguments(m, 0, Collections.nCopies(n, Object.class));
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   157
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   158
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   159
    private enum CollectionType {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   160
        ARRAY, LIST, MAP
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   161
    };
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   162
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   163
    private GuardedInvocationComponent getElementGetter(final ComponentLinkRequest req) throws Exception {
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   164
        final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   165
        final Object name = req.name;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   166
        final boolean isFixedKey = name != null;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   167
        assertParameterCount(callSiteDescriptor, isFixedKey ? 1 : 2);
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   168
        final LinkerServices linkerServices = req.linkerServices;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   169
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   170
        final GuardedInvocationComponent nextComponent = getNextComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   171
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   172
        final GuardedInvocationComponentAndCollectionType gicact = guardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   173
                callSiteType, linkerServices, MethodHandles::arrayElementGetter, GET_LIST_ELEMENT, GET_MAP_ELEMENT);
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   174
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   175
        if (gicact == null) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   176
            // Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   177
            return nextComponent;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   178
        }
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   179
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   180
        final Object typedName = getTypedName(name, gicact.collectionType == CollectionType.MAP, linkerServices);
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   181
        if (typedName == INVALID_NAME) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   182
            return nextComponent;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   183
        }
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   184
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   185
        return guardComponentWithRangeCheck(gicact, callSiteType, nextComponent,
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   186
                new Binder(linkerServices, callSiteType, typedName), isFixedKey ? NULL_GETTER_1 : NULL_GETTER_2);
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   187
    }
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   188
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   189
    private static class GuardedInvocationComponentAndCollectionType {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   190
        final GuardedInvocationComponent gic;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   191
        final CollectionType collectionType;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   192
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   193
        GuardedInvocationComponentAndCollectionType(final GuardedInvocationComponent gic, final CollectionType collectionType) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   194
            this.gic = gic;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   195
            this.collectionType = collectionType;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   196
        }
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   197
    }
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   198
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   199
    private GuardedInvocationComponentAndCollectionType guardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   200
            final MethodType callSiteType, final LinkerServices linkerServices,
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   201
            final Function<Class<?>, MethodHandle> arrayMethod, final MethodHandle listMethod, final MethodHandle mapMethod) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   202
        final Class<?> declaredType = callSiteType.parameterType(0);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
        // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
        // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
        // dealing with an array, or a list or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   206
        // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   207
        // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   208
        if(declaredType.isArray() && arrayMethod != null) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   209
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   210
                    createInternalFilteredGuardedInvocationComponent(arrayMethod.apply(declaredType), linkerServices),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   211
                    CollectionType.ARRAY);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
        } else if(List.class.isAssignableFrom(declaredType)) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   213
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   214
                    createInternalFilteredGuardedInvocationComponent(listMethod, linkerServices),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   215
                    CollectionType.LIST);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   216
        } else if(Map.class.isAssignableFrom(declaredType)) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   217
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   218
                    createInternalFilteredGuardedInvocationComponent(mapMethod, linkerServices),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   219
                    CollectionType.MAP);
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   220
        } else if(clazz.isArray() && arrayMethod != null) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   221
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   222
                    getClassGuardedInvocationComponent(linkerServices.filterInternalObjects(arrayMethod.apply(clazz)), callSiteType),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   223
                    CollectionType.ARRAY);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
        } else if(List.class.isAssignableFrom(clazz)) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   225
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   226
                    createInternalFilteredGuardedInvocationComponent(listMethod, Guards.asType(LIST_GUARD, callSiteType),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   227
                            List.class, ValidationType.INSTANCE_OF, linkerServices),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   228
                    CollectionType.LIST);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   229
        } else if(Map.class.isAssignableFrom(clazz)) {
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   230
            return new GuardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   231
                    createInternalFilteredGuardedInvocationComponent(mapMethod, Guards.asType(MAP_GUARD, callSiteType),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   232
                            Map.class, ValidationType.INSTANCE_OF, linkerServices),
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   233
                    CollectionType.MAP);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   234
        }
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   235
        return null;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   236
    }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   238
    private static final Object INVALID_NAME = new Object();
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   239
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   240
    private static Object getTypedName(final Object name, final boolean isMap, final LinkerServices linkerServices) throws Exception {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   241
        // Convert the key to a number if we're working with a list or array
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   242
        if (!isMap && name != null) {
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   243
            final Integer integer = convertKeyToInteger(name, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   244
            if (integer == null || integer.intValue() < 0) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   245
                // key is not a non-negative integer, it can never address an
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   246
                // array or list element
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   247
                return INVALID_NAME;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   248
            }
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   249
            return integer;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   250
        }
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   251
        return name;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   252
    }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   253
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   254
    private static GuardedInvocationComponent guardComponentWithRangeCheck(
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   255
            final GuardedInvocationComponentAndCollectionType gicact, final MethodType callSiteType,
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   256
            final GuardedInvocationComponent nextComponent, final Binder binder, final MethodHandle noOp) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   257
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   258
        final MethodHandle checkGuard;
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   259
        switch(gicact.collectionType) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   260
            case LIST:
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   261
                checkGuard = binder.convertArgToNumber(RANGE_CHECK_LIST);
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   262
                break;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   263
            case MAP:
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   264
                checkGuard = binder.linkerServices.filterInternalObjects(CONTAINS_MAP);
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   265
                break;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   266
            case ARRAY:
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   267
                checkGuard = binder.convertArgToNumber(RANGE_CHECK_ARRAY);
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   268
                break;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   269
            default:
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   270
                throw new AssertionError();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   271
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   272
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   273
        // If there's no next component, produce a fixed no-op one
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   274
        final GuardedInvocationComponent finalNextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   275
        if (nextComponent != null) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   276
            finalNextComponent = nextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   277
        } else {
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   278
            finalNextComponent = createGuardedInvocationComponentAsType(noOp, callSiteType, binder.linkerServices);
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   279
        }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   280
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   281
        final GuardedInvocationComponent gic = gicact.gic;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   282
        final GuardedInvocation gi = gic.getGuardedInvocation();
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   283
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   284
        final MethodPair matchedInvocations = matchReturnTypes(binder.bind(gi.getInvocation()),
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   285
                finalNextComponent.getGuardedInvocation().getInvocation());
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   286
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   287
        return finalNextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   288
                gic.getValidatorClass(), gic.getValidationType());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   289
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   290
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   291
    private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   292
            final MethodHandle invocation, final LinkerServices linkerServices) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   293
        return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation));
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   294
    }
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   295
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   296
    private static GuardedInvocationComponent createGuardedInvocationComponentAsType(
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   297
            final MethodHandle invocation, final MethodType fromType, final LinkerServices linkerServices) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   298
        return new GuardedInvocationComponent(linkerServices.asType(invocation, fromType));
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   299
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   300
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   301
    private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   302
            final MethodHandle invocation, final MethodHandle guard, final Class<?> validatorClass,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   303
            final ValidationType validationType, final LinkerServices linkerServices) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   304
        return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation), guard,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   305
                validatorClass, validationType);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   306
    }
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   307
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   308
    private static Integer convertKeyToInteger(final Object fixedKey, final LinkerServices linkerServices) throws Exception {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   309
        if (fixedKey instanceof Integer) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   310
            return (Integer)fixedKey;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   311
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   312
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   313
        final Number n;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   314
        if (fixedKey instanceof Number) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   315
            n = (Number)fixedKey;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   316
        } else {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   317
            final Class<?> keyClass = fixedKey.getClass();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   318
            if(linkerServices.canConvert(keyClass, Number.class)) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   319
                final Object val;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   320
                try {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   321
                    val = linkerServices.getTypeConverter(keyClass, Number.class).invoke(fixedKey);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   322
                } catch(Exception|Error e) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   323
                    throw e;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   324
                } catch(final Throwable t) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   325
                    throw new RuntimeException(t);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   326
                }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   327
                if(!(val instanceof Number)) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   328
                    return null; // not a number
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   329
                }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   330
                n = (Number)val;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   331
            } else if (fixedKey instanceof String){
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   332
                try {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   333
                    return Integer.valueOf((String)fixedKey);
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   334
                } catch(final NumberFormatException e) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   335
                    // key is not a number
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   336
                    return null;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   337
                }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   338
            } else {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   339
                return null;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   340
            }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   341
        }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   342
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   343
        if(n instanceof Integer) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   344
            return (Integer)n;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   345
        }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   346
        final int intIndex = n.intValue();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   347
        final double doubleValue = n.doubleValue();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   348
        if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinites trigger IOOBE
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   349
            return null; // not an exact integer
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   350
        }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   351
        return intIndex;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   352
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   353
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   354
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
     * Contains methods to adapt an item getter/setter method handle to the requested type, optionally binding it to a
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   356
     * fixed key first.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
    private static class Binder {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   359
        private final LinkerServices linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   360
        private final MethodType methodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   361
        private final Object fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   362
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   363
        Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   364
            this.linkerServices = linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   365
            this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   366
            this.fixedKey = fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   367
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   368
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   369
        /*private*/ MethodHandle bind(final MethodHandle handle) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   370
            return bindToFixedKey(linkerServices.asTypeLosslessReturn(handle, methodType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   371
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   372
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   373
        /*private*/ MethodHandle bindTest(final MethodHandle handle) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   374
            return bindToFixedKey(Guards.asType(handle, methodType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   375
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   376
48219
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   377
        /*private*/ MethodHandle convertArgToNumber(final MethodHandle mh) {
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   378
            final Class<?> sourceType = methodType.parameterType(1);
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   379
            if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   380
                return mh;
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   381
            } else if(linkerServices.canConvert(sourceType, Number.class)) {
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   382
                final MethodHandle converter = linkerServices.getTypeConverter(sourceType, Number.class);
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   383
                return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   384
                        mh.type().parameterType(1))));
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   385
            }
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   386
            return mh;
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   387
        }
f3b561b13ddf 8192970: Element getters/setters with fixed key fail to link properly
attila
parents: 47947
diff changeset
   388
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   389
        private MethodHandle bindToFixedKey(final MethodHandle handle) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   390
            return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   391
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   392
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   393
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   394
    private static final MethodHandle RANGE_CHECK_ARRAY = findRangeCheck(Object.class);
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   395
    private static final MethodHandle RANGE_CHECK_LIST = findRangeCheck(List.class);
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   396
    private static final MethodHandle CONTAINS_MAP = Lookup.PUBLIC.findVirtual(Map.class, "containsKey",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   397
            MethodType.methodType(boolean.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   399
    private static MethodHandle findRangeCheck(final Class<?> collectionType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   400
        return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   401
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   402
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   403
    @SuppressWarnings("unused")
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   404
    private static boolean rangeCheck(final Object array, final Object index) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   407
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   408
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   409
        final int intIndex = n.intValue();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   410
        if (intIndex != n.doubleValue()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   411
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   412
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   413
        return 0 <= intIndex && intIndex < Array.getLength(array);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   414
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   415
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   416
    @SuppressWarnings("unused")
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   417
    private static boolean rangeCheck(final List<?> list, final Object index) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   418
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   419
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   420
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   421
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   422
        final int intIndex = n.intValue();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   423
        if (intIndex != n.doubleValue()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   424
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   425
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   426
        return 0 <= intIndex && intIndex < list.size();
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   427
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   428
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   429
    @SuppressWarnings("unused")
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   430
    private static void noOp() {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   431
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   432
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   433
    private static final MethodHandle SET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "set",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   434
            MethodType.methodType(Object.class, int.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   435
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   436
    private static final MethodHandle PUT_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "put",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   437
            MethodType.methodType(Object.class, Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   438
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   439
    private static final MethodHandle NO_OP_1;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   440
    private static final MethodHandle NO_OP_2;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   441
    private static final MethodHandle NO_OP_3;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   442
    static {
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   443
        final MethodHandle noOp = Lookup.findOwnStatic(MethodHandles.lookup(), "noOp", void.class);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   444
        NO_OP_1 = dropObjectArguments(noOp, 1);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   445
        NO_OP_2 = dropObjectArguments(noOp, 2);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   446
        NO_OP_3 = dropObjectArguments(noOp, 3);
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   447
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   448
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   449
    private GuardedInvocationComponent getElementSetter(final ComponentLinkRequest req) throws Exception {
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   450
        final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   451
        final Object name = req.name;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   452
        final boolean isFixedKey = name != null;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   453
        assertParameterCount(callSiteDescriptor, isFixedKey ? 2 : 3);
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   454
        final LinkerServices linkerServices = req.linkerServices;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   455
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   456
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   457
        final GuardedInvocationComponentAndCollectionType gicact = guardedInvocationComponentAndCollectionType(
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   458
                callSiteType, linkerServices, MethodHandles::arrayElementSetter, SET_LIST_ELEMENT, PUT_MAP_ELEMENT);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   459
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   460
        if(gicact == null) {
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   461
            return getNextComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   462
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   463
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   464
        final boolean isMap = gicact.collectionType == CollectionType.MAP;
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   465
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   466
        // In contrast to, say, getElementGetter, we only compute the nextComponent if the target object is not a map,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   467
        // as maps will always succeed in setting the element and will never need to fall back to the next component
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   468
        // operation.
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   469
        final GuardedInvocationComponent nextComponent = isMap ? null : getNextComponent(req);
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   470
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   471
        final Object typedName = getTypedName(name, isMap, linkerServices);
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   472
        if (typedName == INVALID_NAME) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   473
            return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   474
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   475
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   476
        final GuardedInvocationComponent gic = gicact.gic;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   477
        final GuardedInvocation gi = gic.getGuardedInvocation();
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   478
        final Binder binder = new Binder(linkerServices, callSiteType, typedName);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   479
        final MethodHandle invocation = gi.getInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   480
47947
5dab5e8f06a3 8191878: Reduce code duplication in BeanLinker
attila
parents: 47216
diff changeset
   481
        if (isMap) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   482
            return gic.replaceInvocation(binder.bind(invocation));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   483
        }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   484
48244
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   485
        return guardComponentWithRangeCheck(gicact, callSiteType, nextComponent, binder, isFixedKey ? NO_OP_2 : NO_OP_3);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   486
    }
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   487
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   488
    private static final MethodHandle REMOVE_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "remove",
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   489
            MethodType.methodType(Object.class, int.class));
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   490
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   491
    private static final MethodHandle REMOVE_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "remove",
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   492
            MethodType.methodType(Object.class, Object.class));
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   493
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   494
    private GuardedInvocationComponent getElementRemover(final ComponentLinkRequest req) throws Exception {
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   495
        final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   496
        final Object name = req.name;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   497
        final boolean isFixedKey = name != null;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   498
        assertParameterCount(callSiteDescriptor, isFixedKey ? 1 : 2);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   499
        final LinkerServices linkerServices = req.linkerServices;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   500
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   501
        final GuardedInvocationComponent nextComponent = getNextComponent(req);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   502
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   503
        final GuardedInvocationComponentAndCollectionType gicact = guardedInvocationComponentAndCollectionType(
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   504
                callSiteType, linkerServices, null, REMOVE_LIST_ELEMENT, REMOVE_MAP_ELEMENT);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   505
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   506
        if (gicact == null) {
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   507
            // Can't remove elements for objects that are neither lists, nor maps.
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   508
            return nextComponent;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   509
        }
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   510
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   511
        final Object typedName = getTypedName(name, gicact.collectionType == CollectionType.MAP, linkerServices);
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   512
        if (typedName == INVALID_NAME) {
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   513
            return nextComponent;
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   514
        }
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   515
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   516
        return guardComponentWithRangeCheck(gicact, callSiteType, nextComponent,
2bf9071e8dce 8191905: Add a REMOVE StandardOperation to Dynalink
attila
parents: 48219
diff changeset
   517
                new Binder(linkerServices, callSiteType, typedName), isFixedKey ? NO_OP_1: NO_OP_2);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   518
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   519
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   520
    private static final MethodHandle GET_COLLECTION_LENGTH = Lookup.PUBLIC.findVirtual(Collection.class, "size",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   521
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   522
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   523
    private static final MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size",
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   524
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   525
48458
7f57c5908c57 8157251: BeanLinker relinks array length operations for array types
hannesw
parents: 48354
diff changeset
   526
    private static final MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength",
7f57c5908c57 8157251: BeanLinker relinks array length operations for array types
hannesw
parents: 48354
diff changeset
   527
            MethodType.methodType(int.class, Object.class));
7f57c5908c57 8157251: BeanLinker relinks array length operations for array types
hannesw
parents: 48354
diff changeset
   528
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   529
    private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   530
        if(descriptor.getMethodType().parameterCount() != paramCount) {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   531
            throw new BootstrapMethodError(descriptor.getOperation() + " must have exactly " + paramCount + " parameters.");
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   532
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   533
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   534
}