nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/beans/AbstractJavaLinker.java
author attila
Tue, 20 Oct 2015 23:33:18 +0200
changeset 33337 af3fea63e008
parent 33332 f180be6368d8
child 33338 faf6471e1cc8
permissions -rw-r--r--
8139761: Improve Dynalink class nomenclature and package organization 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
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    84
package jdk.internal.dynalink.beans;
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;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
    89
import java.lang.reflect.AccessibleObject;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
    90
import java.lang.reflect.Constructor;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
import java.lang.reflect.Field;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
    92
import java.lang.reflect.Member;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
import java.lang.reflect.Method;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
import java.lang.reflect.Modifier;
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
    95
import java.util.Collection;
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
    96
import java.util.Collections;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    97
import java.util.HashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    98
import java.util.List;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
import java.util.Map;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   100
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   102
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
import jdk.internal.dynalink.linker.GuardingDynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   104
import jdk.internal.dynalink.linker.LinkRequest;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   105
import jdk.internal.dynalink.linker.LinkerServices;
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33332
diff changeset
   106
import jdk.internal.dynalink.linker.support.Guards;
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33332
diff changeset
   107
import jdk.internal.dynalink.linker.support.Lookup;
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33332
diff changeset
   108
import jdk.internal.dynalink.linker.support.TypeUtilities;
33330
35531ae624ef 8139304: Remove elaborate call site descriptor class hierarchy and factory for them. Remove AutoDiscovery, DefaultPrelinkFilter, and BottomGuardingDynamicLinker as they can be inlined into DynamicLinkerFactory. Remove CallerSensitiveDetector as it can be inlined into AbstractJavaLinker. Make ClassMap non-public.
attila
parents: 33007
diff changeset
   109
import sun.reflect.CallerSensitive;
16234
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
/**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
 * A base class for both {@link StaticClassLinker} and {@link BeanLinker}. Deals with common aspects of property
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
 * exposure and method calls for both static and instance facets of a class.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
abstract class AbstractJavaLinker implements GuardingDynamicLinker {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   116
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
    final Class<?> clazz;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
    private final MethodHandle classGuard;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
    private final MethodHandle assignableGuard;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   120
    private final Map<String, AnnotatedDynamicMethod> propertyGetters = new HashMap<>();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   121
    private final Map<String, DynamicMethod> propertySetters = new HashMap<>();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
    private final Map<String, DynamicMethod> methods = new HashMap<>();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   124
    AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   125
        this(clazz, classGuard, classGuard);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   126
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   127
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   128
    AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard, final MethodHandle assignableGuard) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
        this.clazz = clazz;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   130
        this.classGuard = classGuard;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
        this.assignableGuard = assignableGuard;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
        final FacetIntrospector introspector = createFacetIntrospector();
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   134
        // Add methods and properties
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   135
        for(final Method method: introspector.getMethods()) {
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   136
            final String name = method.getName();
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   137
            // Add method
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   138
            addMember(name, method, methods);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   139
            // Add the method as a property getter and/or setter
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   140
            if(name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   141
                // Property getter
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   142
                setPropertyGetter(method, 3);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   143
            } else if(name.startsWith("is") && name.length() > 2 && method.getParameterTypes().length == 0 &&
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   144
                    method.getReturnType() == boolean.class) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   145
                // Boolean property getter
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   146
                setPropertyGetter(method, 2);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   147
            } else if(name.startsWith("set") && name.length() > 3 && method.getParameterTypes().length == 1) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   148
                // Property setter
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   149
                addMember(decapitalize(name.substring(3)), method, propertySetters);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   150
            }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   151
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   152
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   153
        // Add field getter/setters as property getters/setters.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   154
        for(final Field field: introspector.getFields()) {
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   155
            final String name = field.getName();
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   156
            // Only add a property getter when one is not defined already as a getXxx()/isXxx() method.
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   157
            if(!propertyGetters.containsKey(name)) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   158
                setPropertyGetter(name, introspector.unreflectGetter(field), ValidationType.EXACT_CLASS);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   159
            }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   160
            if(!(Modifier.isFinal(field.getModifiers()) || propertySetters.containsKey(name))) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   161
                addMember(name, new SimpleDynamicMethod(introspector.unreflectSetter(field), clazz, name),
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   162
                        propertySetters);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   163
            }
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   164
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   165
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   166
        // Add inner classes, but only those for which we don't hide a property with it
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   167
        for(final Map.Entry<String, MethodHandle> innerClassSpec: introspector.getInnerClassGetters().entrySet()) {
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   168
            final String name = innerClassSpec.getKey();
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   169
            if(!propertyGetters.containsKey(name)) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   170
                setPropertyGetter(name, innerClassSpec.getValue(), ValidationType.EXACT_CLASS);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   171
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   172
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   174
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   175
    private static String decapitalize(final String str) {
16267
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   176
        assert str != null;
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   177
        if(str.isEmpty()) {
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   178
            return str;
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   179
        }
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   180
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   181
        final char c0 = str.charAt(0);
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   182
        if(Character.isLowerCase(c0)) {
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   183
            return str;
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   184
        }
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   185
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   186
        // If it has two consecutive upper-case characters, i.e. "URL", don't decapitalize
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   187
        if(str.length() > 1 && Character.isUpperCase(str.charAt(1))) {
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   188
            return str;
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   189
        }
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   190
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   191
        final char c[] = str.toCharArray();
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   192
        c[0] = Character.toLowerCase(c0);
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   193
        return new String(c);
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   194
    }
cd471d62bb6a 8009143: Eliminate Dynalink dependency on java.beans
attila
parents: 16245
diff changeset
   195
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
    abstract FacetIntrospector createFacetIntrospector();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   197
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   198
    Collection<String> getReadablePropertyNames() {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   199
        return getUnmodifiableKeys(propertyGetters);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   200
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   201
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   202
    Collection<String> getWritablePropertyNames() {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   203
        return getUnmodifiableKeys(propertySetters);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   204
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   205
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   206
    Collection<String> getMethodNames() {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   207
        return getUnmodifiableKeys(methods);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   208
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   209
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   210
    private static Collection<String> getUnmodifiableKeys(final Map<String, ?> m) {
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   211
        return Collections.unmodifiableCollection(m.keySet());
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   212
    }
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   213
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   214
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   215
     * Sets the specified dynamic method to be the property getter for the specified property. Note that you can only
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   216
     * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   217
     * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   218
     * instead.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   219
     * @param name name of the property
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   220
     * @param handle the method handle that implements the property getter
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   221
     * @param validationType the validation type for the property
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   222
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   223
    private void setPropertyGetter(final String name, final SingleDynamicMethod handle, final ValidationType validationType) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   224
        propertyGetters.put(name, new AnnotatedDynamicMethod(handle, validationType));
16234
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
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   227
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   228
     * Sets the specified reflective method to be the property getter for the specified property.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   229
     * @param getter the getter method
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   230
     * @param prefixLen the getter prefix in the method name; should be 3 for getter names starting with "get" and 2 for
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   231
     * names starting with "is".
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   232
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   233
    private void setPropertyGetter(final Method getter, final int prefixLen) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   234
        setPropertyGetter(decapitalize(getter.getName().substring(prefixLen)), createDynamicMethod(
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   235
                getMostGenericGetter(getter)), ValidationType.INSTANCE_OF);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   236
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   237
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   238
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   239
     * Sets the specified method handle to be the property getter for the specified property. Note that you can only
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   240
     * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   241
     * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   242
     * instead.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   243
     * @param name name of the property
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   244
     * @param handle the method handle that implements the property getter
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   245
     * @param validationType the validation type for the property
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   246
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   247
    void setPropertyGetter(final String name, final MethodHandle handle, final ValidationType validationType) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   248
        setPropertyGetter(name, new SimpleDynamicMethod(handle, clazz, name), validationType);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   249
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   250
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   251
    private void addMember(final String name, final AccessibleObject ao, final Map<String, DynamicMethod> methodMap) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   252
        addMember(name, createDynamicMethod(ao), methodMap);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   253
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   254
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   255
    private void addMember(final String name, final SingleDynamicMethod method, final Map<String, DynamicMethod> methodMap) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   256
        final DynamicMethod existingMethod = methodMap.get(name);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   257
        final DynamicMethod newMethod = mergeMethods(method, existingMethod, clazz, name);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
        if(newMethod != existingMethod) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   259
            methodMap.put(name, newMethod);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   260
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   261
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   262
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   263
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   264
     * Given one or more reflective methods or constructors, creates a dynamic method that represents them all. The
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   265
     * methods should represent all overloads of the same name (or all constructors of the class).
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   266
     * @param members the reflective members
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   267
     * @param clazz the class declaring the reflective members
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   268
     * @param name the common name of the reflective members.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   269
     * @return a dynamic method representing all the specified reflective members.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   270
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   271
    static DynamicMethod createDynamicMethod(final Iterable<? extends AccessibleObject> members, final Class<?> clazz, final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   272
        DynamicMethod dynMethod = null;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   273
        for(final AccessibleObject method: members) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   274
            dynMethod = mergeMethods(createDynamicMethod(method), dynMethod, clazz, name);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   275
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   276
        return dynMethod;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   277
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   278
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   279
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   280
     * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   281
     * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   282
     * dynamic method when needed.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   283
     * @param m the reflective member
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   284
     * @return the single dynamic method representing the reflective member
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   285
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   286
    private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
33330
35531ae624ef 8139304: Remove elaborate call site descriptor class hierarchy and factory for them. Remove AutoDiscovery, DefaultPrelinkFilter, and BottomGuardingDynamicLinker as they can be inlined into DynamicLinkerFactory. Remove CallerSensitiveDetector as it can be inlined into AbstractJavaLinker. Make ClassMap non-public.
attila
parents: 33007
diff changeset
   287
        if (m.isAnnotationPresent(CallerSensitive.class)) {
27359
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   288
            // Method has @CallerSensitive annotation
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   289
            return new CallerSensitiveDynamicMethod(m);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   290
        }
27359
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   291
        // Method has no @CallerSensitive annotation
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   292
        final MethodHandle mh;
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   293
        try {
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   294
            mh = unreflectSafely(m);
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   295
        } catch (final IllegalAccessError e) {
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   296
            // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   297
            // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   298
            return new CallerSensitiveDynamicMethod(m);
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   299
        }
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   300
        // Proceed with non-caller sensitive
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   301
        final Member member = (Member)m;
27359
59d61197a3f3 8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
attila
parents: 25865
diff changeset
   302
        return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   303
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   304
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   305
    /**
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   306
     * Unreflects a method handle from a Method or a Constructor using safe (zero-privilege) unreflection. Should be
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   307
     * only used for methods and constructors that are not caller sensitive. If a caller sensitive method were
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   308
     * unreflected through this mechanism, it would not be a security issue, but would be bound to the zero-privilege
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   309
     * unreflector as its caller, and thus completely useless.
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   310
     * @param m the method or constructor
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   311
     * @return the method handle
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   312
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   313
    private static MethodHandle unreflectSafely(final AccessibleObject m) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   314
        if(m instanceof Method) {
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   315
            final Method reflMethod = (Method)m;
19093
49bb7e75b326 8021189: Prevent access to constructors of restricted classes
attila
parents: 19092
diff changeset
   316
            final MethodHandle handle = Lookup.PUBLIC.unreflect(reflMethod);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   317
            if(Modifier.isStatic(reflMethod.getModifiers())) {
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   318
                return StaticClassIntrospector.editStaticMethodHandle(handle);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   319
            }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   320
            return handle;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   321
        }
19093
49bb7e75b326 8021189: Prevent access to constructors of restricted classes
attila
parents: 19092
diff changeset
   322
        return StaticClassIntrospector.editConstructorMethodHandle(Lookup.PUBLIC.unreflectConstructor((Constructor<?>)m));
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   323
    }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   324
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   325
    private static DynamicMethod mergeMethods(final SingleDynamicMethod method, final DynamicMethod existing, final Class<?> clazz, final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   326
        if(existing == null) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   327
            return method;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   328
        } else if(existing.contains(method)) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
            return existing;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   330
        } else if(existing instanceof SingleDynamicMethod) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   331
            final OverloadedDynamicMethod odm = new OverloadedDynamicMethod(clazz, name);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   332
            odm.addMethod(((SingleDynamicMethod)existing));
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   333
            odm.addMethod(method);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   334
            return odm;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   335
        } else if(existing instanceof OverloadedDynamicMethod) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   336
            ((OverloadedDynamicMethod)existing).addMethod(method);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   337
            return existing;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   338
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   339
        throw new AssertionError();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   340
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   341
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   342
    @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   343
    public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   344
            throws Exception {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   345
        // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
33332
f180be6368d8 8139588: Remove concept of runtime context arguments, call site tokens, and link counts
attila
parents: 33331
diff changeset
   346
        final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   347
        final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   348
        // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   349
        if("callMethod" == op) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   350
            return getCallPropWithThis(callSiteDescriptor, linkerServices);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   351
        }
33330
35531ae624ef 8139304: Remove elaborate call site descriptor class hierarchy and factory for them. Remove AutoDiscovery, DefaultPrelinkFilter, and BottomGuardingDynamicLinker as they can be inlined into DynamicLinkerFactory. Remove CallerSensitiveDetector as it can be inlined into AbstractJavaLinker. Make ClassMap non-public.
attila
parents: 33007
diff changeset
   352
        List<String> operations = callSiteDescriptor.tokenizeOperators();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   353
        while(!operations.isEmpty()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   354
            final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
                    operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   356
            if(gic != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
                return gic.getGuardedInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   359
            operations = pop(operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   360
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   361
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   362
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   363
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   364
    protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   365
            final LinkerServices linkerServices, final List<String> operations) throws Exception {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   366
        if(operations.isEmpty()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   367
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   368
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   369
        final String op = operations.get(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   370
        // Either dyn:getProp:name(this) or dyn:getProp(this, name)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   371
        if("getProp".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   372
            return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   373
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   374
        // Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   375
        if("setProp".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   376
            return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   377
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   378
        // Either dyn:getMethod:name(this), or dyn:getMethod(this, name)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   379
        if("getMethod".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   380
            return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   381
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   382
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   383
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   384
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   385
    static final <T> List<T> pop(final List<T> l) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   386
        return l.subList(1, l.size());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   387
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   388
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   389
    MethodHandle getClassGuard(final CallSiteDescriptor desc) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   390
        return getClassGuard(desc.getMethodType());
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   393
    MethodHandle getClassGuard(final MethodType type) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   394
        return Guards.asType(classGuard, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   395
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   396
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   397
    GuardedInvocationComponent getClassGuardedInvocationComponent(final MethodHandle invocation, final MethodType type) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
        return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
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
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   401
    SingleDynamicMethod getConstructorMethod(final String signature) {
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   402
        return null;
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   403
    }
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   404
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   405
    private MethodHandle getAssignableGuard(final MethodType type) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
        return Guards.asType(assignableGuard, type);
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   409
    private GuardedInvocation getCallPropWithThis(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   410
        switch(callSiteDescriptor.getNameTokenCount()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   411
            case 3: {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   412
                return createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   413
                        callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), methods);
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
            default: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   416
                return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   417
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   418
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   419
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   420
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   421
    private GuardedInvocation createGuardedDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   422
            final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap){
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   423
        final MethodHandle inv = getDynamicMethodInvocation(callSiteDescriptor, linkerServices, methodName, methodMap);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   424
        return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType()));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   425
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   426
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   427
    private MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   428
            final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   429
        final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   430
        return dynaMethod != null ? dynaMethod.getInvocation(callSiteDescriptor, linkerServices) : null;
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
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   433
    private DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   434
        final DynamicMethod dynaMethod = methodMap.get(methodName);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   435
        return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   436
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   437
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   438
    private SingleDynamicMethod getExplicitSignatureDynamicMethod(final String fullName,
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   439
            final Map<String, DynamicMethod> methodsMap) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   440
        // What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   441
        // to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   442
        // resolution works correctly in almost every situation. However, in presence of many language-specific
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   443
        // conversions with a radically dynamic language, most overloaded methods will end up being constantly selected
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   444
        // at invocation time, so a programmer knowledgeable of the situation might choose to pin down an exact overload
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   445
        // for performance reasons.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   446
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   447
        // Is the method name lexically of the form "name(types)"?
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   448
        final int lastChar = fullName.length() - 1;
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   449
        if(fullName.charAt(lastChar) != ')') {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   450
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   451
        }
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   452
        final int openBrace = fullName.indexOf('(');
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   453
        if(openBrace == -1) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   454
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   455
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   456
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   457
        final String name = fullName.substring(0, openBrace);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   458
        final String signature = fullName.substring(openBrace + 1, lastChar);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   459
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   460
        // Find an existing method for the "name" part
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   461
        final DynamicMethod simpleNamedMethod = methodsMap.get(name);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   462
        if(simpleNamedMethod == null) {
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   463
            // explicit signature constructor access
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   464
            // Java.type("java.awt.Color")["(int,int,int)"]
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   465
            // will get Color(int,int,int) constructor of Color class.
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   466
            if (name.isEmpty()) {
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   467
                return getConstructorMethod(signature);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   468
            }
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   469
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   470
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   471
        }
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
        // Try to get a narrowed dynamic method for the explicit parameter types.
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
   474
        return simpleNamedMethod.getMethodForExactParamTypes(signature);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   475
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   476
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   477
    private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   478
            boolean.class, MethodHandle.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   479
    private static final MethodHandle CONSTANT_NULL_DROP_METHOD_HANDLE = MethodHandles.dropArguments(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   480
            MethodHandles.constant(Object.class, null), 0, MethodHandle.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   481
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   482
    private GuardedInvocationComponent getPropertySetter(final CallSiteDescriptor callSiteDescriptor,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   483
            final LinkerServices linkerServices, final List<String> operations) throws Exception {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   484
        switch(callSiteDescriptor.getNameTokenCount()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   485
            case 2: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   486
                // Must have three arguments: target object, property name, and property value.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   487
                assertParameterCount(callSiteDescriptor, 3);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   488
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   489
                // We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   490
                // valid for us to convert return values proactively. Also, since we don't know what setters will be
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27359
diff changeset
   491
                // invoked, we'll conservatively presume Object return type. The one exception is void return.
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27359
diff changeset
   492
                final MethodType origType = callSiteDescriptor.getMethodType();
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27359
diff changeset
   493
                final MethodType type = origType.returnType() == void.class ? origType : origType.changeReturnType(Object.class);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   494
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   495
                // What's below is basically:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   496
                //   foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation),
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   497
                //     get_setter_handle(type, linkerServices))
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   498
                // only with a bunch of method signature adjustments. Basically, retrieve method setter
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   499
                // MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   500
                // component's invocation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   501
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   502
                // Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   503
                // abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   504
                // Object return type).
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   505
                final MethodType setterType = type.dropParameterTypes(1, 2);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   506
                // Bind property setter handle to the expected setter type and linker services. Type is
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   507
                // MethodHandle(Object, String, Object)
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   508
                final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0,
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27359
diff changeset
   509
                        callSiteDescriptor.changeMethodType(setterType), linkerServices);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   510
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   511
                // Cast getter to MethodHandle(O, N, V)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   512
                final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   513
                        MethodHandle.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   514
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   515
                // Handle to invoke the setter R(MethodHandle, O, V)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   516
                final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   517
                // Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   518
                final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   519
                        1));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   520
                final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   521
                        linkerServices, operations);
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
                final MethodHandle fallbackFolded;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   524
                if(nextComponent == null) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   525
                    // Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   526
                    fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   527
                            type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   528
                } else {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   529
                    // Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   530
                    // extra argument resulting from fold
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   531
                    fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   532
                            0, MethodHandle.class);
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   535
                // fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V))
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   536
                final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   537
                            IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   538
                if(nextComponent == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   539
                    return getClassGuardedInvocationComponent(compositeSetter, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   540
                }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   541
                return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   542
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   543
            case 3: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   544
                // Must have two arguments: target object and property value
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   545
                assertParameterCount(callSiteDescriptor, 2);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   546
                final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   547
                        callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), propertySetters);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   548
                // If we have a property setter with this name, this composite operation will always stop here
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   549
                if(gi != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   550
                    return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   551
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   552
                // If we don't have a property setter with this name, always fall back to the next operation in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   553
                // composite (if any)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   554
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   555
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   556
            default: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   557
                // More than two name components; don't know what to do with it.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   558
                return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   559
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   560
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   561
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   562
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   563
    private static final Lookup privateLookup = new Lookup(MethodHandles.lookup());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   564
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   565
    private static final MethodHandle IS_ANNOTATED_METHOD_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   566
            boolean.class, AnnotatedDynamicMethod.class));
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   567
    private static final MethodHandle CONSTANT_NULL_DROP_ANNOTATED_METHOD = MethodHandles.dropArguments(
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   568
            MethodHandles.constant(Object.class, null), 0, AnnotatedDynamicMethod.class);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   569
    private static final MethodHandle GET_ANNOTATED_METHOD = privateLookup.findVirtual(AnnotatedDynamicMethod.class,
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   570
            "getTarget", MethodType.methodType(MethodHandle.class, CallSiteDescriptor.class, LinkerServices.class));
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   571
    private static final MethodHandle GETTER_INVOKER = MethodHandles.invoker(MethodType.methodType(Object.class, Object.class));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   572
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   573
    private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   574
            final LinkerServices linkerServices, final List<String> ops) throws Exception {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   575
        switch(callSiteDescriptor.getNameTokenCount()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   576
            case 2: {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   577
                // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   578
                // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   579
                // runtime might not allow coercing at that call site.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   580
                final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   581
                // Must have exactly two arguments: receiver and name
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   582
                assertParameterCount(callSiteDescriptor, 2);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   583
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   584
                // What's below is basically:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   585
                //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   586
                // only with a bunch of method signature adjustments. Basically, retrieve method getter
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   587
                // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   588
                // or delegate to next component's invocation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   589
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   590
                final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   591
                        AnnotatedDynamicMethod.class));
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   592
                final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   593
                        GET_ANNOTATED_METHOD, 1, callSiteDescriptor, linkerServices);
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   594
                final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   595
                        callSiteBoundMethodGetter);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   596
                // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   597
                final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   598
                        MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   599
                // Since it's in the target of a fold, drop the unnecessary second argument
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   600
                // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   601
                final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   602
                        type.parameterType(1));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   603
                final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   604
                        linkerServices, ops);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   605
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   606
                final MethodHandle fallbackFolded;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   607
                if(nextComponent == null) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   608
                    // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   609
                    fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   610
                            type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   611
                } else {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   612
                    // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   613
                    // drop the extra argument resulting from fold and to change its return type to Object.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   614
                    final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   615
                    final MethodType nextType = nextInvocation.type();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   616
                    fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   617
                            nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   618
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   619
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   620
                // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   621
                final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   622
                            IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   623
                if(nextComponent == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   624
                    return getClassGuardedInvocationComponent(compositeGetter, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   625
                }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   626
                return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   627
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   628
            case 3: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   629
                // Must have exactly one argument: receiver
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   630
                assertParameterCount(callSiteDescriptor, 1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   631
                // Fixed name
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   632
                final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   633
                        CallSiteDescriptor.NAME_OPERAND));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   634
                if(annGetter == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   635
                    // We have no such property, always delegate to the next component operation
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   636
                    return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   637
                }
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   638
                final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   639
                // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   640
                // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   641
                // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   642
                // we're linking against a field getter, don't make the assumption.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   643
                // NOTE: No delegation to the next component operation if we have a property with this name, even if its
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   644
                // value is null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   645
                final ValidationType validationType = annGetter.validationType;
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   646
                // TODO: we aren't using the type that declares the most generic getter here!
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   647
                return new GuardedInvocationComponent(getter, getGuard(validationType,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   648
                        callSiteDescriptor.getMethodType()), clazz, validationType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   649
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   650
            default: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   651
                // Can't do anything with more than 3 name components
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   652
                return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   653
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   654
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   655
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   656
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   657
    private MethodHandle getGuard(final ValidationType validationType, final MethodType methodType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   658
        switch(validationType) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   659
            case EXACT_CLASS: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   660
                return getClassGuard(methodType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   661
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   662
            case INSTANCE_OF: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   663
                return getAssignableGuard(methodType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   664
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   665
            case IS_ARRAY: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   666
                return Guards.isArray(0, methodType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   667
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   668
            case NONE: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   669
                return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   670
            }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   671
            default: {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   672
                throw new AssertionError();
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   673
            }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   674
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   675
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   676
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   677
    private static final MethodHandle IS_DYNAMIC_METHOD = Guards.isInstance(DynamicMethod.class,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   678
            MethodType.methodType(boolean.class, Object.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   679
    private static final MethodHandle OBJECT_IDENTITY = MethodHandles.identity(Object.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   680
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   681
    private GuardedInvocationComponent getMethodGetter(final CallSiteDescriptor callSiteDescriptor,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   682
            final LinkerServices linkerServices, final List<String> ops) throws Exception {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   683
        // The created method handle will always return a DynamicMethod (or null), but since we don't want that type to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   684
        // be visible outside of this linker, declare it to return Object.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   685
        final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   686
        switch(callSiteDescriptor.getNameTokenCount()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   687
            case 2: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   688
                // Must have exactly two arguments: receiver and name
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   689
                assertParameterCount(callSiteDescriptor, 2);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   690
                final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   691
                        linkerServices, ops);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   692
                if(nextComponent == null || !TypeUtilities.areAssignable(DynamicMethod.class,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   693
                        nextComponent.getGuardedInvocation().getInvocation().type().returnType())) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   694
                    // No next component operation, or it can never produce a dynamic method; just return a component
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   695
                    // for this operation.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   696
                    return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   697
                }
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   698
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   699
                // What's below is basically:
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   700
                // foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) only with a
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   701
                // bunch of method signature adjustments. Basically, execute method getter; if it returns a non-null
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   702
                // DynamicMethod, use identity to return it, otherwise delegate to nextComponent's invocation.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   703
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   704
                final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   705
                // Since it is part of the foldArgument() target, it will have extra args that we need to drop.
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   706
                final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments(
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   707
                        OBJECT_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, Object.class));
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   708
                final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   709
                // The assumption is that getGuardedInvocationComponent() already asType()'d it correctly modulo the
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   710
                // return type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   711
                assert nextComponentInvocation.type().changeReturnType(type.returnType()).equals(type);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   712
                // Since it is part of the foldArgument() target, we have to drop an extra arg it receives.
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   713
                final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0,
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   714
                        Object.class);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   715
                // Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get)
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   716
                final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   717
                        IS_DYNAMIC_METHOD, returnMethodHandle, nextCombinedInvocation), typedGetter);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   718
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   719
                return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   720
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   721
            case 3: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   722
                // Must have exactly one argument: receiver
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   723
                assertParameterCount(callSiteDescriptor, 1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   724
                final DynamicMethod method = getDynamicMethod(callSiteDescriptor.getNameToken(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   725
                        CallSiteDescriptor.NAME_OPERAND));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   726
                if(method == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   727
                    // We have no such method, always delegate to the next component
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   728
                    return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   729
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   730
                // No delegation to the next component of the composite operation; if we have a method with that name,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   731
                // we'll always return it at this point.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   732
                return getClassGuardedInvocationComponent(linkerServices.asType(MethodHandles.dropArguments(
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   733
                        MethodHandles.constant(Object.class, method), 0, type.parameterType(0)), type), type);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   734
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   735
            default: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   736
                // Can't do anything with more than 3 name components
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   737
                return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   738
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   739
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   740
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   741
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   742
    static class MethodPair {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   743
        final MethodHandle method1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   744
        final MethodHandle method2;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   745
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   746
        MethodPair(final MethodHandle method1, final MethodHandle method2) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   747
            this.method1 = method1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   748
            this.method2 = method2;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   749
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   750
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   751
        MethodHandle guardWithTest(final MethodHandle test) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   752
            return MethodHandles.guardWithTest(test, method1, method2);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   753
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   754
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   755
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   756
    static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   757
        final MethodType type1 = m1.type();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   758
        final MethodType type2 = m2.type();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   759
        final Class<?> commonRetType = TypeUtilities.getCommonLosslessConversionType(type1.returnType(),
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   760
                type2.returnType());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   761
        return new MethodPair(
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   762
                m1.asType(type1.changeReturnType(commonRetType)),
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   763
                m2.asType(type2.changeReturnType(commonRetType)));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   764
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   765
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   766
    private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   767
        if(descriptor.getMethodType().parameterCount() != paramCount) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   768
            throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   769
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   770
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   771
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   772
    private static MethodHandle GET_PROPERTY_GETTER_HANDLE = MethodHandles.dropArguments(privateLookup.findOwnSpecial(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   773
            "getPropertyGetterHandle", Object.class, Object.class), 1, Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   774
    private final MethodHandle getPropertyGetterHandle = GET_PROPERTY_GETTER_HANDLE.bindTo(this);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   775
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   776
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   777
     * @param id the property ID
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   778
     * @return the method handle for retrieving the property, or null if the property does not exist
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   779
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   780
    @SuppressWarnings("unused")
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   781
    private Object getPropertyGetterHandle(final Object id) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   782
        return propertyGetters.get(id);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   783
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   784
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   785
    // Type is MethodHandle(BeanLinker, MethodType, LinkerServices, Object, String, Object), of which the two "Object"
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   786
    // args are dropped; this makes handles with first three args conform to "Object, String, Object" though, which is
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   787
    // a typical property setter with variable name signature (target, name, value).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   788
    private static final MethodHandle GET_PROPERTY_SETTER_HANDLE = MethodHandles.dropArguments(MethodHandles.dropArguments(
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   789
            privateLookup.findOwnSpecial("getPropertySetterHandle", MethodHandle.class, CallSiteDescriptor.class,
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   790
                    LinkerServices.class, Object.class), 3, Object.class), 5, Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   791
    // Type is MethodHandle(MethodType, LinkerServices, Object, String, Object)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   792
    private final MethodHandle getPropertySetterHandle = GET_PROPERTY_SETTER_HANDLE.bindTo(this);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   793
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   794
    @SuppressWarnings("unused")
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   795
    private MethodHandle getPropertySetterHandle(final CallSiteDescriptor setterDescriptor, final LinkerServices linkerServices,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   796
            final Object id) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   797
        return getDynamicMethodInvocation(setterDescriptor, linkerServices, String.valueOf(id), propertySetters);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   798
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   799
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   800
    private static MethodHandle GET_DYNAMIC_METHOD = MethodHandles.dropArguments(privateLookup.findOwnSpecial(
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   801
            "getDynamicMethod", Object.class, Object.class), 1, Object.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   802
    private final MethodHandle getDynamicMethod = GET_DYNAMIC_METHOD.bindTo(this);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   803
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   804
    @SuppressWarnings("unused")
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   805
    // This method is marked to return Object instead of DynamicMethod as it's used as a linking component and we don't
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   806
    // want to make the DynamicMethod type observable externally (e.g. as the return type of a MethodHandle returned for
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19630
diff changeset
   807
    // "dyn:getMethod" linking).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   808
    private Object getDynamicMethod(final Object name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   809
        return getDynamicMethod(String.valueOf(name), methods);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   810
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   811
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   812
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   813
     * Returns a dynamic method of the specified name.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   814
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   815
     * @param name name of the method
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   816
     * @return the dynamic method (either {@link SimpleDynamicMethod} or {@link OverloadedDynamicMethod}, or null if the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   817
     * method with the specified name does not exist.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   818
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   819
    DynamicMethod getDynamicMethod(final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   820
        return getDynamicMethod(name, methods);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   821
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   822
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   823
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   824
     * Find the most generic superclass that declares this getter. Since getters have zero args (aside from the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   825
     * receiver), they can't be overloaded, so we're free to link with an instanceof guard for the most generic one,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   826
     * creating more stable call sites.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   827
     * @param getter the getter
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   828
     * @return getter with same name, declared on the most generic superclass/interface of the declaring class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   829
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   830
    private static Method getMostGenericGetter(final Method getter) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   831
        return getMostGenericGetter(getter.getName(), getter.getReturnType(), getter.getDeclaringClass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   832
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   833
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   834
    private static Method getMostGenericGetter(final String name, final Class<?> returnType, final Class<?> declaringClass) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   835
        if(declaringClass == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   836
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   837
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   838
        // Prefer interfaces
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   839
        for(final Class<?> itf: declaringClass.getInterfaces()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   840
            final Method itfGetter = getMostGenericGetter(name, returnType, itf);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   841
            if(itfGetter != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   842
                return itfGetter;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   843
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   844
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   845
        final Method superGetter = getMostGenericGetter(name, returnType, declaringClass.getSuperclass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   846
        if(superGetter != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   847
            return superGetter;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   848
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   849
        if(!CheckRestrictedPackage.isRestrictedClass(declaringClass)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   850
            try {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   851
                return declaringClass.getMethod(name);
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   852
            } catch(final NoSuchMethodException e) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   853
                // Intentionally ignored, meant to fall through
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   854
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   855
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   856
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   857
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   858
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   859
    private static final class AnnotatedDynamicMethod {
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   860
        private final SingleDynamicMethod method;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   861
        /*private*/ final ValidationType validationType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   862
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   863
        AnnotatedDynamicMethod(final SingleDynamicMethod method, final ValidationType validationType) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   864
            this.method = method;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   865
            this.validationType = validationType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   866
        }
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   867
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   868
        MethodHandle getInvocation(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   869
            return method.getInvocation(callSiteDescriptor, linkerServices);
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   870
        }
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   871
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   872
        @SuppressWarnings("unused")
33331
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   873
        MethodHandle getTarget(final CallSiteDescriptor desc, final LinkerServices linkerServices) {
273e6a10de22 8139435: Make sure CallSiteDescriptor.getLookup is subject to a security check
attila
parents: 33330
diff changeset
   874
            final MethodHandle inv = linkerServices.filterInternalObjects(method.getTarget(desc));
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   875
            assert inv != null;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   876
            return inv;
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16267
diff changeset
   877
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   878
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   879
}