Java Primitive Type Create toPrimitiveType(Class cls)

Here you can find the source of toPrimitiveType(Class cls)

Description

to Primitive Type

License

Open Source License

Declaration

public static Class<?> toPrimitiveType(Class<?> cls) 

Method Source Code

//package com.java2s;
/*/*from  ww  w .  j a  v a 2s  . co m*/
 * Copyright (c) 2005, Jeong-Ho Eun
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

public class Main {

    public static Class<?> toPrimitiveType(Class<?> cls) {
        Class<?> pType = cls;
        if (!cls.isArray()) {
            if (Boolean.TYPE == cls)
                pType = Boolean.class;
            else if (Character.TYPE == cls)
                pType = Character.class;
            else if (Byte.TYPE == cls)
                pType = Byte.class;
            else if (Short.TYPE == cls)
                pType = Short.class;
            else if (Integer.TYPE == cls)
                pType = Integer.class;
            else if (Long.TYPE == cls)
                pType = Long.class;
            else if (Float.TYPE == cls)
                pType = Float.class;
            else if (Double.TYPE == cls)
                pType = Double.class;
        } else {
            Class<?> itemType = cls.getComponentType();
            if (Boolean.TYPE == itemType)
                pType = Boolean[].class;
            else if (Character.TYPE == itemType)
                pType = Character[].class;
            else if (Byte.TYPE == itemType)
                pType = Byte[].class;
            else if (Short.TYPE == itemType)
                pType = Short[].class;
            else if (Integer.TYPE == itemType)
                pType = Integer[].class;
            else if (Long.TYPE == itemType)
                pType = Long[].class;
            else if (Float.TYPE == itemType)
                pType = Float[].class;
            else if (Double.TYPE == itemType)
                pType = Double[].class;
        }
        return pType;
    }
}

Related

  1. toPrimitiveDouble(Double d)
  2. toPrimitiveInt(Integer value)
  3. toPrimitiveInt(Object o)
  4. toPrimitiveNumber(String s)
  5. toPrimitiveType(Class c)
  6. toPrimitiveType(Class type)
  7. toPrimitiveWrapper(Class type)
  8. toPrimitiveWrapper(String primitiveJson, Class clazz)