Java Reflection Primitive resolvePrimitiveIfNecessary(Class clazz)

Here you can find the source of resolvePrimitiveIfNecessary(Class clazz)

Description

Resolve the given class if it is a primitive class, returning the corresponding primitive wrapper type instead.

License

Open Source License

Parameter

Parameter Description
clazz the class to check

Return

the original class, or a primitive wrapper for the original primitive type

Declaration

public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**/*from w  ww  .  j a va  2 s.  c  om*/
     * Map with primitive type as key and corresponding wrapper
     * type as value, for example: int.class -> Integer.class.
     */
    private static final Map<Class<?>, Class<?>> primitiveTypeToWrapperMap = new HashMap<Class<?>, Class<?>>(8);

    /**
     * Resolve the given class if it is a primitive class,
     * returning the corresponding primitive wrapper type instead.
     * @param clazz the class to check
     * @return the original class, or a primitive wrapper for the original primitive type
     */
    public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("Class must not be null");
        }
        return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);
    }
}

Related

  1. primitiveToWrapper(final Class cls)
  2. primitiveToWrapper(final Class cls)
  3. resolveClassPrimitiveAware(String className)
  4. resolvePrimitiveClassName(String name)
  5. resolvePrimitiveClassName(String name)
  6. resolvePrimitiveType(Class clazz)
  7. toPrimitive(String primitiveJson, Class clazz)
  8. unwrapPrimitive(final Class clazz)