src/jdk.dynalink/share/classes/jdk/dynalink/linker/LinkerServices.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 36686 nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/linker/LinkerServices.java@a351eacd4c42
child 50695 36ca515343e0
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33339
diff changeset
    84
package jdk.dynalink.linker;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    86
import java.lang.invoke.MethodHandle;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.lang.invoke.MethodHandles;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.lang.invoke.MethodType;
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    89
import java.util.function.Supplier;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33339
diff changeset
    90
import jdk.dynalink.DynamicLinker;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33339
diff changeset
    91
import jdk.dynalink.DynamicLinkerFactory;
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    92
import jdk.dynalink.SecureLookupSupplier;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33339
diff changeset
    93
import jdk.dynalink.linker.ConversionComparator.Comparison;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33339
diff changeset
    94
import jdk.dynalink.linker.support.TypeUtilities;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    95
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    96
/**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
    97
 * Interface for services provided to {@link GuardingDynamicLinker} instances by
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
    98
 * the {@link DynamicLinker} that owns them.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   100
public interface LinkerServices {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   102
     * Similar to {@link MethodHandle#asType(MethodType)} except it also hooks
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   103
     * in method handles produced by all available
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   104
     * {@link GuardingTypeConverterFactory} implementations, providing for
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   105
     * language-specific type coercing of parameters. It will apply
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   106
     * {@link MethodHandle#asType(MethodType)} for all primitive-to-primitive,
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   107
     * wrapper-to-primitive, primitive-to-wrapper conversions as well as for all
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   108
     * upcasts. For all other conversions, it'll insert
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   109
     * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)}
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   110
     * with composite filters provided by {@link GuardingTypeConverterFactory}
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   111
     * implementations.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
     * @param handle target method handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
     * @param fromType the types of source arguments
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   115
     * @return a method handle that is a suitable combination of
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   116
     * {@link MethodHandle#asType(MethodType)},
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   117
     * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)},
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   118
     * and {@link MethodHandles#filterReturnValue(MethodHandle, MethodHandle)}
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   119
     * with {@link GuardingTypeConverterFactory}-produced type converters as
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   120
     * filters.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   121
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
    public MethodHandle asType(MethodHandle handle, MethodType fromType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   125
     * Similar to {@link #asType(MethodHandle, MethodType)} except it treats
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   126
     * return value type conversion specially. It only converts the return type
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   127
     * of the method handle when it can be done using a conversion that loses
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   128
     * neither precision nor magnitude, otherwise it leaves it unchanged. These
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   129
     * are the only return value conversions that should be performed by
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   130
     * individual language-specific linkers, and
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   131
     * {@link DynamicLinkerFactory#setPrelinkTransformer(GuardedInvocationTransformer)
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   132
     * pre-link transformer of the dynamic linker} should implement the strategy
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   133
     * for dealing with potentially lossy return type conversions in a manner
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   134
     * specific to the language runtime where the call site is located.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   135
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   136
     * @param handle target method handle
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   137
     * @param fromType the types of source arguments
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   138
     * @return a method handle that is a suitable combination of
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   139
     * {@link MethodHandle#asType(MethodType)}, and
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   140
     * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)}
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   141
     * with {@link GuardingTypeConverterFactory}-produced type converters as filters.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   142
     */
33004
e4de5829b493 8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
attila
parents: 28881
diff changeset
   143
    public default MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) {
e4de5829b493 8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
attila
parents: 28881
diff changeset
   144
        final Class<?> handleReturnType = handle.type().returnType();
e4de5829b493 8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
attila
parents: 28881
diff changeset
   145
        return asType(handle, TypeUtilities.isConvertibleWithoutLoss(handleReturnType, fromType.returnType()) ?
e4de5829b493 8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
attila
parents: 28881
diff changeset
   146
                fromType : fromType.changeReturnType(handleReturnType));
e4de5829b493 8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
attila
parents: 28881
diff changeset
   147
    }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   148
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   149
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   150
     * Given a source and target type, returns a method handle that converts
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   151
     * between them. Never returns null; in worst case it will return an
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   152
     * identity conversion (that might fail for some values at runtime). You
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   153
     * rarely need to use this method directly and should mostly rely on
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   154
     * {@link #asType(MethodHandle, MethodType)} instead. This method is needed
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   155
     * when you need to reuse existing type conversion machinery outside the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   156
     * context of processing a link request.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   157
     * @param sourceType the type to convert from
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   158
     * @param targetType the type to convert to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   159
     * @return a method handle performing the conversion.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   160
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   161
    public MethodHandle getTypeConverter(Class<?> sourceType, Class<?> targetType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   162
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   163
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   164
     * Returns true if there might exist a conversion between the requested
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   165
     * types (either an automatic JVM conversion, or one provided by any
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   166
     * available {@link GuardingTypeConverterFactory}), or false if there
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   167
     * definitely does not exist a conversion between the requested types. Note
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   168
     * that returning true does not guarantee that the conversion will succeed
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   169
     * at runtime for all values (especially if the "from" or "to" types are
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   170
     * sufficiently generic), but returning false guarantees that it would fail.
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
     * @param from the source type for the conversion
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
     * @param to the target type for the conversion
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   174
     * @return true if there can be a conversion, false if there can not.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   175
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   176
    public boolean canConvert(Class<?> from, Class<?> to);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   177
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   178
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   179
     * Creates a guarded invocation delegating back to the {@link DynamicLinker}
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   180
     * that exposes this linker services object. The dynamic linker will then
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   181
     * itself delegate the linking to all of its managed
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   182
     * {@link GuardingDynamicLinker}s including potentially this one if no
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   183
     * linker responds earlier, so beware of infinite recursion. You'll
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   184
     * typically craft the link request so that it will be different than the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   185
     * one you are currently trying to link.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   186
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   187
     * @param linkRequest a request for linking the invocation
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   188
     * @return a guarded invocation linked by some of the guarding dynamic
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   189
     * linkers managed by the top-level dynamic linker. Can be null if no
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   190
     * available linker is able to link the invocation. You will typically use
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   191
     * the elements of the returned invocation to compose your own invocation.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
     * @throws Exception in case the top-level linker throws an exception
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   193
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
    public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest) throws Exception;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   195
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   197
     * Determines which of the two type conversions from a source type to the
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   198
     * two target types is preferred. This is used for dynamic overloaded method
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   199
     * resolution. If the source type is convertible to exactly one target type
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   200
     * with a method invocation conversion, it is chosen, otherwise available
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   201
     * {@link ConversionComparator}s are consulted.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
     * @param sourceType the source type.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
     * @param targetType1 one potential target type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
     * @param targetType2 another potential target type.
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   205
     * @return one of Comparison constants that establish which &ndash; if any
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   206
     * &ndash; of the target types is preferable for the conversion.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   207
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
    public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   209
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   210
    /**
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   211
     * Modifies the method handle so that any parameters that can receive
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   212
     * potentially internal language runtime objects will have a filter added on
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   213
     * them to prevent them from escaping, potentially by wrapping them. It can
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   214
     * also potentially add an unwrapping filter to the return value. Basically
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   215
     * transforms the method handle using the transformer configured by
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   216
     * {@link DynamicLinkerFactory#setInternalObjectsFilter(MethodHandleTransformer)}.
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   217
     * @param target the target method handle
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   218
     * @return a method handle with parameters and/or return type potentially
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33337
diff changeset
   219
     * filtered for wrapping and unwrapping.
28881
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   220
     */
0008daeef352 8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents: 25865
diff changeset
   221
    public MethodHandle filterInternalObjects(final MethodHandle target);
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   222
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   223
    /**
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   224
     * Executes an operation within the context of a particular
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   225
     * {@code MethodHandles.Lookup} lookup object. Normally, methods on
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   226
     * {@code LinkerServices} are invoked as part of the linking mechanism in
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   227
     * which case Dynalink internally maintains a per-thread current lookup
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   228
     * (the one belonging to the descriptor of the call site being linked). This
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   229
     * lookup can be retrieved by any {@link GuardingTypeConverterFactory}
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   230
     * involved in linking if it needs to generate lookup-sensitive converters.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   231
     * However, linker services' methods can be invoked outside the linking
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   232
     * process too when implementing invocation-time dispatch schemes, invoking
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   233
     * conversions at runtime, etc. If it becomes necessary to use any type
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   234
     * converter in this situation, and it needs a lookup, it will normally only
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   235
     * get {@link MethodHandles#publicLookup()} as the thread is not engaged in
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   236
     * a linking operation. If there is a way to meaningfully associate the
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   237
     * operation to the context of some caller class, consider performing it
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   238
     * within an invocation of this method and passing a full-strength lookup
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   239
     * for that class, as it will associate that lookup with the current thread
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   240
     * for the duration of the operation. Note that since you are passing a
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   241
     * {@link SecureLookupSupplier}, any invoked type converter factories will
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   242
     * still need to hold the necessary runtime permission to be able to get the
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   243
     * lookup should they need it.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   244
     * @param <T> the type of the return value provided by the passed-in supplier.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   245
     * @param operation the operation to execute in context of the specified lookup.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   246
     * @param lookupSupplier secure supplier of the lookup
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   247
     * @return the return value of the action
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   248
     * @throws NullPointerException if either action or lookupSupplier are null.
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   249
     * @see GuardingTypeConverterFactory#convertToType(Class, Class, Supplier)
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   250
     */
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   251
    public <T> T getWithLookup(final Supplier<T> operation, final SecureLookupSupplier lookupSupplier);
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   252
}