src/jdk.dynalink/share/classes/jdk/dynalink/CallSiteDescriptor.java
author mbaesken
Tue, 02 Jul 2019 13:45:08 +0200
changeset 55556 19d0b382f086
parent 50695 36ca515343e0
permissions -rw-r--r--
8226238: Improve error output and fix elf issues in os::dll_load Reviewed-by: clanger, mdoerr
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
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    36
   Redistribution and use in source and binary forms, with or without
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    37
   modification, are permitted provided that the following conditions are
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    38
   met:
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    39
   * Redistributions of source code must retain the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    40
     notice, this list of conditions and the following disclaimer.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    41
   * Redistributions in binary form must reproduce the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    42
     notice, this list of conditions and the following disclaimer in the
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    43
     documentation and/or other materials provided with the distribution.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    44
   * Neither the name of the copyright holder nor the names of
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    45
     contributors may be used to endorse or promote products derived from
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    46
     this software without specific prior written permission.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    48
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    49
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    50
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    51
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    52
   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    53
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    54
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    55
   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    56
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    57
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    58
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33343
diff changeset
    61
package jdk.dynalink;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    63
import java.lang.invoke.MethodHandles;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
import java.lang.invoke.MethodHandles.Lookup;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
import java.lang.invoke.MethodType;
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    66
import java.util.Objects;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
    67
import java.util.function.Supplier;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    68
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    69
/**
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    70
 * Call site descriptors contain all the information necessary for linking a
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    71
 * call site. This information is normally passed as parameters to bootstrap
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    72
 * methods and consists of the {@code MethodHandles.Lookup} object on the caller
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    73
 * class in which the call site occurs, the dynamic operation at the call
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    74
 * site, and the method type of the call site. {@code CallSiteDescriptor}
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    75
 * objects are used in Dynalink to capture and store these parameters for
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    76
 * subsequent use by the {@link DynamicLinker}.
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33333
diff changeset
    77
 * <p>
33333
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33331
diff changeset
    78
 * The constructors of built-in {@link RelinkableCallSite} implementations all
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33333
diff changeset
    79
 * take a call site descriptor.
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33333
diff changeset
    80
 * <p>
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    81
 * Call site descriptors must be immutable. You can use this class as-is or you
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    82
 * can subclass it, especially if you need to add further information to the
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    83
 * descriptors (typically, values passed in additional parameters to the
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    84
 * bootstrap method. Since the descriptors must be immutable, you can set up a
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    85
 * cache for equivalent descriptors to have the call sites share them.
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    86
 * <p>
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    87
 * The class extends {@link SecureLookupSupplier} for security-checked access to
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    88
 * the {@code MethodHandles.Lookup} object it carries. This lookup should be used
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    89
 * to find method handles to set as targets of the call site described by this
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    90
 * descriptor.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
 */
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    92
public class CallSiteDescriptor extends SecureLookupSupplier {
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
    93
    private final Operation operation;
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    94
    private final MethodType methodType;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    95
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    96
    /**
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    97
     * Creates a new call site descriptor.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
    98
     * @param lookup the lookup object describing the class the call site belongs to.
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
    99
     * When creating descriptors from a {@link java.lang.invoke} bootstrap method,
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   100
     * it should be the lookup passed to the bootstrap.
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   101
     * @param operation the dynamic operation at the call site.
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   102
     * @param methodType the method type of the call site. When creating
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   103
     * descriptors from a {@link java.lang.invoke} bootstrap method, it should be
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   104
     * the method type passed to the bootstrap.
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   105
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   106
    public CallSiteDescriptor(final Lookup lookup, final Operation operation, final MethodType methodType) {
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   107
        super(lookup);
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   108
        this.operation = Objects.requireNonNull(operation, "name");
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   109
        this.methodType = Objects.requireNonNull(methodType, "methodType");
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   110
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   111
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   112
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   113
     * Returns the operation at the call site.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   114
     * @return the operation at the call site.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   116
    public final Operation getOperation() {
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   117
        return operation;
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   118
    }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   120
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   121
     * The type of the method at the call site.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
     * @return type of the method at the call site.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
     */
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   125
    public final MethodType getMethodType() {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   126
        return methodType;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   127
    }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   128
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   130
     * Finds or creates a call site descriptor that only differs in its
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   131
     * method type from this descriptor.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   132
     * Invokes {@link #changeMethodTypeInternal(MethodType)}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
     * @param newMethodType the new method type
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   135
     * @return a call site descriptor with changed method type.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   136
     * @throws NullPointerException if {@code newMethodType} is null.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   137
     */
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   138
    public final CallSiteDescriptor changeMethodType(final MethodType newMethodType) {
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   139
        final CallSiteDescriptor changed = changeMethodTypeInternal(newMethodType);
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   140
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   141
        if (getClass() != CallSiteDescriptor.class) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   142
            assertChangeInvariants(changed, "changeMethodTypeInternal");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   143
            alwaysAssert(operation == changed.operation, () -> "changeMethodTypeInternal must not change the descriptor's operation");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   144
            alwaysAssert(newMethodType == changed.methodType, () -> "changeMethodTypeInternal didn't set the correct new method type");
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   145
        }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   146
        return changed;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   147
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   148
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   149
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   150
     * Finds or creates a call site descriptor that only differs in its
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   151
     * method type from this descriptor. Subclasses must override this method
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   152
     * to return an object of their exact class. If an overridden method changes
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   153
     * something other than the method type in the descriptor (its class, lookup,
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   154
     * or operation), or returns null, an {@code AssertionError} will be thrown
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   155
     * from {@link #changeMethodType(MethodType)}.
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   156
     *
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   157
     * @param newMethodType the new method type
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   158
     * @return a call site descriptor with the changed method type.
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   159
     */
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   160
    protected CallSiteDescriptor changeMethodTypeInternal(final MethodType newMethodType) {
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   161
        return new CallSiteDescriptor(getLookupPrivileged(), operation, newMethodType);
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   162
    }
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
   163
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
   164
    /**
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   165
     * Finds or creates a call site descriptor that only differs in its
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   166
     * operation from this descriptor.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   167
     * Invokes {@link #changeOperationInternal(Operation)}.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   168
     *
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   169
     * @param newOperation the new operation
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   170
     * @return a call site descriptor with the changed operation.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   171
     * @throws NullPointerException if {@code newOperation} is null.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   172
     * @throws SecurityException if the descriptor's lookup isn't the
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   173
     * {@link MethodHandles#publicLookup()}, and a security manager is present,
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   174
     * and a check for {@code RuntimePermission("dynalink.getLookup")} fails.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   175
     * This is necessary as changing the operation in the call site descriptor
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   176
     * allows fabrication of descriptors for arbitrary operations with the lookup.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   177
     */
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   178
    public final CallSiteDescriptor changeOperation(final Operation newOperation) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   179
        getLookup(); // force security check
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   180
        final CallSiteDescriptor changed = changeOperationInternal(newOperation);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   181
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   182
        if (getClass() != CallSiteDescriptor.class) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   183
            assertChangeInvariants(changed, "changeOperationInternal");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   184
            alwaysAssert(methodType == changed.methodType, () -> "changeOperationInternal must not change the descriptor's method type");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   185
            alwaysAssert(newOperation == changed.operation, () -> "changeOperationInternal didn't set the correct new operation");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   186
        }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   187
        return changed;
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   188
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   189
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   190
    /**
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   191
     * Finds or creates a call site descriptor that only differs in its
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   192
     * operation from this descriptor. Subclasses must override this method
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   193
     * to return an object of their exact class. If an overridden method changes
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   194
     * something other than the operation in the descriptor (its class, lookup,
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   195
     * or method type), or returns null, an {@code AssertionError} will be thrown
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   196
     * from {@link #changeOperation(Operation)}.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   197
     *
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   198
     * @param newOperation the new operation
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   199
     * @return a call site descriptor with the changed operation.
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   200
     */
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   201
    protected CallSiteDescriptor changeOperationInternal(final Operation newOperation) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   202
        return new CallSiteDescriptor(getLookupPrivileged(), newOperation, methodType);
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   203
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   204
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   205
    /**
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   206
     * Returns true if this call site descriptor is equal to the passed object.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   207
     * It is considered equal if the other object is of the exact same class,
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   208
     * their operations and method types are equal, and their lookups have the
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   209
     * same {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   210
     * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}.
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
   211
     */
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   212
    @Override
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   213
    public boolean equals(final Object obj) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   214
        if (obj == this) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   215
            return true;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   216
        } else if (obj == null) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   217
            return false;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   218
        } else if (obj.getClass() != getClass()) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   219
            return false;
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   220
        }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   221
        final CallSiteDescriptor other = (CallSiteDescriptor)obj;
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   222
        return operation.equals(other.operation) &&
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   223
               methodType.equals(other.methodType) &&
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   224
               lookupsEqual(getLookupPrivileged(), other.getLookupPrivileged());
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   225
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   226
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   227
    /**
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   228
     * Compares two lookup objects for value-based equality. They are considered
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   229
     * equal if they have the same
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   230
     * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   231
     * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   232
     * @param l1 first lookup
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   233
     * @param l2 second lookup
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   234
     * @return true if the two lookups are equal, false otherwise.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   235
     */
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   236
    private static boolean lookupsEqual(final Lookup l1, final Lookup l2) {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   237
        return l1.lookupClass() == l2.lookupClass() && l1.lookupModes() == l2.lookupModes();
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   238
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   239
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   240
    /**
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   241
     * Returns a value-based hash code of this call site descriptor computed
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   242
     * from its operation, method type, and lookup object's lookup class and
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   243
     * lookup modes.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   244
     * @return value-based hash code for this call site descriptor.
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   245
     */
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   246
    @Override
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   247
    public int hashCode() {
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   248
        return operation.hashCode() + 31 * methodType.hashCode() + 31 * 31 * lookupHashCode(getLookupPrivileged());
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   249
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   250
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   251
    /**
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   252
     * Returns a value-based hash code for the passed lookup object. It is
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   253
     * based on the lookup object's
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   254
     * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   255
     * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()} values.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   256
     * @param lookup the lookup object.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   257
     * @return a hash code for the object..
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   258
     */
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   259
    private static int lookupHashCode(final Lookup lookup) {
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   260
        return lookup.lookupClass().hashCode() + 31 * lookup.lookupModes();
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   261
    }
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   262
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   263
    /**
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   264
     * Returns the string representation of this call site descriptor, of the
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   265
     * format {@code name(parameterTypes)returnType@lookup}.
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   266
     */
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   267
    @Override
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   268
    public String toString() {
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   269
        final String mt = methodType.toString();
36686
a351eacd4c42 8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents: 34447
diff changeset
   270
        final String l = getLookupPrivileged().toString();
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   271
        final String o = operation.toString();
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   272
        final StringBuilder b = new StringBuilder(o.length() + mt.length() + 1 + l.length());
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33342
diff changeset
   273
        return b.append(o).append(mt).append('@').append(l).toString();
33342
86b4260bb17a 8139919: Make CallSiteDescriptor a concrete class
attila
parents: 33339
diff changeset
   274
    }
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   275
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   276
    private void assertChangeInvariants(final CallSiteDescriptor changed, final String caller) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   277
        alwaysAssert(changed != null, () -> caller + " must not return null.");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   278
        alwaysAssert(getClass() == changed.getClass(), () -> caller + " must not change the descriptor's class");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   279
        alwaysAssert(lookupsEqual(getLookupPrivileged(), changed.getLookupPrivileged()), () -> caller + " must not change the descriptor's lookup");
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   280
    }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   281
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   282
    private static void alwaysAssert(final boolean cond, final Supplier<String> errorMessage) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   283
        if (!cond) {
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   284
            throw new AssertionError(errorMessage.get());
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   285
        }
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 36686
diff changeset
   286
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   287
}