nashorn/src/jdk/internal/dynalink/support/TypeUtilities.java
author attila
Wed, 04 Jun 2014 13:08:57 +0200
changeset 24778 2ff5d7041566
parent 24751 ccbd9cd3f720
child 24782 82c9c92e3733
permissions -rw-r--r--
8044638: Tidy up Nashorn codebase for code standards Reviewed-by: lagergren, 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.util.ArrayList;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.util.Collection;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.util.Collections;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
import java.util.HashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
import java.util.HashSet;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
import java.util.IdentityHashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
import java.util.Iterator;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
import java.util.List;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
import java.util.Map;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    95
import java.util.Set;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    96
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
 * Various static utility methods for testing type relationships.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   100
 * @author Attila Szegedi
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
public class TypeUtilities {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
    static final Class<Object> OBJECT_CLASS = Object.class;
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
    private TypeUtilities() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   107
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   109
     * Given two types represented by c1 and c2, returns a type that is their most specific common supertype for
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   110
     * purposes of lossless conversions.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
     * @param c1 one type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
     * @param c2 another type
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   114
     * @return their most common superclass or superinterface for purposes of lossless conversions. If they have several
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   115
     * unrelated superinterfaces as their most specific common type, or the types themselves are completely
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   116
     * unrelated interfaces, {@link java.lang.Object} is returned.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   118
    public static Class<?> getCommonLosslessConversionType(final Class<?> c1, final Class<?> c2) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
        if(c1 == c2) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   120
            return c1;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   121
        } else if(isConvertibleWithoutLoss(c2, c1)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   122
            return c1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   123
        } else if(isConvertibleWithoutLoss(c1, c2)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   124
            return c2;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   125
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   126
        if(c1 == void.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   127
            return c2;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   128
        } else if(c2 == void.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   129
            return c1;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   130
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   131
        if(c1.isPrimitive() && c2.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   132
            if((c1 == byte.class && c2 == char.class) || (c1 == char.class && c2 == byte.class)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   133
                // byte + char = int
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   134
                return int.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   135
            } else if((c1 == short.class && c2 == char.class) || (c1 == char.class && c2 == short.class)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   136
                // short + char = int
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   137
                return int.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   138
            } else if((c1 == int.class && c2 == float.class) || (c1 == float.class && c2 == int.class)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   139
                // int + float = double
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   140
                return double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   141
            }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   142
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   143
        // For all other cases. This will handle long + (float|double) = Number case as well as boolean + anything = Object case too.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   144
        return getMostSpecificCommonTypeUnequalNonprimitives(c1, c2);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   145
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   146
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   147
    private static Class<?> getMostSpecificCommonTypeUnequalNonprimitives(final Class<?> c1, final Class<?> c2) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   148
        final Class<?> npc1 = c1.isPrimitive() ? getWrapperType(c1) : c1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   149
        final Class<?> npc2 = c2.isPrimitive() ? getWrapperType(c2) : c2;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   150
        final Set<Class<?>> a1 = getAssignables(npc1, npc2);
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   151
        final Set<Class<?>> a2 = getAssignables(npc2, npc1);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   152
        a1.retainAll(a2);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   153
        if(a1.isEmpty()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   154
            // Can happen when at least one of the arguments is an interface,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   155
            // as they don't have Object at the root of their hierarchy.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   156
            return Object.class;
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
        // Gather maximally specific elements. Yes, there can be more than one
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   159
        // thank to interfaces. I.e., if you call this method for String.class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   160
        // and Number.class, you'll have Comparable, Serializable, and Object
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   161
        // as maximal elements.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   162
        final List<Class<?>> max = new ArrayList<>();
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   163
        outer: for(final Class<?> clazz: a1) {
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   164
            for(final Iterator<Class<?>> maxiter = max.iterator(); maxiter.hasNext();) {
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   165
                final Class<?> maxClazz = maxiter.next();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   166
                if(isSubtype(maxClazz, clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   167
                    // It can't be maximal, if there's already a more specific
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   168
                    // maximal than it.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   169
                    continue outer;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   170
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   171
                if(isSubtype(clazz, maxClazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   172
                    // If it's more specific than a currently maximal element,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
                    // that currently maximal is no longer a maximal.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   174
                    maxiter.remove();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   175
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   176
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   177
            // If we get here, no current maximal is more specific than the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   178
            // current class, so it is considered maximal as well
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   179
            max.add(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   180
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   181
        if(max.size() > 1) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   182
            return Object.class;
16234
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 max.get(0);
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   187
    private static Set<Class<?>> getAssignables(final Class<?> c1, final Class<?> c2) {
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   188
        final Set<Class<?>> s = new HashSet<>();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   189
        collectAssignables(c1, c2, s);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   190
        return s;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   191
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   193
    private static void collectAssignables(final Class<?> c1, final Class<?> c2, final Set<Class<?>> s) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
        if(c1.isAssignableFrom(c2)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   195
            s.add(c1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
        }
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   197
        final Class<?> sc = c1.getSuperclass();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   198
        if(sc != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   199
            collectAssignables(sc, c2, s);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   200
        }
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   201
        final Class<?>[] itf = c1.getInterfaces();
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
        for(int i = 0; i < itf.length; ++i) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
            collectAssignables(itf[i], c2, s);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
    }
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
    private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
    private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   209
    private static final Map<String, Class<?>> PRIMITIVE_TYPES_BY_NAME = createClassNameMapping(WRAPPER_TYPES.keySet());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   210
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   211
    private static Map<Class<?>, Class<?>> createWrapperTypes() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
        final Map<Class<?>, Class<?>> wrapperTypes = new IdentityHashMap<>(8);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   213
        wrapperTypes.put(Boolean.TYPE, Boolean.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
        wrapperTypes.put(Byte.TYPE, Byte.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   215
        wrapperTypes.put(Character.TYPE, Character.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   216
        wrapperTypes.put(Short.TYPE, Short.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   217
        wrapperTypes.put(Integer.TYPE, Integer.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   218
        wrapperTypes.put(Long.TYPE, Long.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
        wrapperTypes.put(Float.TYPE, Float.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   220
        wrapperTypes.put(Double.TYPE, Double.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   221
        return Collections.unmodifiableMap(wrapperTypes);
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   224
    private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   225
        final Map<String, Class<?>> map = new HashMap<>();
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   226
        for(final Class<?> clazz: classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   227
            map.put(clazz.getName(), clazz);
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
        return map;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   230
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   231
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   232
    private static <K, V> Map<V, K> invertMap(final Map<K, V> map) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   233
        final Map<V, K> inverted = new IdentityHashMap<>(map.size());
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   234
        for(final Map.Entry<K, V> entry: map.entrySet()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
            inverted.put(entry.getValue(), entry.getKey());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   236
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
        return Collections.unmodifiableMap(inverted);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   238
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   239
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   240
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   241
     * Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   242
     * "Method Invocation Conversion". This is basically all conversions allowed by subtyping (see
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   243
     * {@link #isSubtype(Class, Class)}) as well as boxing conversion (JLS 5.1.7) optionally followed by widening
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   244
     * reference conversion and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   245
     *
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   246
     * @param sourceType the type being converted from (call site type for parameter types, method type for return types)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   247
     * @param targetType the parameter type being converted to (method type for parameter types, call site type for return types)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   248
     * @return true if source type is method invocation convertible to target type.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   249
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   250
    public static boolean isMethodInvocationConvertible(final Class<?> sourceType, final Class<?> targetType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   251
        if(targetType.isAssignableFrom(sourceType)) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   252
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   253
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   254
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   255
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   256
                return isProperPrimitiveSubtype(sourceType, targetType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   257
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
            // Boxing + widening reference conversion
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   259
            assert WRAPPER_TYPES.get(sourceType) != null : sourceType.getName();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   260
            return targetType.isAssignableFrom(WRAPPER_TYPES.get(sourceType));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   261
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   262
        if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   263
            final Class<?> unboxedCallSiteType = PRIMITIVE_TYPES.get(sourceType);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   264
            return unboxedCallSiteType != null
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   265
                    && (unboxedCallSiteType == targetType || isProperPrimitiveSubtype(unboxedCallSiteType, targetType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   266
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   267
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   268
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   269
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   270
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   271
     * Determines whether a type can be converted to another without losing any
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   272
     * precision.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   273
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   274
     * @param sourceType the source type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   275
     * @param targetType the target type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   276
     * @return true if lossess conversion is possible
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   277
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   278
    public static boolean isConvertibleWithoutLoss(final Class<?> sourceType, final Class<?> targetType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   279
        if(targetType.isAssignableFrom(sourceType)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   280
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   281
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   282
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   283
            if(sourceType == void.class) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24719
diff changeset
   284
                return false; // Void can't be losslessly represented by any type
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   285
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   286
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   287
                return isProperPrimitiveLosslessSubtype(sourceType, targetType);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   288
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   289
            // Boxing + widening reference conversion
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   290
            assert WRAPPER_TYPES.get(sourceType) != null : sourceType.getName();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   291
            return targetType.isAssignableFrom(WRAPPER_TYPES.get(sourceType));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   292
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   293
        if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   294
            if(targetType == void.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   295
                return false; // Void can't represent anything losslessly
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   296
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   297
            final Class<?> unboxedCallSiteType = PRIMITIVE_TYPES.get(sourceType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   298
            return unboxedCallSiteType != null
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   299
                    && (unboxedCallSiteType == targetType || isProperPrimitiveLosslessSubtype(unboxedCallSiteType, targetType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   300
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   301
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   302
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   303
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
     * Determines whether one type can be potentially converted to another type at runtime. Allows a conversion between
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   306
     * any subtype and supertype in either direction, and also allows a conversion between any two primitive types, as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   307
     * well as between any primitive type and any reference type that can hold a boxed primitive.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   308
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   309
     * @param callSiteType the parameter type at the call site
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   310
     * @param methodType the parameter type in the method declaration
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   311
     * @return true if callSiteType is potentially convertible to the methodType.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   312
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   313
    public static boolean isPotentiallyConvertible(final Class<?> callSiteType, final Class<?> methodType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   314
        // Widening or narrowing reference conversion
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   315
        if(areAssignable(callSiteType, methodType)) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   316
            return true;
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
        if(callSiteType.isPrimitive()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   319
            // Allow any conversion among primitives, as well as from any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   320
            // primitive to any type that can receive a boxed primitive.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   321
            // TODO: narrow this a bit, i.e. allow, say, boolean to Character?
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   322
            // MethodHandles.convertArguments() allows it, so we might need to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   323
            // too.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   324
            return methodType.isPrimitive() || isAssignableFromBoxedPrimitive(methodType);
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
        if(methodType.isPrimitive()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   327
            // Allow conversion from any reference type that can contain a
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   328
            // boxed primitive to any primitive.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
            // TODO: narrow this a bit too?
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   330
            return isAssignableFromBoxedPrimitive(callSiteType);
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
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   333
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   334
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   335
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   336
     * Returns true if either of the types is assignable from the other.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   337
     * @param c1 one of the types
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   338
     * @param c2 another one of the types
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   339
     * @return true if either c1 is assignable from c2 or c2 is assignable from c1.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   340
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   341
    public static boolean areAssignable(final Class<?> c1, final Class<?> c2) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   342
        return c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   343
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   344
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   345
    /**
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   346
     * Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping". Note: this is not strict
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   347
     * or proper subtype, therefore true is also returned for identical types; to be completely precise, it allows
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   348
     * identity conversion (JLS 5.1.1), widening primitive conversion (JLS 5.1.2) and widening reference conversion (JLS
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   349
     * 5.1.5).
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
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   352
     * @param superType the supposed supertype of the subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   353
     * @return true if subType can be converted by identity conversion, widening primitive conversion, or widening
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   354
     * reference conversion to superType.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   356
    public static boolean isSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
        // Covers both JLS 4.10.2 "Subtyping among Class and Interface Types"
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
        // and JLS 4.10.3 "Subtyping among Array Types", as well as primitive
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   359
        // type identity.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   360
        if(superType.isAssignableFrom(subType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   361
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   362
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   363
        // JLS 4.10.1 "Subtyping among Primitive Types". Note we don't test for
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   364
        // identity, as identical types were taken care of in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   365
        // isAssignableFrom test. As per 4.10.1, the supertype relation is as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   366
        // follows:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   367
        // double > float
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   368
        // float > long
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   369
        // long > int
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   370
        // int > short
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   371
        // int > char
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   372
        // short > byte
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   373
        if(superType.isPrimitive() && subType.isPrimitive()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   374
            return isProperPrimitiveSubtype(subType, superType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   375
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   376
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   377
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   378
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   379
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   380
     * Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   381
     * supposed primitive supertype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   382
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   383
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   384
     * @param superType the supposed supertype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   385
     * @return true if subType is a proper (not identical to) primitive subtype of the superType
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   386
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   387
    private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   388
        if(superType == boolean.class || subType == boolean.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   389
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   390
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   391
        if(subType == byte.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   392
            return superType != char.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   393
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   394
        if(subType == char.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   395
            return superType != short.class && superType != byte.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   396
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   397
        if(subType == short.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
            return superType != char.class && superType != byte.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   399
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   400
        if(subType == int.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   401
            return superType == long.class || superType == float.class || superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   402
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   403
        if(subType == long.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   404
            return superType == float.class || superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
        if(subType == float.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   407
            return superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   408
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   409
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   410
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   411
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   412
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   413
     * Similar to {@link #isProperPrimitiveSubtype(Class, Class)}, except it disallows conversions from int and long to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   414
     * float, and from long to double, as those can lose precision. It also disallows conversion from and to char and
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   415
     * anything else (similar to boolean) as char is not meant to be an arithmetic type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   416
     * @param subType the supposed subtype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   417
     * @param superType the supposed supertype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   418
     * @return true if subType is a proper (not identical to) primitive subtype of the superType that can be represented
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   419
     * by the supertype without no precision loss.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   420
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   421
    private static boolean isProperPrimitiveLosslessSubtype(final Class<?> subType, final Class<?> superType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   422
        if(superType == boolean.class || subType == boolean.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   423
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   424
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   425
        if(superType == char.class || subType == char.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   426
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   427
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   428
        if(subType == byte.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   429
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   430
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   431
        if(subType == short.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   432
            return superType != byte.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   433
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   434
        if(subType == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   435
            return superType == long.class || superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   436
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   437
        if(subType == float.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   438
            return superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   439
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   440
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   441
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   442
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   443
    private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPES = createWrapperToPrimitiveTypes();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   444
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   445
    private static Map<Class<?>, Class<?>> createWrapperToPrimitiveTypes() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   446
        final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   447
        classes.put(Void.class, Void.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   448
        classes.put(Boolean.class, Boolean.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   449
        classes.put(Byte.class, Byte.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   450
        classes.put(Character.class, Character.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   451
        classes.put(Short.class, Short.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   452
        classes.put(Integer.class, Integer.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   453
        classes.put(Long.class, Long.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   454
        classes.put(Float.class, Float.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   455
        classes.put(Double.class, Double.TYPE);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   456
        return classes;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   457
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   458
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   459
    private static final Set<Class<?>> PRIMITIVE_WRAPPER_TYPES = createPrimitiveWrapperTypes();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   460
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   461
    private static Set<Class<?>> createPrimitiveWrapperTypes() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   462
        final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   463
        addClassHierarchy(classes, Boolean.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   464
        addClassHierarchy(classes, Byte.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   465
        addClassHierarchy(classes, Character.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   466
        addClassHierarchy(classes, Short.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   467
        addClassHierarchy(classes, Integer.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   468
        addClassHierarchy(classes, Long.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   469
        addClassHierarchy(classes, Float.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   470
        addClassHierarchy(classes, Double.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   471
        return classes.keySet();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   472
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   473
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   474
    private static void addClassHierarchy(final Map<Class<?>, Class<?>> map, final Class<?> clazz) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   475
        if(clazz == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   476
            return;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   477
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   478
        map.put(clazz, clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   479
        addClassHierarchy(map, clazz.getSuperclass());
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   480
        for(final Class<?> itf: clazz.getInterfaces()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   481
            addClassHierarchy(map, itf);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   482
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   483
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   484
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   485
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   486
     * Returns true if the class can be assigned from any boxed primitive.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   487
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   488
     * @param clazz the class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   489
     * @return true if the class can be assigned from any boxed primitive. Basically, it is true if the class is any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   490
     * primitive wrapper class, or a superclass or superinterface of any primitive wrapper class.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   491
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   492
    private static boolean isAssignableFromBoxedPrimitive(final Class<?> clazz) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   493
        return PRIMITIVE_WRAPPER_TYPES.contains(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   494
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   495
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   496
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   497
     * Given a name of a primitive type (except "void"), returns the class representing it. I.e. when invoked with
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   498
     * "int", returns {@link Integer#TYPE}.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   499
     * @param name the name of the primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   500
     * @return the class representing the primitive type, or null if the name does not correspond to a primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   501
     * or is "void".
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   502
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   503
    public static Class<?> getPrimitiveTypeByName(final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   504
        return PRIMITIVE_TYPES_BY_NAME.get(name);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   505
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   506
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   507
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   508
     * When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   509
     * primitive type. I.e. calling it with {@code Integer.class} will return {@code Integer.TYPE}. If passed a class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   510
     * that is not a wrapper for primitive type, returns null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   511
     * @param wrapperType the class object representing a wrapper for a primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   512
     * @return the class object representing the primitive type, or null if the passed class is not a primitive wrapper.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   513
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   514
    public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   515
        return WRAPPER_TO_PRIMITIVE_TYPES.get(wrapperType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   516
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   517
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   518
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   519
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   520
     * When passed a class representing a primitive type, returns the class representing the corresponding
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   521
     * wrapper type. I.e. calling it with {@code int.class} will return {@code Integer.class}. If passed a class
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   522
     * that is not a primitive type, returns null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   523
     * @param primitiveType the class object representing a primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   524
     * @return the class object representing the wrapper type, or null if the passed class is not a primitive.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   525
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   526
    public static Class<?> getWrapperType(final Class<?> primitiveType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   527
        return WRAPPER_TYPES.get(primitiveType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   528
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   529
}