nashorn/src/jdk/internal/dynalink/support/Guards.java
author attila
Mon, 18 Feb 2013 16:00:15 +0100
changeset 16245 6a1c6c8bc113
parent 16234 86cb162cec6c
child 19455 b972b61a6921
permissions -rw-r--r--
8008371: Fix Dynalink compiler warnings and whitespace Reviewed-by: jlaskey, 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.support;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    86
import java.lang.invoke.MethodHandle;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.lang.invoke.MethodHandles;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.lang.invoke.MethodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
import java.util.logging.Level;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
import java.util.logging.Logger;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
import jdk.internal.dynalink.linker.LinkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
/**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
 * Utility methods for creating typical guards. TODO: introduce reasonable caching of created guards.
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
 * @author Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    97
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    98
public class Guards {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
    private static final Logger LOG = Logger
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   100
            .getLogger(Guards.class.getName(), "jdk.internal.dynalink.support.messages");
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   102
    private Guards() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   104
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   105
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
     * Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   107
     * returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
     * arguments will be ignored.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
     * @param clazz the class of the first argument to test for
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
     * @param type the method type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
     * @return a method handle testing whether its first argument is of the specified class.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
    @SuppressWarnings("boxing")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
    public static MethodHandle isOfClass(Class<?> clazz, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   116
        final Class<?> declaredType = type.parameterType(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
        if(clazz == declaredType) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
            LOG.log(Level.WARNING, "isOfClassGuardAlwaysTrue", new Object[] { clazz.getName(), 0, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
            return constantTrue(type);
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
        if(!declaredType.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
            LOG.log(Level.WARNING, "isOfClassGuardAlwaysFalse", new Object[] { clazz.getName(), 0, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
            return constantFalse(type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   125
        return getClassBoundArgumentTest(IS_OF_CLASS, clazz, 0, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   126
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   127
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
     * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   130
     * returns true if the first argument is instance of the specified class or its subclass). The rest of the arguments
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
     * will be ignored.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
     * @param clazz the class of the first argument to test for
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
     * @param type the method type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   135
     * @return a method handle testing whether its first argument is of the specified class or subclass.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   136
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   137
    public static MethodHandle isInstance(Class<?> clazz, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   138
        return isInstance(clazz, 0, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   139
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   140
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   141
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   142
     * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   143
     * returns true if the n'th argument is instance of the specified class or its subclass). The rest of the arguments
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   144
     * will be ignored.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   145
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   146
     * @param clazz the class of the first argument to test for
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   147
     * @param pos the position on the argument list to test
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   148
     * @param type the method type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   149
     * @return a method handle testing whether its first argument is of the specified class or subclass.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   150
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   151
    @SuppressWarnings("boxing")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   152
    public static MethodHandle isInstance(Class<?> clazz, int pos, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   153
        final Class<?> declaredType = type.parameterType(pos);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   154
        if(clazz.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   155
            LOG.log(Level.WARNING, "isInstanceGuardAlwaysTrue", new Object[] { clazz.getName(), pos, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   156
            return constantTrue(type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   157
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   158
        if(!declaredType.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   159
            LOG.log(Level.WARNING, "isInstanceGuardAlwaysFalse", new Object[] { clazz.getName(), pos, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   160
            return constantFalse(type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   161
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   162
        return getClassBoundArgumentTest(IS_INSTANCE, clazz, pos, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   163
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   164
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   165
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   166
     * Creates a method handle that returns true if the argument in the specified position is a Java array.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   167
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   168
     * @param pos the position in the argument lit
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   169
     * @param type the method type of the handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   170
     * @return a method handle that returns true if the argument in the specified position is a Java array; the rest of
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   171
     * the arguments are ignored.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   172
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
    @SuppressWarnings("boxing")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   174
    public static MethodHandle isArray(int pos, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   175
        final Class<?> declaredType = type.parameterType(pos);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   176
        if(declaredType.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   177
            LOG.log(Level.WARNING, "isArrayGuardAlwaysTrue", new Object[] { pos, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   178
            return constantTrue(type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   179
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   180
        if(!declaredType.isAssignableFrom(Object[].class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   181
            LOG.log(Level.WARNING, "isArrayGuardAlwaysFalse", new Object[] { pos, type });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   182
            return constantFalse(type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   183
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   184
        return asType(IS_ARRAY, pos, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   185
    }
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
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   188
     * Return true if it is safe to strongly reference a class from the referred class loader from a class associated
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   189
     * with the referring class loader without risking a class loader memory leak.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   190
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   191
     * @param referrerLoader the referrer class loader
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
     * @param referredLoader the referred class loader
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   193
     * @return true if it is safe to strongly reference the class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   195
    public static boolean canReferenceDirectly(ClassLoader referrerLoader, final ClassLoader referredLoader) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
        if(referredLoader == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   197
            // Can always refer directly to a system class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   198
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   199
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   200
        if(referrerLoader == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   201
            // System classes can't refer directly to any non-system class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
        // Otherwise, can only refer directly to classes residing in same or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
        // parent class loader.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   206
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   207
        ClassLoader referrer = referrerLoader;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
        do {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   209
            if(referrer == referredLoader) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   210
                return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   211
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
            referrer = referrer.getParent();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   213
        } while(referrer != null);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   215
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   216
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   217
    private static MethodHandle getClassBoundArgumentTest(MethodHandle test, Class<?> clazz, int pos, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   218
        // Bind the class to the first argument of the test
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
        return asType(test.bindTo(clazz), pos, type);
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   222
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   223
     * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Only applies
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
     * conversions as per {@link MethodHandle#asType(MethodType)}.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   225
     * @param test the test method handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   226
     * @param type the type to adapt the method handle to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   227
     * @return the adapted method handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   228
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   229
    public static MethodHandle asType(MethodHandle test, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   230
        return test.asType(getTestType(test, type));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   231
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   232
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   233
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   234
     * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Applies the passed
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
     * {@link LinkerServices} object's {@link LinkerServices#asType(MethodHandle, MethodType)}.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   236
     * @param linkerServices the linker services to use for type conversions
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
     * @param test the test method handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   238
     * @param type the type to adapt the method handle to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   239
     * @return the adapted method handle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   240
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   241
    public static MethodHandle asType(LinkerServices linkerServices, MethodHandle test, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   242
        return linkerServices.asType(test, getTestType(test, type));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   243
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   244
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   245
    private static MethodType getTestType(MethodHandle test, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   246
        return type.dropParameterTypes(test.type().parameterCount(),
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   247
                type.parameterCount()).changeReturnType(boolean.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   248
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   249
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   250
    private static MethodHandle asType(MethodHandle test, int pos, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   251
        assert test != null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   252
        assert type != null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   253
        assert type.parameterCount() > 0;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   254
        assert pos >= 0 && pos < type.parameterCount();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   255
        assert test.type().parameterCount() == 1;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   256
        assert test.type().returnType() == Boolean.TYPE;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   257
        return MethodHandles.permuteArguments(test.asType(test.type().changeParameterType(0, type.parameterType(pos))),
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
                type.changeReturnType(Boolean.TYPE), new int[] { pos });
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   259
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   260
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   261
    private static final MethodHandle IS_OF_CLASS = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   262
            "isOfClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   263
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   264
    private static final MethodHandle IS_INSTANCE = Lookup.PUBLIC.findVirtual(Class.class, "isInstance",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   265
            MethodType.methodType(Boolean.TYPE, Object.class));
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
    private static final MethodHandle IS_ARRAY = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, "isArray",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   268
            MethodType.methodType(Boolean.TYPE, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   269
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   270
    private static final MethodHandle IS_IDENTICAL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   271
            "isIdentical", MethodType.methodType(Boolean.TYPE, Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   272
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   273
    private static final MethodHandle IS_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   274
            "isNull", MethodType.methodType(Boolean.TYPE, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   275
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   276
    private static final MethodHandle IS_NOT_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   277
            "isNotNull", MethodType.methodType(Boolean.TYPE, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   278
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
     * Creates a guard method that tests its only argument for being of an exact particular class.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   281
     * @param clazz the class to test for.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   282
     * @return the desired guard method.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   283
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   284
    public static MethodHandle getClassGuard(Class<?> clazz) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   285
        return IS_OF_CLASS.bindTo(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   286
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   287
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   288
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   289
     * Creates a guard method that tests its only argument for being an instance of a particular class.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   290
     * @param clazz the class to test for.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   291
     * @return the desired guard method.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   292
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   293
    public static MethodHandle getInstanceOfGuard(Class<?> clazz) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   294
        return IS_INSTANCE.bindTo(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   295
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   296
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   297
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   298
     * Creates a guard method that tests its only argument for being referentially identical to another object
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   299
     * @param obj the object used as referential identity test
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   300
     * @return the desired guard method.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   301
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   302
    public static MethodHandle getIdentityGuard(Object obj) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   303
        return IS_IDENTICAL.bindTo(obj);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   304
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   305
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   306
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   307
     * Returns a guard that tests whether the first argument is null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   308
     * @return a guard that tests whether the first argument is null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   309
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   310
    public static MethodHandle isNull() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   311
        return IS_NULL;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   312
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   313
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   314
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   315
     * Returns a guard that tests whether the first argument is not null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   316
     * @return a guard that tests whether the first argument is not null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   317
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   318
    public static MethodHandle isNotNull() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   319
        return IS_NOT_NULL;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   320
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   321
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   322
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   323
    private static boolean isNull(Object obj) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   324
        return obj == null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   325
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   326
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   327
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   328
    private static boolean isNotNull(Object obj) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
        return obj != null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   330
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   331
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   332
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   333
    private static boolean isArray(Object o) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   334
        return o != null && o.getClass().isArray();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   335
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   336
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   337
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   338
    private static boolean isOfClass(Class<?> c, Object o) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   339
        return o != null && o.getClass() == c;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   340
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   341
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   342
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   343
    private static boolean isIdentical(Object o1, Object o2) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   344
        return o1 == o2;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   345
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   346
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   347
    private static MethodHandle constantTrue(MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   348
        return constantBoolean(Boolean.TRUE, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   349
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   350
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   351
    private static MethodHandle constantFalse(MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   352
        return constantBoolean(Boolean.FALSE, type);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   353
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   354
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
    private static MethodHandle constantBoolean(Boolean value, MethodType type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   356
        return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
                type.changeReturnType(Boolean.TYPE));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   359
}