nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/linker/support/TypeUtilities.java
author attila
Tue, 20 Oct 2015 23:33:39 +0200
changeset 33338 faf6471e1cc8
parent 33337 af3fea63e008
child 33688 649d5d76f602
permissions -rw-r--r--
8139887: Reduce visibility of few methods in TypeUtilities and Guards API Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     1
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     4
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    10
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    15
 * accompanied this code).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    16
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    20
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    23
 * questions.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    24
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    25
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    26
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    30
 * file, and Oracle licenses the original version of this file under the BSD
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    31
 * license:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    32
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    33
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    34
   Copyright 2009-2013 Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    35
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    36
   Licensed under both the Apache License, Version 2.0 (the "Apache License")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    37
   and the BSD License (the "BSD License"), with licensee being free to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    38
   choose either of the two at their discretion.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    39
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    40
   You may not use this file except in compliance with either the Apache
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    41
   License or the BSD License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    42
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    43
   If you choose to use this file in compliance with the Apache License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    44
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    45
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    46
       You may obtain a copy of the Apache License at
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    48
           http://www.apache.org/licenses/LICENSE-2.0
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    49
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    50
       Unless required by applicable law or agreed to in writing, software
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    51
       distributed under the License is distributed on an "AS IS" BASIS,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    52
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    53
       implied. See the License for the specific language governing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    54
       permissions and limitations under the License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    55
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    56
   If you choose to use this file in compliance with the BSD License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    57
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    58
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
       Redistribution and use in source and binary forms, with or without
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
       modification, are permitted provided that the following conditions are
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    61
       met:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
       * Redistributions of source code must retain the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    63
         notice, this list of conditions and the following disclaimer.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
       * Redistributions in binary form must reproduce the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
         notice, this list of conditions and the following disclaimer in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    66
         documentation and/or other materials provided with the distribution.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    67
       * Neither the name of the copyright holder nor the names of
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    68
         contributors may be used to endorse or promote products derived from
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    69
         this software without specific prior written permission.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    70
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    71
       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    72
       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    73
       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    74
       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    75
       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    76
       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    77
       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    78
       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    79
       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    80
       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    81
       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    82
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    83
33337
af3fea63e008 8139761: Improve Dynalink class nomenclature and package organization
attila
parents: 33007
diff changeset
    84
package jdk.internal.dynalink.linker.support;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    86
import java.util.Collection;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.util.Collections;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.util.HashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
import java.util.IdentityHashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
import java.util.Map;
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
    91
import jdk.internal.dynalink.DynamicLinkerFactory;
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
    92
import jdk.internal.dynalink.linker.MethodTypeConversionStrategy;
16234
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
/**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
    95
 * Various static utility methods for working with Java types.
16234
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
public class TypeUtilities {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    98
    static final Class<Object> OBJECT_CLASS = Object.class;
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
    private TypeUtilities() {
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
    private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   104
    private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   105
    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
   106
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   107
    private static Map<Class<?>, Class<?>> createWrapperTypes() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
        final Map<Class<?>, Class<?>> wrapperTypes = new IdentityHashMap<>(8);
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   109
        wrapperTypes.put(Void.TYPE, Void.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
        wrapperTypes.put(Boolean.TYPE, Boolean.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
        wrapperTypes.put(Byte.TYPE, Byte.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
        wrapperTypes.put(Character.TYPE, Character.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
        wrapperTypes.put(Short.TYPE, Short.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
        wrapperTypes.put(Integer.TYPE, Integer.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
        wrapperTypes.put(Long.TYPE, Long.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   116
        wrapperTypes.put(Float.TYPE, Float.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
        wrapperTypes.put(Double.TYPE, Double.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
        return Collections.unmodifiableMap(wrapperTypes);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   120
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   121
    private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
        final Map<String, Class<?>> map = new HashMap<>();
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   123
        for(final Class<?> clazz: classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
            map.put(clazz.getName(), clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   125
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   126
        return map;
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   129
    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
   130
        final Map<V, K> inverted = new IdentityHashMap<>(map.size());
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   131
        for(final Map.Entry<K, V> entry: map.entrySet()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
            inverted.put(entry.getValue(), entry.getKey());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
        return Collections.unmodifiableMap(inverted);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   135
    }
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
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   138
     * 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
   139
     * "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
   140
     * {@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
   141
     * 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
   142
     *
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   143
     * @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
   144
     * @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
   145
     * @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
   146
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   147
    public static boolean isMethodInvocationConvertible(final Class<?> sourceType, final Class<?> targetType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   148
        if(targetType.isAssignableFrom(sourceType)) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   149
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   150
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   151
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   152
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   153
                return isProperPrimitiveSubtype(sourceType, targetType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   154
            }
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   155
            return isBoxingAndWideningReferenceConversion(sourceType, targetType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   156
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   157
        if(targetType.isPrimitive()) {
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   158
            final Class<?> unboxedCallSiteType = getPrimitiveType(sourceType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   159
            return unboxedCallSiteType != null
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   160
                    && (unboxedCallSiteType == targetType || isProperPrimitiveSubtype(unboxedCallSiteType, targetType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   161
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   162
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   163
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   164
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   165
    private static boolean isBoxingAndWideningReferenceConversion(final Class<?> sourceType, final Class<?> targetType) {
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   166
        final Class<?> wrapperType = getWrapperType(sourceType);
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   167
        assert wrapperType != null : sourceType.getName();
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   168
        return targetType.isAssignableFrom(wrapperType);
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   169
    }
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   170
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   171
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   172
     * Determines whether a type can be converted to another without losing any
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   173
     * precision. As a special case, void is considered convertible only to void
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   174
     * and {@link Object} (either as {@code null} or as a custom value set in
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   175
     * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}).
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   176
     * Somewhat unintuitively, we consider anything to be convertible to void
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   177
     * even though converting to void causes the ultimate loss of data. On the
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   178
     * other hand, conversion to void essentially means that the value is of no
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   179
     * interest and should be discarded, thus there's no expectation of
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   180
     * preserving any precision.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   181
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   182
     * @param sourceType the source type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   183
     * @param targetType the target type
24782
82c9c92e3733 8038413: NPE in unboxInteger
attila
parents: 24778
diff changeset
   184
     * @return true if lossless conversion is possible
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   185
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   186
    public static boolean isConvertibleWithoutLoss(final Class<?> sourceType, final Class<?> targetType) {
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   187
        if(targetType.isAssignableFrom(sourceType) || targetType == void.class) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   188
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   189
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   190
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   191
            if(sourceType == void.class) {
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   192
                // Void should be losslessly representable by Object, either as null or as a custom value that
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   193
                // can be set with DynamicLinkerFactory.setAutoConversionStrategy.
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   194
                return targetType == Object.class;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   195
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   196
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   197
                return isProperPrimitiveLosslessSubtype(sourceType, targetType);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   198
            }
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   199
            return isBoxingAndWideningReferenceConversion(sourceType, targetType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   200
        }
24782
82c9c92e3733 8038413: NPE in unboxInteger
attila
parents: 24778
diff changeset
   201
        // Can't convert from any non-primitive type to any primitive type without data loss because of null.
82c9c92e3733 8038413: NPE in unboxInteger
attila
parents: 24778
diff changeset
   202
        // Also, can't convert non-assignable reference types.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
        return false;
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
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   207
     * Determines whether one type is a subtype of another type, as per JLS
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   208
     * 4.10 "Subtyping". Note: this is not strict or proper subtype, therefore
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   209
     * true is also returned for identical types; to be completely precise, it
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   210
     * allows identity conversion (JLS 5.1.1), widening primitive conversion
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   211
     * (JLS 5.1.2) and widening reference conversion (JLS 5.1.5).
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   213
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
     * @param superType the supposed supertype of the subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   215
     * @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
   216
     * reference conversion to superType.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   217
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   218
    public static boolean isSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
        // 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
   220
        // 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
   221
        // type identity.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   222
        if(superType.isAssignableFrom(subType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   223
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   225
        // 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
   226
        // identity, as identical types were taken care of in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   227
        // 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
   228
        // follows:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   229
        // double > float
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   230
        // float > long
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   231
        // long > int
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   232
        // int > short
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   233
        // int > char
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   234
        // short > byte
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
        if(superType.isPrimitive() && subType.isPrimitive()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   236
            return isProperPrimitiveSubtype(subType, superType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   238
        return false;
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
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   242
     * 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
   243
     * supposed primitive supertype
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
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   246
     * @param superType the supposed supertype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   247
     * @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
   248
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   249
    private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   250
        if(superType == boolean.class || subType == boolean.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   251
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   252
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   253
        if(subType == byte.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   254
            return superType != char.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   255
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   256
        if(subType == char.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   257
            return superType != short.class && superType != byte.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   259
        if(subType == short.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   260
            return superType != char.class && superType != byte.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   261
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   262
        if(subType == int.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   263
            return superType == long.class || superType == float.class || superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   264
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   265
        if(subType == long.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   266
            return superType == float.class || superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   267
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   268
        if(subType == float.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   269
            return superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   270
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   271
        return false;
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
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   274
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   275
     * Similar to {@link #isProperPrimitiveSubtype(Class, Class)}, except it disallows conversions from int and long to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   276
     * 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
   277
     * anything else (similar to boolean) as char is not meant to be an arithmetic type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   278
     * @param subType the supposed subtype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   279
     * @param superType the supposed supertype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   280
     * @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
   281
     * by the supertype without no precision loss.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   282
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   283
    private static boolean isProperPrimitiveLosslessSubtype(final Class<?> subType, final Class<?> superType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   284
        if(superType == boolean.class || subType == boolean.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   285
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   286
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   287
        if(superType == char.class || subType == char.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   288
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   289
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   290
        if(subType == byte.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   291
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   292
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   293
        if(subType == short.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   294
            return superType != byte.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   295
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   296
        if(subType == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   297
            return superType == long.class || superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   298
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   299
        if(subType == float.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   300
            return superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   301
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   302
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   303
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   304
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   305
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   306
     * Given a name of a primitive type returns the class representing it. I.e.
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   307
     * when invoked with "int", returns {@link Integer#TYPE}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   308
     * @param name the name of the primitive type
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   309
     * @return the class representing the primitive type, or null if the name
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   310
     * does not correspond to a primitive type.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   311
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   312
    public static Class<?> getPrimitiveTypeByName(final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   313
        return PRIMITIVE_TYPES_BY_NAME.get(name);
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
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   316
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   317
     * When passed a class representing a wrapper for a primitive type, returns
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   318
     * the class representing the corresponding primitive type. I.e. calling it
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   319
     * with {@code Integer.class} will return {@code Integer.TYPE}. If passed a
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   320
     * class that is not a wrapper for primitive type, returns null.
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   321
     * @param wrapperType the class object representing a wrapper for a
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   322
     * primitive type.
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   323
     * @return the class object representing the primitive type, or null if the
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   324
     * passed class is not a primitive wrapper.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   325
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   326
    public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   327
        return PRIMITIVE_TYPES.get(wrapperType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   328
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
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
     * 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
   332
     * 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
   333
     * that is not a primitive type, returns null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   334
     * @param primitiveType the class object representing a primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   335
     * @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
   336
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   337
    public static Class<?> getWrapperType(final Class<?> primitiveType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   338
        return WRAPPER_TYPES.get(primitiveType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   339
    }
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   340
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   341
    /**
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   342
     * Returns true if the passed type is a wrapper for a primitive type.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   343
     * @param type the examined type
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   344
     * @return true if the passed type is a wrapper for a primitive type.
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   345
     */
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   346
    public static boolean isWrapperType(final Class<?> type) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   347
        return PRIMITIVE_TYPES.containsKey(type);
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   348
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   349
}