Java Reflection Primitive unwrapPrimitive(final Class clazz)

Here you can find the source of unwrapPrimitive(final Class clazz)

Description

If the specified Class is a wrapped primitive, then this function will return the primitive version.

License

Apache License

Parameter

Parameter Description
clazz The Class to unwrap

Return

The specified or the primitive version of the if it is a wrapped primitive

Declaration

public static Class<?> unwrapPrimitive(final Class<?> clazz) 

Method Source Code

//package com.java2s;
/*/*from   ww  w. ja v a2  s .  c  om*/
 * Copyright 2012 Robert Philipp
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Class<?>, Class<?>> UNWRAP_MAP = new HashMap<>();

    /**
     * If the specified {@link Class} is a wrapped primitive, then this function will return the primitive
     * version. For example, if the specified {@link Class} is {@code Integer.class} this method will return 
     * {@code int.class} or equivalently, or {@code Integer.TYPE}
     * @param clazz The {@link Class} to unwrap
     * @return The specified {@link Class} or the primitive version of the {@link Class} if it is a wrapped primitive
     */
    public static Class<?> unwrapPrimitive(final Class<?> clazz) {
        if (UNWRAP_MAP.containsKey(clazz)) {
            return UNWRAP_MAP.get(clazz);
        } else {
            return clazz;
        }
    }
}

Related

  1. resolvePrimitiveClassName(String name)
  2. resolvePrimitiveClassName(String name)
  3. resolvePrimitiveIfNecessary(Class clazz)
  4. resolvePrimitiveType(Class clazz)
  5. toPrimitive(String primitiveJson, Class clazz)