src/jdk/internal/dynalink/support/TypeUtilities.java

Mon, 03 Nov 2014 09:49:52 +0100

author
attila
date
Mon, 03 Nov 2014 09:49:52 +0100
changeset 1090
99571b7922c0
parent 963
e2497b11a021
child 1183
6ed91931b5a7
permissions
-rw-r--r--

8059443: NPE when unboxing return values
Reviewed-by: lagergren, sundar

attila@90 1 /*
attila@90 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
attila@90 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila@90 4 *
attila@90 5 * This code is free software; you can redistribute it and/or modify it
attila@90 6 * under the terms of the GNU General Public License version 2 only, as
attila@90 7 * published by the Free Software Foundation. Oracle designates this
attila@90 8 * particular file as subject to the "Classpath" exception as provided
attila@90 9 * by Oracle in the LICENSE file that accompanied this code.
attila@90 10 *
attila@90 11 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@90 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@90 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@90 14 * version 2 for more details (a copy is included in the LICENSE file that
attila@90 15 * accompanied this code).
attila@90 16 *
attila@90 17 * You should have received a copy of the GNU General Public License version
attila@90 18 * 2 along with this work; if not, write to the Free Software Foundation,
attila@90 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila@90 20 *
attila@90 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@90 22 * or visit www.oracle.com if you need additional information or have any
attila@90 23 * questions.
attila@90 24 */
attila@90 25
attila@90 26 /*
attila@90 27 * This file is available under and governed by the GNU General Public
attila@90 28 * License version 2 only, as published by the Free Software Foundation.
attila@90 29 * However, the following notice accompanied the original version of this
attila@90 30 * file, and Oracle licenses the original version of this file under the BSD
attila@90 31 * license:
attila@90 32 */
attila@90 33 /*
attila@90 34 Copyright 2009-2013 Attila Szegedi
attila@90 35
attila@90 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
attila@90 37 and the BSD License (the "BSD License"), with licensee being free to
attila@90 38 choose either of the two at their discretion.
attila@90 39
attila@90 40 You may not use this file except in compliance with either the Apache
attila@90 41 License or the BSD License.
attila@90 42
attila@90 43 If you choose to use this file in compliance with the Apache License, the
attila@90 44 following notice applies to you:
attila@90 45
attila@90 46 You may obtain a copy of the Apache License at
attila@90 47
attila@90 48 http://www.apache.org/licenses/LICENSE-2.0
attila@90 49
attila@90 50 Unless required by applicable law or agreed to in writing, software
attila@90 51 distributed under the License is distributed on an "AS IS" BASIS,
attila@90 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
attila@90 53 implied. See the License for the specific language governing
attila@90 54 permissions and limitations under the License.
attila@90 55
attila@90 56 If you choose to use this file in compliance with the BSD License, the
attila@90 57 following notice applies to you:
attila@90 58
attila@90 59 Redistribution and use in source and binary forms, with or without
attila@90 60 modification, are permitted provided that the following conditions are
attila@90 61 met:
attila@90 62 * Redistributions of source code must retain the above copyright
attila@90 63 notice, this list of conditions and the following disclaimer.
attila@90 64 * Redistributions in binary form must reproduce the above copyright
attila@90 65 notice, this list of conditions and the following disclaimer in the
attila@90 66 documentation and/or other materials provided with the distribution.
attila@90 67 * Neither the name of the copyright holder nor the names of
attila@90 68 contributors may be used to endorse or promote products derived from
attila@90 69 this software without specific prior written permission.
attila@90 70
attila@90 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
attila@90 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
attila@90 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
attila@90 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
attila@90 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
attila@90 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
attila@90 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
attila@90 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
attila@90 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
attila@90 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
attila@90 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
attila@90 82 */
attila@90 83
attila@90 84 package jdk.internal.dynalink.support;
attila@90 85
attila@90 86 import java.util.ArrayList;
attila@90 87 import java.util.Collection;
attila@90 88 import java.util.Collections;
attila@90 89 import java.util.HashMap;
attila@90 90 import java.util.HashSet;
attila@90 91 import java.util.IdentityHashMap;
attila@90 92 import java.util.Iterator;
attila@90 93 import java.util.List;
attila@90 94 import java.util.Map;
attila@90 95 import java.util.Set;
attila@90 96
attila@90 97 /**
attila@90 98 * Various static utility methods for testing type relationships.
attila@90 99 *
attila@90 100 * @author Attila Szegedi
attila@90 101 */
attila@90 102 public class TypeUtilities {
attila@90 103 static final Class<Object> OBJECT_CLASS = Object.class;
attila@90 104
attila@90 105 private TypeUtilities() {
attila@90 106 }
attila@90 107
attila@90 108 /**
attila@963 109 * Given two types represented by c1 and c2, returns a type that is their most specific common supertype for
attila@963 110 * purposes of lossless conversions.
attila@90 111 *
attila@90 112 * @param c1 one type
attila@90 113 * @param c2 another type
attila@963 114 * @return their most common superclass or superinterface for purposes of lossless conversions. If they have several
attila@963 115 * unrelated superinterfaces as their most specific common type, or the types themselves are completely
attila@963 116 * unrelated interfaces, {@link java.lang.Object} is returned.
attila@90 117 */
attila@963 118 public static Class<?> getCommonLosslessConversionType(final Class<?> c1, final Class<?> c2) {
attila@90 119 if(c1 == c2) {
attila@90 120 return c1;
attila@963 121 } else if(isConvertibleWithoutLoss(c2, c1)) {
attila@963 122 return c1;
attila@963 123 } else if(isConvertibleWithoutLoss(c1, c2)) {
attila@963 124 return c2;
attila@90 125 }
attila@963 126 if(c1 == void.class) {
attila@963 127 return c2;
attila@963 128 } else if(c2 == void.class) {
attila@963 129 return c1;
attila@90 130 }
attila@963 131 if(c1.isPrimitive() && c2.isPrimitive()) {
attila@963 132 if((c1 == byte.class && c2 == char.class) || (c1 == char.class && c2 == byte.class)) {
attila@963 133 // byte + char = int
attila@963 134 return int.class;
attila@963 135 } else if((c1 == short.class && c2 == char.class) || (c1 == char.class && c2 == short.class)) {
attila@963 136 // short + char = int
attila@963 137 return int.class;
attila@963 138 } else if((c1 == int.class && c2 == float.class) || (c1 == float.class && c2 == int.class)) {
attila@963 139 // int + float = double
attila@963 140 return double.class;
attila@963 141 }
attila@963 142 }
attila@963 143 // For all other cases. This will handle long + (float|double) = Number case as well as boolean + anything = Object case too.
attila@963 144 return getMostSpecificCommonTypeUnequalNonprimitives(c1, c2);
attila@963 145 }
attila@963 146
attila@963 147 private static Class<?> getMostSpecificCommonTypeUnequalNonprimitives(final Class<?> c1, final Class<?> c2) {
attila@963 148 final Class<?> npc1 = c1.isPrimitive() ? getWrapperType(c1) : c1;
attila@963 149 final Class<?> npc2 = c2.isPrimitive() ? getWrapperType(c2) : c2;
attila@963 150 final Set<Class<?>> a1 = getAssignables(npc1, npc2);
attila@963 151 final Set<Class<?>> a2 = getAssignables(npc2, npc1);
attila@90 152 a1.retainAll(a2);
attila@90 153 if(a1.isEmpty()) {
attila@90 154 // Can happen when at least one of the arguments is an interface,
attila@90 155 // as they don't have Object at the root of their hierarchy.
attila@90 156 return Object.class;
attila@90 157 }
attila@90 158 // Gather maximally specific elements. Yes, there can be more than one
attila@90 159 // thank to interfaces. I.e., if you call this method for String.class
attila@90 160 // and Number.class, you'll have Comparable, Serializable, and Object
attila@90 161 // as maximal elements.
attila@962 162 final List<Class<?>> max = new ArrayList<>();
attila@962 163 outer: for(final Class<?> clazz: a1) {
attila@962 164 for(final Iterator<Class<?>> maxiter = max.iterator(); maxiter.hasNext();) {
attila@962 165 final Class<?> maxClazz = maxiter.next();
attila@90 166 if(isSubtype(maxClazz, clazz)) {
attila@90 167 // It can't be maximal, if there's already a more specific
attila@90 168 // maximal than it.
attila@90 169 continue outer;
attila@90 170 }
attila@90 171 if(isSubtype(clazz, maxClazz)) {
attila@90 172 // If it's more specific than a currently maximal element,
attila@90 173 // that currently maximal is no longer a maximal.
attila@90 174 maxiter.remove();
attila@90 175 }
attila@90 176 }
attila@90 177 // If we get here, no current maximal is more specific than the
attila@90 178 // current class, so it is considered maximal as well
attila@90 179 max.add(clazz);
attila@90 180 }
attila@90 181 if(max.size() > 1) {
attila@963 182 return Object.class;
attila@90 183 }
attila@90 184 return max.get(0);
attila@90 185 }
attila@90 186
attila@962 187 private static Set<Class<?>> getAssignables(final Class<?> c1, final Class<?> c2) {
attila@962 188 final Set<Class<?>> s = new HashSet<>();
attila@90 189 collectAssignables(c1, c2, s);
attila@90 190 return s;
attila@90 191 }
attila@90 192
attila@962 193 private static void collectAssignables(final Class<?> c1, final Class<?> c2, final Set<Class<?>> s) {
attila@90 194 if(c1.isAssignableFrom(c2)) {
attila@90 195 s.add(c1);
attila@90 196 }
attila@962 197 final Class<?> sc = c1.getSuperclass();
attila@90 198 if(sc != null) {
attila@90 199 collectAssignables(sc, c2, s);
attila@90 200 }
attila@962 201 final Class<?>[] itf = c1.getInterfaces();
attila@90 202 for(int i = 0; i < itf.length; ++i) {
attila@90 203 collectAssignables(itf[i], c2, s);
attila@90 204 }
attila@90 205 }
attila@90 206
attila@90 207 private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes();
attila@90 208 private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES);
attila@90 209 private static final Map<String, Class<?>> PRIMITIVE_TYPES_BY_NAME = createClassNameMapping(WRAPPER_TYPES.keySet());
attila@90 210
attila@90 211 private static Map<Class<?>, Class<?>> createWrapperTypes() {
attila@90 212 final Map<Class<?>, Class<?>> wrapperTypes = new IdentityHashMap<>(8);
attila@90 213 wrapperTypes.put(Boolean.TYPE, Boolean.class);
attila@90 214 wrapperTypes.put(Byte.TYPE, Byte.class);
attila@90 215 wrapperTypes.put(Character.TYPE, Character.class);
attila@90 216 wrapperTypes.put(Short.TYPE, Short.class);
attila@90 217 wrapperTypes.put(Integer.TYPE, Integer.class);
attila@90 218 wrapperTypes.put(Long.TYPE, Long.class);
attila@90 219 wrapperTypes.put(Float.TYPE, Float.class);
attila@90 220 wrapperTypes.put(Double.TYPE, Double.class);
attila@90 221 return Collections.unmodifiableMap(wrapperTypes);
attila@90 222 }
attila@90 223
attila@962 224 private static Map<String, Class<?>> createClassNameMapping(final Collection<Class<?>> classes) {
attila@90 225 final Map<String, Class<?>> map = new HashMap<>();
attila@962 226 for(final Class<?> clazz: classes) {
attila@90 227 map.put(clazz.getName(), clazz);
attila@90 228 }
attila@90 229 return map;
attila@90 230 }
attila@90 231
attila@962 232 private static <K, V> Map<V, K> invertMap(final Map<K, V> map) {
attila@90 233 final Map<V, K> inverted = new IdentityHashMap<>(map.size());
attila@962 234 for(final Map.Entry<K, V> entry: map.entrySet()) {
attila@90 235 inverted.put(entry.getValue(), entry.getKey());
attila@90 236 }
attila@90 237 return Collections.unmodifiableMap(inverted);
attila@90 238 }
attila@90 239
attila@90 240 /**
attila@90 241 * Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3
attila@90 242 * "Method Invocation Conversion". This is basically all conversions allowed by subtyping (see
attila@90 243 * {@link #isSubtype(Class, Class)}) as well as boxing conversion (JLS 5.1.7) optionally followed by widening
attila@90 244 * reference conversion and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion.
attila@90 245 *
attila@963 246 * @param sourceType the type being converted from (call site type for parameter types, method type for return types)
attila@963 247 * @param targetType the parameter type being converted to (method type for parameter types, call site type for return types)
attila@963 248 * @return true if source type is method invocation convertible to target type.
attila@90 249 */
attila@963 250 public static boolean isMethodInvocationConvertible(final Class<?> sourceType, final Class<?> targetType) {
attila@963 251 if(targetType.isAssignableFrom(sourceType)) {
attila@90 252 return true;
attila@90 253 }
attila@963 254 if(sourceType.isPrimitive()) {
attila@963 255 if(targetType.isPrimitive()) {
attila@963 256 return isProperPrimitiveSubtype(sourceType, targetType);
attila@90 257 }
attila@90 258 // Boxing + widening reference conversion
attila@963 259 assert WRAPPER_TYPES.get(sourceType) != null : sourceType.getName();
attila@963 260 return targetType.isAssignableFrom(WRAPPER_TYPES.get(sourceType));
attila@90 261 }
attila@963 262 if(targetType.isPrimitive()) {
attila@963 263 final Class<?> unboxedCallSiteType = PRIMITIVE_TYPES.get(sourceType);
attila@90 264 return unboxedCallSiteType != null
attila@963 265 && (unboxedCallSiteType == targetType || isProperPrimitiveSubtype(unboxedCallSiteType, targetType));
attila@90 266 }
attila@90 267 return false;
attila@90 268 }
attila@90 269
attila@90 270 /**
attila@963 271 * Determines whether a type can be converted to another without losing any
attila@963 272 * precision.
attila@963 273 *
attila@963 274 * @param sourceType the source type
attila@963 275 * @param targetType the target type
attila@963 276 * @return true if lossless conversion is possible
attila@963 277 */
attila@963 278 public static boolean isConvertibleWithoutLoss(final Class<?> sourceType, final Class<?> targetType) {
attila@963 279 if(targetType.isAssignableFrom(sourceType)) {
attila@963 280 return true;
attila@963 281 }
attila@963 282 if(sourceType.isPrimitive()) {
attila@963 283 if(sourceType == void.class) {
attila@963 284 return false; // Void can't be losslessly represented by any type
attila@963 285 }
attila@963 286 if(targetType.isPrimitive()) {
attila@963 287 return isProperPrimitiveLosslessSubtype(sourceType, targetType);
attila@963 288 }
attila@963 289 // Boxing + widening reference conversion
attila@963 290 assert WRAPPER_TYPES.get(sourceType) != null : sourceType.getName();
attila@963 291 return targetType.isAssignableFrom(WRAPPER_TYPES.get(sourceType));
attila@963 292 }
attila@963 293 // Can't convert from any non-primitive type to any primitive type without data loss because of null.
attila@963 294 // Also, can't convert non-assignable reference types.
attila@963 295 return false;
attila@963 296 }
attila@963 297
attila@963 298 /**
attila@90 299 * Determines whether one type can be potentially converted to another type at runtime. Allows a conversion between
attila@90 300 * any subtype and supertype in either direction, and also allows a conversion between any two primitive types, as
attila@90 301 * well as between any primitive type and any reference type that can hold a boxed primitive.
attila@90 302 *
attila@90 303 * @param callSiteType the parameter type at the call site
attila@90 304 * @param methodType the parameter type in the method declaration
attila@90 305 * @return true if callSiteType is potentially convertible to the methodType.
attila@90 306 */
attila@962 307 public static boolean isPotentiallyConvertible(final Class<?> callSiteType, final Class<?> methodType) {
attila@90 308 // Widening or narrowing reference conversion
attila@963 309 if(areAssignable(callSiteType, methodType)) {
attila@90 310 return true;
attila@90 311 }
attila@90 312 if(callSiteType.isPrimitive()) {
attila@90 313 // Allow any conversion among primitives, as well as from any
attila@90 314 // primitive to any type that can receive a boxed primitive.
attila@90 315 // TODO: narrow this a bit, i.e. allow, say, boolean to Character?
attila@90 316 // MethodHandles.convertArguments() allows it, so we might need to
attila@90 317 // too.
attila@90 318 return methodType.isPrimitive() || isAssignableFromBoxedPrimitive(methodType);
attila@90 319 }
attila@90 320 if(methodType.isPrimitive()) {
attila@90 321 // Allow conversion from any reference type that can contain a
attila@90 322 // boxed primitive to any primitive.
attila@90 323 // TODO: narrow this a bit too?
attila@90 324 return isAssignableFromBoxedPrimitive(callSiteType);
attila@90 325 }
attila@90 326 return false;
attila@90 327 }
attila@90 328
attila@90 329 /**
attila@963 330 * Returns true if either of the types is assignable from the other.
attila@963 331 * @param c1 one of the types
attila@963 332 * @param c2 another one of the types
attila@963 333 * @return true if either c1 is assignable from c2 or c2 is assignable from c1.
attila@963 334 */
attila@963 335 public static boolean areAssignable(final Class<?> c1, final Class<?> c2) {
attila@963 336 return c1.isAssignableFrom(c2) || c2.isAssignableFrom(c1);
attila@963 337 }
attila@963 338
attila@963 339 /**
attila@90 340 * Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping". Note: this is not strict
attila@90 341 * or proper subtype, therefore true is also returned for identical types; to be completely precise, it allows
attila@90 342 * identity conversion (JLS 5.1.1), widening primitive conversion (JLS 5.1.2) and widening reference conversion (JLS
attila@90 343 * 5.1.5).
attila@90 344 *
attila@90 345 * @param subType the supposed subtype
attila@90 346 * @param superType the supposed supertype of the subtype
attila@90 347 * @return true if subType can be converted by identity conversion, widening primitive conversion, or widening
attila@90 348 * reference conversion to superType.
attila@90 349 */
attila@962 350 public static boolean isSubtype(final Class<?> subType, final Class<?> superType) {
attila@90 351 // Covers both JLS 4.10.2 "Subtyping among Class and Interface Types"
attila@90 352 // and JLS 4.10.3 "Subtyping among Array Types", as well as primitive
attila@90 353 // type identity.
attila@90 354 if(superType.isAssignableFrom(subType)) {
attila@90 355 return true;
attila@90 356 }
attila@90 357 // JLS 4.10.1 "Subtyping among Primitive Types". Note we don't test for
attila@90 358 // identity, as identical types were taken care of in the
attila@90 359 // isAssignableFrom test. As per 4.10.1, the supertype relation is as
attila@90 360 // follows:
attila@90 361 // double > float
attila@90 362 // float > long
attila@90 363 // long > int
attila@90 364 // int > short
attila@90 365 // int > char
attila@90 366 // short > byte
attila@90 367 if(superType.isPrimitive() && subType.isPrimitive()) {
attila@90 368 return isProperPrimitiveSubtype(subType, superType);
attila@90 369 }
attila@90 370 return false;
attila@90 371 }
attila@90 372
attila@90 373 /**
attila@90 374 * Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the
attila@90 375 * supposed primitive supertype
attila@90 376 *
attila@90 377 * @param subType the supposed subtype
attila@90 378 * @param superType the supposed supertype
attila@90 379 * @return true if subType is a proper (not identical to) primitive subtype of the superType
attila@90 380 */
attila@962 381 private static boolean isProperPrimitiveSubtype(final Class<?> subType, final Class<?> superType) {
attila@90 382 if(superType == boolean.class || subType == boolean.class) {
attila@90 383 return false;
attila@90 384 }
attila@90 385 if(subType == byte.class) {
attila@90 386 return superType != char.class;
attila@90 387 }
attila@90 388 if(subType == char.class) {
attila@90 389 return superType != short.class && superType != byte.class;
attila@90 390 }
attila@90 391 if(subType == short.class) {
attila@90 392 return superType != char.class && superType != byte.class;
attila@90 393 }
attila@90 394 if(subType == int.class) {
attila@90 395 return superType == long.class || superType == float.class || superType == double.class;
attila@90 396 }
attila@90 397 if(subType == long.class) {
attila@90 398 return superType == float.class || superType == double.class;
attila@90 399 }
attila@90 400 if(subType == float.class) {
attila@90 401 return superType == double.class;
attila@90 402 }
attila@90 403 return false;
attila@90 404 }
attila@90 405
attila@963 406 /**
attila@963 407 * Similar to {@link #isProperPrimitiveSubtype(Class, Class)}, except it disallows conversions from int and long to
attila@963 408 * float, and from long to double, as those can lose precision. It also disallows conversion from and to char and
attila@963 409 * anything else (similar to boolean) as char is not meant to be an arithmetic type.
attila@963 410 * @param subType the supposed subtype
attila@963 411 * @param superType the supposed supertype
attila@963 412 * @return true if subType is a proper (not identical to) primitive subtype of the superType that can be represented
attila@963 413 * by the supertype without no precision loss.
attila@963 414 */
attila@963 415 private static boolean isProperPrimitiveLosslessSubtype(final Class<?> subType, final Class<?> superType) {
attila@963 416 if(superType == boolean.class || subType == boolean.class) {
attila@963 417 return false;
attila@963 418 }
attila@963 419 if(superType == char.class || subType == char.class) {
attila@963 420 return false;
attila@963 421 }
attila@963 422 if(subType == byte.class) {
attila@963 423 return true;
attila@963 424 }
attila@963 425 if(subType == short.class) {
attila@963 426 return superType != byte.class;
attila@963 427 }
attila@963 428 if(subType == int.class) {
attila@963 429 return superType == long.class || superType == double.class;
attila@963 430 }
attila@963 431 if(subType == float.class) {
attila@963 432 return superType == double.class;
attila@963 433 }
attila@963 434 return false;
attila@963 435 }
attila@963 436
attila@90 437 private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPES = createWrapperToPrimitiveTypes();
attila@90 438
attila@90 439 private static Map<Class<?>, Class<?>> createWrapperToPrimitiveTypes() {
attila@90 440 final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>();
attila@90 441 classes.put(Void.class, Void.TYPE);
attila@90 442 classes.put(Boolean.class, Boolean.TYPE);
attila@90 443 classes.put(Byte.class, Byte.TYPE);
attila@90 444 classes.put(Character.class, Character.TYPE);
attila@90 445 classes.put(Short.class, Short.TYPE);
attila@90 446 classes.put(Integer.class, Integer.TYPE);
attila@90 447 classes.put(Long.class, Long.TYPE);
attila@90 448 classes.put(Float.class, Float.TYPE);
attila@90 449 classes.put(Double.class, Double.TYPE);
attila@90 450 return classes;
attila@90 451 }
attila@90 452
attila@90 453 private static final Set<Class<?>> PRIMITIVE_WRAPPER_TYPES = createPrimitiveWrapperTypes();
attila@90 454
attila@90 455 private static Set<Class<?>> createPrimitiveWrapperTypes() {
attila@90 456 final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>();
attila@90 457 addClassHierarchy(classes, Boolean.class);
attila@90 458 addClassHierarchy(classes, Byte.class);
attila@90 459 addClassHierarchy(classes, Character.class);
attila@90 460 addClassHierarchy(classes, Short.class);
attila@90 461 addClassHierarchy(classes, Integer.class);
attila@90 462 addClassHierarchy(classes, Long.class);
attila@90 463 addClassHierarchy(classes, Float.class);
attila@90 464 addClassHierarchy(classes, Double.class);
attila@90 465 return classes.keySet();
attila@90 466 }
attila@90 467
attila@962 468 private static void addClassHierarchy(final Map<Class<?>, Class<?>> map, final Class<?> clazz) {
attila@90 469 if(clazz == null) {
attila@90 470 return;
attila@90 471 }
attila@90 472 map.put(clazz, clazz);
attila@90 473 addClassHierarchy(map, clazz.getSuperclass());
attila@962 474 for(final Class<?> itf: clazz.getInterfaces()) {
attila@90 475 addClassHierarchy(map, itf);
attila@90 476 }
attila@90 477 }
attila@90 478
attila@90 479 /**
attila@90 480 * Returns true if the class can be assigned from any boxed primitive.
attila@90 481 *
attila@90 482 * @param clazz the class
attila@90 483 * @return true if the class can be assigned from any boxed primitive. Basically, it is true if the class is any
attila@90 484 * primitive wrapper class, or a superclass or superinterface of any primitive wrapper class.
attila@90 485 */
attila@962 486 private static boolean isAssignableFromBoxedPrimitive(final Class<?> clazz) {
attila@90 487 return PRIMITIVE_WRAPPER_TYPES.contains(clazz);
attila@90 488 }
attila@90 489
attila@90 490 /**
attila@90 491 * Given a name of a primitive type (except "void"), returns the class representing it. I.e. when invoked with
attila@90 492 * "int", returns {@link Integer#TYPE}.
attila@90 493 * @param name the name of the primitive type
attila@90 494 * @return the class representing the primitive type, or null if the name does not correspond to a primitive type
attila@90 495 * or is "void".
attila@90 496 */
attila@962 497 public static Class<?> getPrimitiveTypeByName(final String name) {
attila@90 498 return PRIMITIVE_TYPES_BY_NAME.get(name);
attila@90 499 }
attila@90 500
attila@90 501 /**
attila@90 502 * When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding
attila@90 503 * primitive type. I.e. calling it with {@code Integer.class} will return {@code Integer.TYPE}. If passed a class
attila@90 504 * that is not a wrapper for primitive type, returns null.
attila@90 505 * @param wrapperType the class object representing a wrapper for a primitive type
attila@90 506 * @return the class object representing the primitive type, or null if the passed class is not a primitive wrapper.
attila@90 507 */
attila@962 508 public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
attila@90 509 return WRAPPER_TO_PRIMITIVE_TYPES.get(wrapperType);
attila@90 510 }
attila@90 511
attila@90 512
attila@90 513 /**
attila@90 514 * When passed a class representing a primitive type, returns the class representing the corresponding
attila@90 515 * wrapper type. I.e. calling it with {@code int.class} will return {@code Integer.class}. If passed a class
attila@90 516 * that is not a primitive type, returns null.
attila@90 517 * @param primitiveType the class object representing a primitive type
attila@90 518 * @return the class object representing the wrapper type, or null if the passed class is not a primitive.
attila@90 519 */
attila@962 520 public static Class<?> getWrapperType(final Class<?> primitiveType) {
attila@90 521 return WRAPPER_TYPES.get(primitiveType);
attila@90 522 }
attila@1090 523
attila@1090 524 /**
attila@1090 525 * Returns true if the passed type is a wrapper for a primitive type.
attila@1090 526 * @param type the examined type
attila@1090 527 * @return true if the passed type is a wrapper for a primitive type.
attila@1090 528 */
attila@1090 529 public static boolean isWrapperType(final Class<?> type) {
attila@1090 530 return PRIMITIVE_TYPES.containsKey(type);
attila@1090 531 }
attila@101 532 }

mercurial