Java Reflection Primitive convertPrimitiveToWrapper(Class clazz)

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

Description

Checks whether a given class object is a primitive and returns its wrapper class (except for void where always the primitive class is used).

License

Open Source License

Parameter

Parameter Description
clazz the class to convert

Return

the wrapper class

Declaration

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

Method Source Code

//package com.java2s;

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

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

    /**// ww w .  ja va  2 s  .com
     * Checks whether a given class object is a primitive and returns its
     * wrapper class (except for void where always the primitive class is used).
     * If the class is no primitive the class will be returned without changes.
     *
     * @param clazz the class to convert
     * @return the wrapper class
     */
    public static Class<?> convertPrimitiveToWrapper(Class<?> clazz) {
        Class<?> result = clazz;

        if (clazz.isPrimitive()) {
            //            if (clazz.getName().equals("boolean")) {
            //                result = Boolean.class;
            //            } else if (clazz.getName().equals("short")) {
            //                result = Short.class;
            //            } else if (clazz.getName().equals("int")) {
            //                result = Integer.class;
            //            } else if (clazz.getName().equals("long")) {
            //                result = Long.class;
            //            } else if (clazz.getName().equals("float")) {
            //                result = Float.class;
            //            } else if (clazz.getName().equals("double")) {
            //                result = Double.class;
            //            } else if (clazz.getName().equals("byte")) {
            //                result = Byte.class;
            //            } else if (clazz.getName().equals("char")) {
            //                result = Character.class;
            //            }

            return primitiveWrapperClasses.get(clazz.getName());
        }

        return result;
    }
}

Related

  1. calculateDistancePrimitive(Class a, Class b)
  2. canCoerceLiteralNumberExpr(Class type)
  3. castToPrimitive(Object start, Class prim)
  4. convertPrimitiveToObjectClass(Class clazz)
  5. convertPrimitiveToWrapper(Class clazz)
  6. convertToPrimitive(Class type)
  7. convertWrapperToPrimitive(Class clazz)
  8. createDefaultPrimitive(Class type)
  9. getBENameByPrimitiveClassName( String implementClassName)