nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/DynamicLinkerFactory.java
author attila
Tue, 20 Oct 2015 23:34:46 +0200
changeset 33340 6c945c826f36
parent 33339 334cd3ebfa5e
child 33341 cc9fa3638714
permissions -rw-r--r--
8139895: Introduce GuardingDynamicLinkerExporter 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;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
    86
import java.lang.invoke.MethodHandle;
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
    87
import java.lang.invoke.MethodType;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.lang.invoke.MutableCallSite;
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
    89
import java.security.AccessController;
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
    90
import java.security.PrivilegedAction;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
import java.util.ArrayList;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
import java.util.Arrays;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
import java.util.Collections;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
import java.util.HashSet;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
    95
import java.util.Iterator;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    96
import java.util.LinkedList;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    97
import java.util.List;
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
    98
import java.util.Objects;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
    99
import java.util.ServiceConfigurationError;
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
   100
import java.util.ServiceLoader;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
import java.util.Set;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   102
import java.util.function.Supplier;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
import jdk.internal.dynalink.beans.BeansLinker;
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
   104
import jdk.internal.dynalink.linker.GuardedInvocation;
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   105
import jdk.internal.dynalink.linker.GuardedInvocationTransformer;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
import jdk.internal.dynalink.linker.GuardingDynamicLinker;
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   107
import jdk.internal.dynalink.linker.GuardingDynamicLinkerExporter;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
import jdk.internal.dynalink.linker.LinkRequest;
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   110
import jdk.internal.dynalink.linker.LinkerServices;
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   111
import jdk.internal.dynalink.linker.MethodHandleTransformer;
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   112
import jdk.internal.dynalink.linker.MethodTypeConversionStrategy;
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   113
import jdk.internal.dynalink.linker.support.CompositeGuardingDynamicLinker;
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   114
import jdk.internal.dynalink.linker.support.CompositeTypeBasedGuardingDynamicLinker;
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   115
import jdk.internal.dynalink.linker.support.DefaultInternalObjectFilter;
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   116
import jdk.internal.dynalink.linker.support.TypeUtilities;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
/**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   119
 * A factory class for creating {@link DynamicLinker} objects. Dynamic linkers
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   120
 * are the central objects in Dynalink; these are composed of several
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   121
 * {@link GuardingDynamicLinker} objects and coordinate linking of call sites
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   122
 * with them. The usual dynamic linker is a linker
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   123
 * composed of all {@link GuardingDynamicLinker} objects explicitly pre-created
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   124
 * by the user of the factory and configured with
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   125
 * {@link #setPrioritizedLinkers(List)}, as well as any
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   126
 * {@link #setClassLoader(ClassLoader) automatically discovered} ones, and
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   127
 * finally the ones configured with {@link #setFallbackLinkers(List)}; this last
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   128
 * category usually includes {@link BeansLinker}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
 */
33003
f1e00197031f 8139273: Small improvements to DynamicLinker and DynamicLinkerFactory
attila
parents: 28881
diff changeset
   130
public final class DynamicLinkerFactory {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
    /**
33333
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   132
     * Default value for {@link #setUnstableRelinkThreshold(int) unstable relink
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   133
     * threshold}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   135
    public static final int DEFAULT_UNSTABLE_RELINK_THRESHOLD = 8;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   136
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   137
    private boolean classLoaderExplicitlySet = false;
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   138
    private ClassLoader classLoader;
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   139
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   140
    private List<? extends GuardingDynamicLinker> prioritizedLinkers;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   141
    private List<? extends GuardingDynamicLinker> fallbackLinkers;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   142
    private boolean syncOnRelink = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   143
    private int unstableRelinkThreshold = DEFAULT_UNSTABLE_RELINK_THRESHOLD;
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   144
    private GuardedInvocationTransformer prelinkTransformer;
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   145
    private MethodTypeConversionStrategy autoConversionStrategy;
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   146
    private MethodHandleTransformer internalObjectsFilter;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   147
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   148
    private List<ServiceConfigurationError> autoLoadingErrors = Collections.emptyList();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   149
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   150
    /**
33333
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   151
     * Creates a new dynamic linker factory with default configuration. Upon
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   152
     * creation, the factory can be configured using various {@code setXxx()}
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   153
     * methods and used to create one or more dynamic linkers according to its
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   154
     * current configuration using {@link #createLinker()}.
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   155
     */
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   156
    public DynamicLinkerFactory() {
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   157
    }
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   158
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   159
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   160
     * Sets the class loader for automatic discovery of available guarding
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   161
     * dynamic linkers. {@link GuardingDynamicLinkerExporter} implementations
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   162
     * available through this class loader will be automatically instantiated
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   163
     * using the {@link ServiceLoader} mechanism and the linkers they provide
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   164
     * will be incorporated into {@code DynamicLinker}s that this factory
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   165
     * creates. This allows for cross-language interoperability where call sites
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   166
     * belonging to this language runtime can be linked by linkers from these
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   167
     * automatically discovered runtimes if their native objects are passed to
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   168
     * this runtime. If class loader is not set explicitly by invoking this
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   169
     * method, then the thread context class loader of the thread invoking
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   170
     * {@link #createLinker()} will be used. If this method is invoked
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   171
     * explicitly with null then {@link ServiceLoader#loadInstalled(Class)} will
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   172
     * be used to load the linkers.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
     *
33333
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   174
     * @param classLoader the class loader used for the automatic discovery of
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33332
diff changeset
   175
     * available linkers.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   176
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   177
    public void setClassLoader(final ClassLoader classLoader) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   178
        this.classLoader = classLoader;
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   179
        classLoaderExplicitlySet = true;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   180
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   181
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   182
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   183
     * Sets the prioritized guarding dynamic linkers. Language runtimes using
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   184
     * Dynalink will usually have at least one linker for their own language.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   185
     * These linkers will be consulted first by the resulting dynamic linker
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   186
     * when it is linking call sites, before any autodiscovered and fallback
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   187
     * linkers. If the factory also autodiscovers a linker class matching one
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   188
     * of the prioritized linkers, the autodiscovered class will be ignored and
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   189
     * the explicit prioritized instance will be used.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   190
     *
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   191
     * @param prioritizedLinkers the list of prioritized linkers. Can be null.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   192
     * @throws NullPointerException if any of the list elements are null.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   193
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   194
    public void setPrioritizedLinkers(final List<? extends GuardingDynamicLinker> prioritizedLinkers) {
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   195
        this.prioritizedLinkers = copyListRequireNonNullElements(prioritizedLinkers);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   197
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   198
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   199
     * Sets the prioritized guarding dynamic linkers. Identical to calling
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   200
     * {@link #setPrioritizedLinkers(List)} with
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   201
     * {@code Arrays.asList(prioritizedLinkers)}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
     *
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   203
     * @param prioritizedLinkers an array of prioritized linkers. Can be null.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   204
     * @throws NullPointerException if any of the array elements are null.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   206
    public void setPrioritizedLinkers(final GuardingDynamicLinker... prioritizedLinkers) {
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   207
        setPrioritizedLinkers(prioritizedLinkers == null ? null : Arrays.asList(prioritizedLinkers));
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   208
    }
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   209
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   210
    /**
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   211
     * Sets a single prioritized linker. Identical to calling
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   212
     * {@link #setPrioritizedLinkers(List)} with a single-element list.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   213
     *
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   214
     * @param prioritizedLinker the single prioritized linker. Must not be null.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   215
     * @throws NullPointerException if null is passed.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   216
     */
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   217
    public void setPrioritizedLinker(final GuardingDynamicLinker prioritizedLinker) {
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   218
        this.prioritizedLinkers = Collections.singletonList(Objects.requireNonNull(prioritizedLinker));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   220
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   221
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   222
     * Sets the fallback guarding dynamic linkers. These linkers will be
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   223
     * consulted last by the resulting dynamic linker when it is linking call
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   224
     * sites, after any autodiscovered and prioritized linkers. If the factory
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   225
     * also autodiscovers a linker class matching one of the fallback linkers,
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   226
     * the autodiscovered class will be ignored and the explicit fallback
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   227
     * instance will be used.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   228
     *
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   229
     * @param fallbackLinkers the list of fallback linkers. Can be empty to
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   230
     * indicate the caller wishes to set no fallback linkers. Note that if this
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   231
     * method is not invoked explicitly or is passed null, then the factory
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   232
     * will create an instance of {@link BeansLinker} to serve as the default
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   233
     * fallback linker.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   234
     * @throws NullPointerException if any of the list elements are null.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
     */
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   236
    public void setFallbackLinkers(final List<? extends GuardingDynamicLinker> fallbackLinkers) {
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   237
        this.fallbackLinkers = copyListRequireNonNullElements(fallbackLinkers);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   238
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   239
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   240
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   241
     * Sets the fallback guarding dynamic linkers. Identical to calling
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   242
     * {@link #setFallbackLinkers(List)} with
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   243
     * {@code Arrays.asList(fallbackLinkers)}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   244
     *
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   245
     * @param fallbackLinkers an array of fallback linkers. Can be empty to
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   246
     * indicate the caller wishes to set no fallback linkers. Note that if this
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   247
     * method is not invoked explicitly or is passed null, then the factory
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   248
     * will create an instance of {@link BeansLinker} to serve as the default
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   249
     * fallback linker.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   250
     * @throws NullPointerException if any of the array elements are null.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   251
     */
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   252
    public void setFallbackLinkers(final GuardingDynamicLinker... fallbackLinkers) {
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   253
        setFallbackLinkers(fallbackLinkers == null ? null : Arrays.asList(fallbackLinkers));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   254
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   255
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   256
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   257
     * Sets whether the dynamic linker created by this factory will invoke
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   258
     * {@link MutableCallSite#syncAll(MutableCallSite[])} after a call site is
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   259
     * relinked. Defaults to false. You probably want to set it to true if your
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   260
     * runtime supports multithreaded execution of dynamically linked code.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   261
     * @param syncOnRelink true for invoking sync on relink, false otherwise.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   262
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   263
    public void setSyncOnRelink(final boolean syncOnRelink) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   264
        this.syncOnRelink = syncOnRelink;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   265
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   266
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   267
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   268
     * Sets the unstable relink threshold; the number of times a call site is
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   269
     * relinked after which it will be considered unstable, and subsequent link
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   270
     * requests for it will indicate this.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   271
     * @param unstableRelinkThreshold the new threshold. Must not be less than
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   272
     * zero. The value of zero means that call sites will never be considered
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   273
     * unstable.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   274
     * @see LinkRequest#isCallSiteUnstable()
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   275
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   276
    public void setUnstableRelinkThreshold(final int unstableRelinkThreshold) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   277
        if(unstableRelinkThreshold < 0) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   278
            throw new IllegalArgumentException("unstableRelinkThreshold < 0");
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   279
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   280
        this.unstableRelinkThreshold = unstableRelinkThreshold;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   281
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   282
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   283
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   284
     * Set the pre-link transformer. This is a
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   285
     * {@link GuardedInvocationTransformer} that will get the final chance to
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   286
     * modify the guarded invocation after it has been created by a component
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   287
     * linker and before the dynamic linker links it into the call site. It is
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   288
     * normally used to adapt the return value type of the invocation to the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   289
     * type of the call site. When not set explicitly, a default pre-link
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   290
     * transformer will be used that simply calls
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   291
     * {@link GuardedInvocation#asType(LinkerServices, MethodType)}. Customized
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   292
     * pre-link transformers are rarely needed; they are mostly used as a
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   293
     * building block for implementing advanced techniques such as code
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   294
     * deoptimization strategies.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   295
     * @param prelinkTransformer the pre-link transformer for the dynamic
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   296
     * linker. Can be null to have the factory use the default transformer.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   297
     */
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   298
    public void setPrelinkTransformer(final GuardedInvocationTransformer prelinkTransformer) {
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   299
        this.prelinkTransformer = prelinkTransformer;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   300
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   301
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   302
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   303
     * Sets an object representing the conversion strategy for automatic type
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   304
     * conversions. After
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   305
     * {@link LinkerServices#asType(MethodHandle, MethodType)} has applied all
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   306
     * custom conversions to a method handle, it still needs to effect
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   307
     * {@link TypeUtilities#isMethodInvocationConvertible(Class, Class) method
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   308
     * invocation conversions} that can usually be automatically applied as per
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   309
     * {@link MethodHandle#asType(MethodType)}. However, sometimes language
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   310
     * runtimes will want to customize even those conversions for their own call
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   311
     * sites. A typical example is allowing unboxing of null return values,
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   312
     * which is by default prohibited by ordinary
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   313
     * {@code MethodHandles.asType()}. In this case, a language runtime can
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   314
     * install its own custom automatic conversion strategy, that can deal with
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   315
     * null values. Note that when the strategy's
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   316
     * {@link MethodTypeConversionStrategy#asType(MethodHandle, MethodType)}
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   317
     * is invoked, the custom language conversions will already have been
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   318
     * applied to the method handle, so by design the difference between the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   319
     * handle's current method type and the desired final type will always only
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   320
     * be ones that can be subjected to method invocation conversions. The
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   321
     * strategy also doesn't need to invoke a final
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   322
     * {@code MethodHandle.asType()} as that will be done internally as the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   323
     * final step.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   324
     * @param autoConversionStrategy the strategy for applying method invocation
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   325
     * conversions for the linker created by this factory. Can be null for no
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   326
     * custom strategy.
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   327
     */
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   328
    public void setAutoConversionStrategy(final MethodTypeConversionStrategy autoConversionStrategy) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   329
        this.autoConversionStrategy = autoConversionStrategy;
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   330
    }
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   331
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   332
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   333
     * Sets a method handle transformer that is supposed to act as the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   334
     * implementation of
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   335
     * {@link LinkerServices#filterInternalObjects(MethodHandle)} for linker
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   336
     * services of dynamic linkers created by this factory. Some language
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   337
     * runtimes can have internal objects that should not escape their scope.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   338
     * They can add a transformer here that will modify the method handle so
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   339
     * that any parameters that can receive potentially internal language
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   340
     * runtime objects will have a filter added on them to prevent them from
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   341
     * escaping, potentially by wrapping them. The transformer can also
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   342
     * potentially add an unwrapping filter to the return value.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   343
     * {@link DefaultInternalObjectFilter} is provided as a convenience class
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   344
     * for easily creating such filtering transformers.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   345
     * @param internalObjectsFilter a method handle transformer filtering out
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   346
     * internal objects, or null.
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   347
     */
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   348
    public void setInternalObjectsFilter(final MethodHandleTransformer internalObjectsFilter) {
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   349
        this.internalObjectsFilter = internalObjectsFilter;
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   350
    }
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   351
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   352
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   353
     * Creates a new dynamic linker based on the current configuration. This
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   354
     * method can be invoked more than once to create multiple dynamic linkers.
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   355
     * Automatically discovered linkers are newly instantiated on every
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   356
     * invocation of this method. It is allowed to change the factory's
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   357
     * configuration between invocations. The method is not thread safe. After
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   358
     * invocation, callers can invoke {@link #getAutoLoadingErrors()} to
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   359
     * retrieve a list of {@link ServiceConfigurationError}s that occurred while
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   360
     * trying to load automatically discovered linkers. These are never thrown
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   361
     * from the call to this method as it makes every effort to recover from
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   362
     * them and ignore the failing linkers.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   363
     * @return the new dynamic Linker
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   364
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   365
    public DynamicLinker createLinker() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   366
        // Treat nulls appropriately
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   367
        if(prioritizedLinkers == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   368
            prioritizedLinkers = Collections.emptyList();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   369
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   370
        if(fallbackLinkers == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   371
            fallbackLinkers = Collections.singletonList(new BeansLinker());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   372
        }
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
        // Gather classes of all precreated (prioritized and fallback) linkers.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   375
        // We'll filter out any discovered linkers of the same class.
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   376
        final Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses =
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   377
                new HashSet<>();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   378
        addClasses(knownLinkerClasses, prioritizedLinkers);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   379
        addClasses(knownLinkerClasses, fallbackLinkers);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   380
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   381
        final List<GuardingDynamicLinker> discovered = discoverAutoLoadLinkers();
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
   382
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   383
        // Now, concatenate ...
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   384
        final List<GuardingDynamicLinker> linkers =
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   385
                new ArrayList<>(prioritizedLinkers.size() + discovered.size()
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   386
                        + fallbackLinkers.size());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   387
        // ... prioritized linkers, ...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   388
        linkers.addAll(prioritizedLinkers);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   389
        // ... filtered discovered linkers, ...
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   390
        for(final GuardingDynamicLinker linker: discovered) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   391
            if(!knownLinkerClasses.contains(linker.getClass())) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   392
                linkers.add(linker);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   393
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   394
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   395
        // ... and finally fallback linkers.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   396
        linkers.addAll(fallbackLinkers);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   397
        final List<GuardingDynamicLinker> optimized = CompositeTypeBasedGuardingDynamicLinker.optimize(linkers);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
        final GuardingDynamicLinker composite;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   399
        switch(linkers.size()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   400
            case 0: {
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
   401
                composite = (r, s) -> null; // linker that can't link anything
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   402
                break;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   403
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   404
            case 1: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
                composite = optimized.get(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
                break;
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
            default: {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   409
                composite = new CompositeGuardingDynamicLinker(optimized);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   410
                break;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   411
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   412
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   413
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   414
        final List<GuardingTypeConverterFactory> typeConverters = new LinkedList<>();
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   415
        for(final GuardingDynamicLinker linker: linkers) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   416
            if(linker instanceof GuardingTypeConverterFactory) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   417
                typeConverters.add((GuardingTypeConverterFactory)linker);
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
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   421
        if(prelinkTransformer == null) {
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   422
            prelinkTransformer = (inv, request, linkerServices) -> inv.asType(linkerServices, request.getCallSiteDescriptor().getMethodType());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   423
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19461
diff changeset
   424
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   425
        return new DynamicLinker(new LinkerServicesImpl(new TypeConverterFactory(typeConverters,
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33333
diff changeset
   426
                autoConversionStrategy), composite, internalObjectsFilter), prelinkTransformer,
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 27360
diff changeset
   427
                syncOnRelink, unstableRelinkThreshold);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   428
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   429
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   430
    /**
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   431
     * Returns a list of {@link ServiceConfigurationError}s that were
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   432
     * encountered while loading automatically discovered linkers during the
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   433
     * last invocation of {@link #createLinker()}. They can be any non-Dynalink
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   434
     * specific service configuration issues, as well as some Dynalink-specific
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   435
     * errors when an exporter that the factory tried to automatically load:
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   436
     * <ul>
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   437
     * <li>did not have the runtime permission named
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   438
     * {@link GuardingDynamicLinkerExporter#AUTOLOAD_PERMISSION_NAME} in a
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   439
     * system with a security manager, or</li>
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   440
     * <li>returned null from {@link GuardingDynamicLinkerExporter#get()}, or</li>
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   441
     * <li>the list returned from {@link GuardingDynamicLinkerExporter#get()}
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   442
     * had a null element.</li>
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   443
     * </ul>
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   444
     * @return an immutable list of encountered
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   445
     * {@link ServiceConfigurationError}s. Can be empty.
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   446
     */
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   447
    public List<ServiceConfigurationError> getAutoLoadingErrors() {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   448
        return Collections.unmodifiableList(autoLoadingErrors);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   449
    }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   450
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   451
    private List<GuardingDynamicLinker> discoverAutoLoadLinkers() {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   452
        autoLoadingErrors = new LinkedList<>();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   453
        final ClassLoader effectiveClassLoader = classLoaderExplicitlySet ? classLoader : getThreadContextClassLoader();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   454
        final List<GuardingDynamicLinker> discovered = new LinkedList<>();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   455
        try {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   456
            final ServiceLoader<GuardingDynamicLinkerExporter> linkerLoader =
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   457
                    AccessController.doPrivileged((PrivilegedAction<ServiceLoader<GuardingDynamicLinkerExporter>>)()-> {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   458
                        if (effectiveClassLoader == null) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   459
                            return ServiceLoader.loadInstalled(GuardingDynamicLinkerExporter.class);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   460
                        }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   461
                        return ServiceLoader.load(GuardingDynamicLinkerExporter.class, effectiveClassLoader);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   462
                    });
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   463
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   464
            for(final Iterator<GuardingDynamicLinkerExporter> it = linkerLoader.iterator(); it.hasNext();) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   465
                try {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   466
                    final GuardingDynamicLinkerExporter autoLoader = it.next();
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   467
                    try {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   468
                        discovered.addAll(requireNonNullElements(
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   469
                                Objects.requireNonNull(autoLoader.get(),
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   470
                                        ()->(autoLoader.getClass().getName() + " returned null from get()")),
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   471
                                ()->(autoLoader.getClass().getName() + " returned a list with at least one null element")));
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   472
                    } catch (final ServiceConfigurationError|VirtualMachineError e) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   473
                        // Don't wrap a SCE in another SCE. Also, don't ignore
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   474
                        // any VME (e.g. StackOverflowError or OutOfMemoryError).
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   475
                        throw e;
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   476
                    } catch (final Throwable t) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   477
                        throw new ServiceConfigurationError(t.getMessage(), t);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   478
                    }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   479
                } catch (final ServiceConfigurationError e) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   480
                    // Catch SCE with an individual exporter, carry on with it.hasNext().
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   481
                    autoLoadingErrors.add(e);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   482
                }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   483
            }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   484
        } catch (final ServiceConfigurationError e) {
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   485
            // Catch a top-level SCE; one either in ServiceLoader.load(),
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   486
            // ServiceLoader.iterator(), or Iterator.hasNext().
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   487
            autoLoadingErrors.add(e);
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   488
        }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   489
        return discovered;
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   490
    }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   491
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   492
    private static ClassLoader getThreadContextClassLoader() {
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   493
        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   494
            @Override
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   495
            public ClassLoader run() {
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   496
                return Thread.currentThread().getContextClassLoader();
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   497
            }
19461
30ba4fba981d 8022789: Revisit doPrivileged blocks in Dynalink
attila
parents: 19455
diff changeset
   498
        }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
19455
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   499
    }
b972b61a6921 8022509: Various Dynalink security enhancements
attila
parents: 16245
diff changeset
   500
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   501
    private static void addClasses(final Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses,
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   502
            final List<? extends GuardingDynamicLinker> linkers) {
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24719
diff changeset
   503
        for(final GuardingDynamicLinker linker: linkers) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   504
            knownLinkerClasses.add(linker.getClass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   505
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   506
    }
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   507
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   508
    private static <T> List<T> copyListRequireNonNullElements(final List<T> list) {
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   509
        if (list == null) {
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   510
            return null;
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   511
        }
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   512
        return new ArrayList<>(requireNonNullElements(list, ()->"List has at least one null element"));
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   513
    }
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   514
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   515
    private static <T> List<T> requireNonNullElements(final List<T> list, final Supplier<String> msgSupplier) {
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   516
        for(final T t: list) {
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   517
            Objects.requireNonNull(t, msgSupplier);
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   518
        }
33340
6c945c826f36 8139895: Introduce GuardingDynamicLinkerExporter
attila
parents: 33339
diff changeset
   519
        return list;
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   520
    }
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   521
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   522
}