Java Reflection Primitive getWrapper(final Class beanClass)

Here you can find the source of getWrapper(final Class beanClass)

Description

Get the wrapper class for this class if there is one

License

Apache License

Parameter

Parameter Description
beanClass any class

Return

the wrapper class if there is one OR just returns the given class

Declaration

public static Class<?> getWrapper(final Class<?> beanClass) 

Method Source Code

//package com.java2s;
/**//  w ww . ja va  2 s. c  o m
 * $Id: ConstructorUtils.java 61 2009-09-25 11:14:16Z azeckoski $
 * $URL: http://reflectutils.googlecode.com/svn/trunk/src/main/java/org/azeckoski/reflectutils/ConstructorUtils.java $
 * FieldUtils.java - genericdao - May 19, 2008 10:10:15 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski (azeckoski @ gmail.com) (aaronz @ vt.edu) (aaron @ caret.cam.ac.uk)
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<Class<?>, Class<?>> primitiveToWrapper = null;

    /**
     * Get the wrapper class for this class if there is one
     * @param beanClass any class
     * @return the wrapper class if there is one OR just returns the given class
     */
    public static Class<?> getWrapper(final Class<?> beanClass) {
        Class<?> wrapper = null;
        if (beanClass != null) {
            if (isClassPrimitive(beanClass)) {
                wrapper = getPrimitiveToWrapper().get(beanClass);
            } else if (isClassArray(beanClass) && beanClass.getComponentType().isPrimitive()) {
                wrapper = getPrimitiveToWrapper().get(beanClass);
            } else {
                wrapper = beanClass;
            }
            if (wrapper == null) {
                wrapper = beanClass;
            }
        }
        return wrapper;
    }

    /**
     * @param type any class
     * @return true if this class is a primitive (e.g. int.class, boolean.class)
     */
    public static boolean isClassPrimitive(Class<?> type) {
        checkNull(type);
        boolean primitive = false;
        if (type.isPrimitive()) {
            primitive = true;
        }
        return primitive;
    }

    /**
     * @return the map of all primitive types -> wrapper types
     */
    public static synchronized Map<Class<?>, Class<?>> getPrimitiveToWrapper() {
        if (primitiveToWrapper == null || primitiveToWrapper.isEmpty()) {
            makePrimitiveWrapperMap();
        }
        return primitiveToWrapper;
    }

    /**
     * @param type any class
     * @return true if this class is an array (e.g. int[].class, {@link Integer}[] )
     */
    public static boolean isClassArray(Class<?> type) {
        checkNull(type);
        boolean array = false;
        if (type.isArray()) {
            array = true;
        }
        return array;
    }

    private static void checkNull(Class<?> type) {
        if (type == null) {
            throw new IllegalArgumentException("class type cannot be null to check the type");
        }
    }

    private static void makePrimitiveWrapperMap() {
        primitiveToWrapper = new HashMap<Class<?>, Class<?>>();
        primitiveToWrapper.put(boolean.class, Boolean.class);
        primitiveToWrapper.put(byte.class, Byte.class);
        primitiveToWrapper.put(char.class, Character.class);
        primitiveToWrapper.put(double.class, Double.class);
        primitiveToWrapper.put(float.class, Float.class);
        primitiveToWrapper.put(int.class, Integer.class);
        primitiveToWrapper.put(long.class, Long.class);
        primitiveToWrapper.put(short.class, Short.class);
        primitiveToWrapper.put(boolean[].class, Boolean[].class);
        primitiveToWrapper.put(byte[].class, Byte[].class);
        primitiveToWrapper.put(char[].class, Character[].class);
        primitiveToWrapper.put(double[].class, Double[].class);
        primitiveToWrapper.put(float[].class, Float[].class);
        primitiveToWrapper.put(int[].class, Integer[].class);
        primitiveToWrapper.put(long[].class, Long[].class);
        primitiveToWrapper.put(short[].class, Short[].class);
    }
}

Related

  1. getPrimitiveType(String javaClassName)
  2. getPrimitiveWrapper(Class primitiveType)
  3. getRank(Class aNumberClass)
  4. getWrapper(Class clazz)
  5. getWrapper(Class type)
  6. getWrapperClass(Class clazz)
  7. getWrapperClass(Class primitiveClass)
  8. getWrapperClass(String className)
  9. getWrapperClassForPrimitive(Class primitiveClass)