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

Thu, 31 Aug 2017 15:30:47 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:30:47 +0800
changeset 952
6d5471a497fb
parent 101
f8221ce53c2e
parent 0
b1a7da25b547
child 1205
4112748288bb
permissions
-rw-r--r--

merge

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

mercurial