src/jdk.dynalink/share/classes/jdk/dynalink/linker/support/TypeUtilities.java
author jlaskey
Thu, 21 Jun 2018 08:58:59 -0300
changeset 50695 36ca515343e0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8203637: Fix Sources 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
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    36
   Redistribution and use in source and binary forms, with or without
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    37
   modification, are permitted provided that the following conditions are
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    38
   met:
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    39
   * Redistributions of source code must retain the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    40
     notice, this list of conditions and the following disclaimer.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    41
   * Redistributions in binary form must reproduce the above copyright
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    42
     notice, this list of conditions and the following disclaimer in the
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    43
     documentation and/or other materials provided with the distribution.
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    44
   * Neither the name of the copyright holder nor the names of
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    45
     contributors may be used to endorse or promote products derived from
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    46
     this software without specific prior written permission.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
50695
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    48
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    49
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    50
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    51
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    52
   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    53
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    54
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    55
   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    56
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    57
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36ca515343e0 8203637: Fix Sources
jlaskey
parents: 47216
diff changeset
    58
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    61
package jdk.dynalink.linker.support;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    63
import java.util.Collection;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
import java.util.Collections;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
import java.util.HashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    66
import java.util.IdentityHashMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    67
import java.util.Map;
34447
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    68
import jdk.dynalink.DynamicLinkerFactory;
ec4c069f9436 8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents: 33688
diff changeset
    69
import jdk.dynalink.linker.MethodTypeConversionStrategy;
16234
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
/**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
    72
 * Various static utility methods for working with Java types.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    73
 */
33688
649d5d76f602 8142422: Smaller Dynalink API adjustments
attila
parents: 33338
diff changeset
    74
public final class TypeUtilities {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    75
    static final Class<Object> OBJECT_CLASS = Object.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    76
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    77
    private TypeUtilities() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    78
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    79
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    80
    private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    81
    private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    82
    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
    83
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    84
    private static Map<Class<?>, Class<?>> createWrapperTypes() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
        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
    86
        wrapperTypes.put(Void.TYPE, Void.class);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
        wrapperTypes.put(Boolean.TYPE, Boolean.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
        wrapperTypes.put(Byte.TYPE, Byte.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
        wrapperTypes.put(Character.TYPE, Character.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
        wrapperTypes.put(Short.TYPE, Short.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
        wrapperTypes.put(Integer.TYPE, Integer.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
        wrapperTypes.put(Long.TYPE, Long.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
        wrapperTypes.put(Float.TYPE, Float.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
        wrapperTypes.put(Double.TYPE, Double.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    95
        return Collections.unmodifiableMap(wrapperTypes);
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
    98
    private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
        final Map<String, Class<?>> map = new HashMap<>();
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   100
        for(final Class<?> clazz: classes) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
            map.put(clazz.getName(), clazz);
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
        return map;
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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   106
    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
   107
        final Map<V, K> inverted = new IdentityHashMap<>(map.size());
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   108
        for(final Map.Entry<K, V> entry: map.entrySet()) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
            inverted.put(entry.getValue(), entry.getKey());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
        return Collections.unmodifiableMap(inverted);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
     * 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
   116
     * "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
   117
     * {@link #isSubtype(Class, Class)}) as well as boxing conversion (JLS 5.1.7) optionally followed by widening
39792
3ed4aa54785e 8161928: Dynalink documentation updates
attila
parents: 34447
diff changeset
   118
     * reference conversion, and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
     *
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   120
     * @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
   121
     * @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
   122
     * @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
   123
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   124
    public static boolean isMethodInvocationConvertible(final Class<?> sourceType, final Class<?> targetType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   125
        if(targetType.isAssignableFrom(sourceType)) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   126
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   127
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   128
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   129
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   130
                return isProperPrimitiveSubtype(sourceType, targetType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
            }
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   132
            return isBoxingAndWideningReferenceConversion(sourceType, targetType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   133
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   134
        if(targetType.isPrimitive()) {
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   135
            final Class<?> unboxedCallSiteType = getPrimitiveType(sourceType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   136
            return unboxedCallSiteType != null
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   137
                    && (unboxedCallSiteType == targetType || isProperPrimitiveSubtype(unboxedCallSiteType, targetType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   138
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   139
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   140
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   141
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   142
    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
   143
        final Class<?> wrapperType = getWrapperType(sourceType);
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   144
        assert wrapperType != null : sourceType.getName();
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   145
        return targetType.isAssignableFrom(wrapperType);
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   146
    }
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   147
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   148
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   149
     * 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
   150
     * 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
   151
     * 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
   152
     * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}).
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   153
     * 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
   154
     * 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
   155
     * 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
   156
     * 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
   157
     * preserving any precision.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   158
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   159
     * @param sourceType the source type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   160
     * @param targetType the target type
24782
82c9c92e3733 8038413: NPE in unboxInteger
attila
parents: 24778
diff changeset
   161
     * @return true if lossless conversion is possible
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   162
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   163
    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
   164
        if(targetType.isAssignableFrom(sourceType) || targetType == void.class) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   165
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   166
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   167
        if(sourceType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   168
            if(sourceType == void.class) {
28438
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   169
                // 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
   170
                // can be set with DynamicLinkerFactory.setAutoConversionStrategy.
f164fc2618a0 8068573: POJO setter using [] syntax throws an exception
attila
parents: 27360
diff changeset
   171
                return targetType == Object.class;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   172
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   173
            if(targetType.isPrimitive()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   174
                return isProperPrimitiveLosslessSubtype(sourceType, targetType);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   175
            }
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   176
            return isBoxingAndWideningReferenceConversion(sourceType, targetType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   177
        }
24782
82c9c92e3733 8038413: NPE in unboxInteger
attila
parents: 24778
diff changeset
   178
        // 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
   179
        // Also, can't convert non-assignable reference types.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   180
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   181
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   182
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   183
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   184
     * 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
   185
     * 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
   186
     * 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
   187
     * 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
   188
     * (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
   189
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   190
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   191
     * @param superType the supposed supertype of the subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
     * @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
   193
     * reference conversion to superType.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   195
    public static boolean isSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
        // 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
   197
        // 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
   198
        // type identity.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   199
        if(superType.isAssignableFrom(subType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   200
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   201
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
        // 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
   203
        // identity, as identical types were taken care of in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
        // 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
   205
        // follows:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   206
        // double > float
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   207
        // float > long
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
        // long > int
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   209
        // int > short
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   210
        // int > char
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   211
        // short > byte
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
        if(superType.isPrimitive() && subType.isPrimitive()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   213
            return isProperPrimitiveSubtype(subType, superType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   215
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   216
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   217
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   218
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
     * 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
   220
     * supposed primitive supertype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   221
     *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   222
     * @param subType the supposed subtype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   223
     * @param superType the supposed supertype
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
     * @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
   225
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   226
    private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   227
        if(superType == boolean.class || subType == boolean.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   228
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   229
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   230
        if(subType == byte.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   231
            return superType != char.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   232
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   233
        if(subType == char.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   234
            return superType != short.class && superType != byte.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   236
        if(subType == short.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
            return superType != char.class && superType != byte.class;
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
        if(subType == int.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   240
            return superType == long.class || superType == float.class || superType == double.class;
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
        if(subType == long.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   243
            return superType == float.class || superType == double.class;
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
        if(subType == float.class) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   246
            return superType == double.class;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   247
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   248
        return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   249
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   250
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   251
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   252
     * Similar to {@link #isProperPrimitiveSubtype(Class, Class)}, except it disallows conversions from int and long to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   253
     * 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
   254
     * anything else (similar to boolean) as char is not meant to be an arithmetic type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   255
     * @param subType the supposed subtype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   256
     * @param superType the supposed supertype
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   257
     * @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
   258
     * by the supertype without no precision loss.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   259
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   260
    private static boolean isProperPrimitiveLosslessSubtype(final Class<?> subType, final Class<?> superType) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   261
        if(superType == boolean.class || subType == boolean.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   262
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   263
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   264
        if(superType == char.class || subType == char.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   265
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   266
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   267
        if(subType == byte.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   268
            return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   269
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   270
        if(subType == short.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   271
            return superType != byte.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   272
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   273
        if(subType == int.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   274
            return superType == long.class || superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   275
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   276
        if(subType == float.class) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   277
            return superType == double.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   278
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   279
        return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   280
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   281
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   282
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   283
     * 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
   284
     * when invoked with "int", returns {@link Integer#TYPE}.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   285
     * @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
   286
     * @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
   287
     * does not correspond to a primitive type.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   288
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   289
    public static Class<?> getPrimitiveTypeByName(final String name) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   290
        return PRIMITIVE_TYPES_BY_NAME.get(name);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   291
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   292
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   293
    /**
33338
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   294
     * 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
   295
     * 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
   296
     * 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
   297
     * 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
   298
     * @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
   299
     * primitive type.
faf6471e1cc8 8139887: Reduce visibility of few methods in TypeUtilities and Guards API
attila
parents: 33337
diff changeset
   300
     * @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
   301
     * passed class is not a primitive wrapper.
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   302
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   303
    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
   304
        return PRIMITIVE_TYPES.get(wrapperType);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   305
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   306
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   307
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   308
     * 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
   309
     * 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
   310
     * that is not a primitive type, returns null.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   311
     * @param primitiveType the class object representing a primitive type
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   312
     * @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
   313
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   314
    public static Class<?> getWrapperType(final Class<?> primitiveType) {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   315
        return WRAPPER_TYPES.get(primitiveType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   316
    }
27360
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   317
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   318
    /**
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   319
     * 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
   320
     * @param type the examined type
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   321
     * @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
   322
     */
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   323
    public static boolean isWrapperType(final Class<?> type) {
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   324
        return PRIMITIVE_TYPES.containsKey(type);
a19c14022fa4 8059443: NPE when unboxing return values
attila
parents: 25865
diff changeset
   325
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   326
}