nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/beans/BeanLinker.java
author stuefe
Mon, 13 Mar 2017 21:46:37 -0400
changeset 44319 8c42183dbd04
parent 41842 50202a344d28
permissions -rw-r--r--
8176442: [aix] assert(_thr_current == 0L) failed: Thread::current already initialized Summary: Revert Thread::current() back to pthread library based TLS on AIX. Reviewed-by: dholmes
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    36
   Licensed under both the Apache License, Version 2.0 (the "Apache License")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    37
   and the BSD License (the "BSD License"), with licensee being free to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    38
   choose either of the two at their discretion.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    39
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    40
   You may not use this file except in compliance with either the Apache
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    41
   License or the BSD License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    42
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    43
   If you choose to use this file in compliance with the Apache License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    44
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    45
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    46
       You may obtain a copy of the Apache License at
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    48
           http://www.apache.org/licenses/LICENSE-2.0
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    49
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    50
       Unless required by applicable law or agreed to in writing, software
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    51
       distributed under the License is distributed on an "AS IS" BASIS,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    52
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    53
       implied. See the License for the specific language governing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    54
       permissions and limitations under the License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    55
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    56
   If you choose to use this file in compliance with the BSD License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    57
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    58
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
       Redistribution and use in source and binary forms, with or without
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
       modification, are permitted provided that the following conditions are
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    61
       met:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
       * Redistributions of source code must retain the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    63
         notice, this list of conditions and the following disclaimer.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
       * Redistributions in binary form must reproduce the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
         notice, this list of conditions and the following disclaimer in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    66
         documentation and/or other materials provided with the distribution.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    67
       * Neither the name of the copyright holder nor the names of
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    68
         contributors may be used to endorse or promote products derived from
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    69
         this software without specific prior written permission.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    70
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    71
       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    72
       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    73
       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    74
       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    75
       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    76
       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    77
       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    78
       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    79
       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    80
       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    81
       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    82
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    83
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    84
package jdk.dynalink.beans;
16234
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
import java.lang.invoke.MethodHandle;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.lang.invoke.MethodHandles;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.lang.invoke.MethodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
import java.lang.reflect.Array;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
import java.util.Collection;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
    91
import java.util.Collections;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
import java.util.List;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
import java.util.Map;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    94
import jdk.dynalink.CallSiteDescriptor;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    95
import jdk.dynalink.Namespace;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    96
import jdk.dynalink.Operation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
    97
import jdk.dynalink.StandardNamespace;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    98
import jdk.dynalink.StandardOperation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    99
import jdk.dynalink.beans.GuardedInvocationComponent.ValidationType;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   100
import jdk.dynalink.linker.GuardedInvocation;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   101
import jdk.dynalink.linker.LinkerServices;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   102
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   103
import jdk.dynalink.linker.support.Guards;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   104
import jdk.dynalink.linker.support.Lookup;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
   105
import jdk.dynalink.linker.support.TypeUtilities;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
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
 * A class that provides linking capabilities for a single POJO class. Normally not used directly, but managed by
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
 * {@link BeansLinker}.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicLinker {
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   112
    BeanLinker(final Class<?> clazz) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
        super(clazz, Guards.getClassGuard(clazz), Guards.getInstanceOfGuard(clazz));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
        if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
            // 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
   116
            // explicit property is beneficial for them.
38486
c348779a6c2b 8157250: BeanLinker assumes fixed array type linkage
mhaupt
parents: 38484
diff changeset
   117
            setPropertyGetter("length", MethodHandles.arrayLength(clazz), ValidationType.EXACT_CLASS);
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   118
        } 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
   119
            setPropertyGetter("length", GET_COLLECTION_LENGTH, ValidationType.INSTANCE_OF);
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   120
        } else if(Map.class.isAssignableFrom(clazz)) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   121
            setPropertyGetter("length", GET_MAP_LENGTH, ValidationType.INSTANCE_OF);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   125
    @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   126
    public boolean canLinkType(final Class<?> type) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   127
        return type == clazz;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   128
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   130
    @Override
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
    FacetIntrospector createFacetIntrospector() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
        return new BeanIntrospector(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   135
    @Override
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   136
    protected GuardedInvocationComponent getGuardedInvocationComponent(final ComponentLinkRequest req) throws Exception {
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   137
        final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   138
        if(superGic != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   139
            return superGic;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   140
        }
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   141
        if (!req.namespaces.isEmpty()) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   142
            final Operation op = req.baseOperation;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   143
            final Namespace ns = req.namespaces.get(0);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   144
            if (ns == StandardNamespace.ELEMENT) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   145
                if (op == StandardOperation.GET) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   146
                    return getElementGetter(req.popNamespace());
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   147
                } else if (op == StandardOperation.SET) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 38486
diff changeset
   148
                    return getElementSetter(req.popNamespace());
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   149
                }
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   150
            }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   151
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   152
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   153
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   154
33688
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   155
    @Override
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   156
    SingleDynamicMethod getConstructorMethod(final String signature) {
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   157
        return null;
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   158
    }
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33343
diff changeset
   159
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   160
    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
   161
            MethodType.methodType(Object.class, int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   162
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   163
    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
   164
            MethodType.methodType(Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   165
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   166
    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
   167
    private static final MethodHandle MAP_GUARD = Guards.getInstanceOfGuard(Map.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   168
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   169
    private static final MethodHandle NULL_GETTER_1;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   170
    private static final MethodHandle NULL_GETTER_2;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   171
    static {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   172
        final MethodHandle constantNull = MethodHandles.constant(Object.class, null);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   173
        NULL_GETTER_1 = dropObjectArguments(constantNull, 1);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   174
        NULL_GETTER_2 = dropObjectArguments(constantNull, 2);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   175
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   176
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   177
    private static MethodHandle dropObjectArguments(final MethodHandle m, final int n) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   178
        return MethodHandles.dropArguments(m, 0, Collections.nCopies(n, Object.class));
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   179
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   180
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   181
    private enum CollectionType {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   182
        ARRAY, LIST, MAP
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   183
    };
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   184
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   185
    private GuardedInvocationComponent getElementGetter(final ComponentLinkRequest req) throws Exception {
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   186
        final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   187
        final Object name = req.name;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   188
        final boolean isFixedKey = name != null;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   189
        assertParameterCount(callSiteDescriptor, isFixedKey ? 1 : 2);
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   190
        final LinkerServices linkerServices = req.linkerServices;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   191
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
        final Class<?> declaredType = callSiteType.parameterType(0);
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   193
        final GuardedInvocationComponent nextComponent = getNextComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   195
        // 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
   196
        // 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
   197
        // dealing with an array, or a list or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   198
        // 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
   199
        // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   200
        final GuardedInvocationComponent gic;
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   201
        final CollectionType collectionType;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
        if(declaredType.isArray()) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   203
            gic = createInternalFilteredGuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType), linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   204
            collectionType = CollectionType.ARRAY;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
        } else if(List.class.isAssignableFrom(declaredType)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   206
            gic = createInternalFilteredGuardedInvocationComponent(GET_LIST_ELEMENT, linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   207
            collectionType = CollectionType.LIST;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
        } else if(Map.class.isAssignableFrom(declaredType)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   209
            gic = createInternalFilteredGuardedInvocationComponent(GET_MAP_ELEMENT, linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   210
            collectionType = CollectionType.MAP;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   211
        } else if(clazz.isArray()) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   212
            gic = getClassGuardedInvocationComponent(linkerServices.filterInternalObjects(MethodHandles.arrayElementGetter(clazz)), callSiteType);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   213
            collectionType = CollectionType.ARRAY;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
        } else if(List.class.isAssignableFrom(clazz)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   215
            gic = createInternalFilteredGuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, ValidationType.INSTANCE_OF,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   216
                    linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   217
            collectionType = CollectionType.LIST;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   218
        } else if(Map.class.isAssignableFrom(clazz)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   219
            gic = createInternalFilteredGuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class, ValidationType.INSTANCE_OF,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   220
                    linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   221
            collectionType = CollectionType.MAP;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   222
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   223
            // Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
            return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   225
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   226
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   227
        // Convert the key to a number if we're working with a list or array
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   228
        final Object typedName;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   229
        if (collectionType != CollectionType.MAP && isFixedKey) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   230
            final Integer integer = convertKeyToInteger(name, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   231
            if (integer == null || integer.intValue() < 0) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   232
                // 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
   233
                // array or list element
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   234
                return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
            }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   236
            typedName = integer;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
        } else {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   238
            typedName = name;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   239
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   240
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   241
        final GuardedInvocation gi = gic.getGuardedInvocation();
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   242
        final Binder binder = new Binder(linkerServices, callSiteType, typedName);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   243
        final MethodHandle invocation = gi.getInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   244
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   245
        final MethodHandle checkGuard;
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   246
        switch(collectionType) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   247
        case LIST:
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   248
            checkGuard = convertArgToNumber(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   249
            break;
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   250
        case MAP:
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   251
            checkGuard = linkerServices.filterInternalObjects(CONTAINS_MAP);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   252
            break;
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   253
        case ARRAY:
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   254
            checkGuard = convertArgToNumber(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   255
            break;
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   256
        default:
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   257
            throw new AssertionError();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   259
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   260
        // If there's no next component, produce a fixed null-returning one
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   261
        final GuardedInvocationComponent finalNextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   262
        if (nextComponent != null) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   263
            finalNextComponent = nextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   264
        } else {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   265
            final MethodHandle nullGetterHandle = isFixedKey ? NULL_GETTER_1 : NULL_GETTER_2;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   266
            finalNextComponent = createGuardedInvocationComponentAsType(nullGetterHandle, callSiteType, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   267
        }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   268
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   269
        final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   270
                finalNextComponent.getGuardedInvocation().getInvocation());
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   271
        return finalNextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   272
                gic.getValidatorClass(), gic.getValidationType());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   273
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   274
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   275
    private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   276
            final MethodHandle invocation, final LinkerServices linkerServices) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   277
        return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation));
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   278
    }
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   279
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   280
    private static GuardedInvocationComponent createGuardedInvocationComponentAsType(
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   281
            final MethodHandle invocation, final MethodType fromType, final LinkerServices linkerServices) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   282
        return new GuardedInvocationComponent(linkerServices.asType(invocation, fromType));
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   283
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   284
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   285
    private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   286
            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
   287
            final ValidationType validationType, final LinkerServices linkerServices) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   288
        return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation), guard,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   289
                validatorClass, validationType);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   290
    }
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   291
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   292
    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
   293
        if (fixedKey instanceof Integer) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   294
            return (Integer)fixedKey;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   295
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   296
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   297
        final Number n;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   298
        if (fixedKey instanceof Number) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   299
            n = (Number)fixedKey;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   300
        } else {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   301
            final Class<?> keyClass = fixedKey.getClass();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   302
            if(linkerServices.canConvert(keyClass, Number.class)) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   303
                final Object val;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   304
                try {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   305
                    val = linkerServices.getTypeConverter(keyClass, Number.class).invoke(fixedKey);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   306
                } catch(Exception|Error e) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   307
                    throw e;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   308
                } catch(final Throwable t) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   309
                    throw new RuntimeException(t);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   310
                }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   311
                if(!(val instanceof Number)) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   312
                    return null; // not a number
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   313
                }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   314
                n = (Number)val;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   315
            } else if (fixedKey instanceof String){
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   316
                try {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   317
                    return Integer.valueOf((String)fixedKey);
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   318
                } catch(final NumberFormatException e) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   319
                    // key is not a number
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   320
                    return null;
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   321
                }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   322
            } else {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   323
                return null;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   324
            }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   325
        }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   326
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   327
        if(n instanceof Integer) {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   328
            return (Integer)n;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
        }
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   330
        final int intIndex = n.intValue();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   331
        final double doubleValue = n.doubleValue();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   332
        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
   333
            return null; // not an exact integer
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   334
        }
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   335
        return intIndex;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   336
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   337
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   338
    private static MethodHandle convertArgToNumber(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   339
        final Class<?> sourceType = desc.getMethodType().parameterType(1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   340
        if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   341
            return mh;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   342
        } else if(ls.canConvert(sourceType, Number.class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   343
            final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   344
            return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   345
                    mh.type().parameterType(1))));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   346
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   347
        return mh;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   348
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   349
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   350
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   351
     * 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
   352
     * fixed key first.
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
    private static class Binder {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
        private final LinkerServices linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   356
        private final MethodType methodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
        private final Object fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   359
        Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   360
            this.linkerServices = linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   361
            this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   362
            this.fixedKey = fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   363
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   364
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   365
        /*private*/ MethodHandle bind(final MethodHandle handle) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   366
            return bindToFixedKey(linkerServices.asTypeLosslessReturn(handle, methodType));
16234
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 bindTest(final MethodHandle handle) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   370
            return bindToFixedKey(Guards.asType(handle, methodType));
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 bindToFixedKey(final MethodHandle handle) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   374
            return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
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
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   377
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   378
    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
   379
    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
   380
    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
   381
            MethodType.methodType(boolean.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   382
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   383
    private static MethodHandle findRangeCheck(final Class<?> collectionType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   384
        return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   385
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   386
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   387
    @SuppressWarnings("unused")
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   388
    private static boolean rangeCheck(final Object array, final Object index) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   389
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   390
            return false;
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
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   393
        final int intIndex = n.intValue();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   394
        if (intIndex != n.doubleValue()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   395
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   396
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   397
        return 0 <= intIndex && intIndex < Array.getLength(array);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   399
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   400
    @SuppressWarnings("unused")
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   401
    private static boolean rangeCheck(final List<?> list, final Object index) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   402
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   403
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   404
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
        final int intIndex = n.intValue();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   407
        if (intIndex != n.doubleValue()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   408
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   409
        }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   410
        return 0 <= intIndex && intIndex < list.size();
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   411
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   412
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   413
    @SuppressWarnings("unused")
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   414
    private static void noOpSetter() {
16234
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
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   417
    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
   418
            MethodType.methodType(Object.class, int.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   419
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   420
    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
   421
            MethodType.methodType(Object.class, Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   422
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   423
    private static final MethodHandle NO_OP_SETTER_2;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   424
    private static final MethodHandle NO_OP_SETTER_3;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   425
    static {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   426
        final MethodHandle noOpSetter = Lookup.findOwnStatic(MethodHandles.lookup(), "noOpSetter", void.class);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   427
        NO_OP_SETTER_2 = dropObjectArguments(noOpSetter, 2);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   428
        NO_OP_SETTER_3 = dropObjectArguments(noOpSetter, 3);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   429
    }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   430
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   431
    private GuardedInvocationComponent getElementSetter(final ComponentLinkRequest req) throws Exception {
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   432
        final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   433
        final Object name = req.name;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   434
        final boolean isFixedKey = name != null;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   435
        assertParameterCount(callSiteDescriptor, isFixedKey ? 2 : 3);
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   436
        final LinkerServices linkerServices = req.linkerServices;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   437
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   438
        final Class<?> declaredType = callSiteType.parameterType(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   439
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   440
        final GuardedInvocationComponent gic;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   441
        // 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
   442
        // 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
   443
        // dealing with an array, or a list or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   444
        // 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
   445
        // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   446
        final CollectionType collectionType;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   447
        if(declaredType.isArray()) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   448
            gic = createInternalFilteredGuardedInvocationComponent(MethodHandles.arrayElementSetter(declaredType), linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   449
            collectionType = CollectionType.ARRAY;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   450
        } else if(List.class.isAssignableFrom(declaredType)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   451
            gic = createInternalFilteredGuardedInvocationComponent(SET_LIST_ELEMENT, linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   452
            collectionType = CollectionType.LIST;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   453
        } else if(Map.class.isAssignableFrom(declaredType)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   454
            gic = createInternalFilteredGuardedInvocationComponent(PUT_MAP_ELEMENT, linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   455
            collectionType = CollectionType.MAP;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   456
        } else if(clazz.isArray()) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   457
            gic = getClassGuardedInvocationComponent(linkerServices.filterInternalObjects(
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   458
                    MethodHandles.arrayElementSetter(clazz)), callSiteType);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   459
            collectionType = CollectionType.ARRAY;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   460
        } else if(List.class.isAssignableFrom(clazz)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   461
            gic = createInternalFilteredGuardedInvocationComponent(SET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, ValidationType.INSTANCE_OF,
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   462
                    linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   463
            collectionType = CollectionType.LIST;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   464
        } else if(Map.class.isAssignableFrom(clazz)) {
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   465
            gic = createInternalFilteredGuardedInvocationComponent(PUT_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType),
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   466
                    Map.class, ValidationType.INSTANCE_OF, linkerServices);
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   467
            collectionType = CollectionType.MAP;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   468
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   469
            // Can't set elements for objects that are neither arrays, nor list, nor maps.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   470
            gic = null;
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   471
            collectionType = null;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   472
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   473
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   474
        // 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
   475
        // 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
   476
        // operation.
34978
e753c4c9f96f 8144917: Prepare AbstractJavaLinker/BeanLinker codebase for missing member implementation
attila
parents: 34447
diff changeset
   477
        final GuardedInvocationComponent nextComponent = collectionType == CollectionType.MAP ? null : getNextComponent(req);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   478
        if(gic == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   479
            return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   480
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   481
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   482
        // Convert the key to a number if we're working with a list or array
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   483
        final Object typedName;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   484
        if (collectionType != CollectionType.MAP && isFixedKey) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   485
            final Integer integer = convertKeyToInteger(name, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   486
            if (integer == null || integer.intValue() < 0) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   487
                // 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
   488
                // array or list element
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   489
                return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   490
            }
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   491
            typedName = integer;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   492
        } else {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   493
            typedName = name;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   494
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   495
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   496
        final GuardedInvocation gi = gic.getGuardedInvocation();
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   497
        final Binder binder = new Binder(linkerServices, callSiteType, typedName);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   498
        final MethodHandle invocation = gi.getInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   499
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   500
        if (collectionType == CollectionType.MAP) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   501
            assert nextComponent == null;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   502
            return gic.replaceInvocation(binder.bind(invocation));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   503
        }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   504
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   505
        assert collectionType == CollectionType.LIST || collectionType == CollectionType.ARRAY;
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   506
        final MethodHandle checkGuard = convertArgToNumber(collectionType == CollectionType.LIST ? RANGE_CHECK_LIST :
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   507
            RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   508
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   509
        // If there's no next component, produce a no-op one.
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   510
        final GuardedInvocationComponent finalNextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   511
        if (nextComponent != null) {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   512
            finalNextComponent = nextComponent;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   513
        } else {
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   514
            final MethodHandle noOpSetterHandle = isFixedKey ? NO_OP_SETTER_2 : NO_OP_SETTER_3;
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   515
            finalNextComponent = createGuardedInvocationComponentAsType(noOpSetterHandle, callSiteType, linkerServices);
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   516
        }
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   517
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   518
        final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
34979
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   519
                finalNextComponent.getGuardedInvocation().getInvocation());
03b189baa361 8144919: Implement missing member handler for BeansLinker
attila
parents: 34978
diff changeset
   520
        return finalNextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   521
                gic.getValidatorClass(), gic.getValidationType());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   522
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   523
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   524
    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
   525
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   526
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 28881
diff changeset
   527
    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
   528
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   529
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   530
    private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   531
        if(descriptor.getMethodType().parameterCount() != paramCount) {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33337
diff changeset
   532
            throw new BootstrapMethodError(descriptor.getOperation() + " must have exactly " + paramCount + " parameters.");
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   533
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   534
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   535
}