Java Primitive Type Create toPrimitive(Object o)

Here you can find the source of toPrimitive(Object o)

Description

to Primitive

License

Open Source License

Declaration

public static Object toPrimitive(Object o) 

Method Source Code

//package com.java2s;

public class Main {
    public static Object toPrimitive(Object o) {

        if (o == null)
            return null;

        Class<?> c = o.getClass();
        if (c.isPrimitive())
            return o;

        // In descending order of likelihood...
        if (c.equals(Void.class))
            return o;
        else if (c.equals(Boolean.class))
            return (boolean) (Boolean) o;
        else if (c.equals(Integer.class))
            return (int) (Integer) o;
        else if (c.equals(Double.class))
            return (double) (Double) o;
        else if (c.equals(Long.class))
            return (long) (Long) o;
        else if (c.equals(Character.class))
            return (char) (Character) o;
        else if (c.equals(Byte.class))
            return (byte) (Byte) o;
        else if (c.equals(Short.class))
            return (short) (Short) o;
        else if (c.equals(Float.class))
            return (float) (Float) o;

        return o;
    }/* www  .  j av a2 s  .co m*/

    /**
     * Gets the class with the given binary name using the given classLoader.
     * If a user-defined class isn't found with the given name, checks for primitive types.
     * 
     * @param className
     * @param classLoader
     * @return
     * @throws ClassNotFoundException
     */
    public static Class<?> getClass(String className, ClassLoader classLoader) throws ClassNotFoundException {

        try {
            return Class.forName(className, false, classLoader);
        } catch (ClassNotFoundException cnfe) {
            if (className.equals("Z") || className.equals("boolean"))
                return Boolean.TYPE;
            else if (className.equals("B") || className.equals("byte"))
                return Byte.TYPE;
            else if (className.equals("C") || className.equals("char"))
                return Character.TYPE;
            else if (className.equals("D") || className.equals("double"))
                return Double.TYPE;
            else if (className.equals("F") || className.equals("float"))
                return Float.TYPE;
            else if (className.equals("I") || className.equals("int"))
                return Integer.TYPE;
            else if (className.equals("J") || className.equals("long"))
                return Long.TYPE;
            else if (className.equals("S") || className.equals("short"))
                return Short.TYPE;
            else
                throw cnfe;
        }
    }

    /**
     * Returns true if obj1 and obj2 are both null, or if obj1.equals(obj2). Else returns false.
     * 
     * @param obj1
     * @param obj2
     * @return boolean
     */
    public static boolean equals(Object obj1, Object obj2) {

        if (obj1 == obj2)
            return true;
        if (obj1 == null || obj2 == null)
            return false;
        return obj1.equals(obj2);
    }
}

Related

  1. toPrimitive(Integer[] ints)
  2. toPrimitive(Integer[] irri)
  3. toPrimitive(Long[] array)
  4. toPrimitive(Long[] array)
  5. toPrimitive(Long[] array)
  6. toPrimitive(Object value)
  7. toPrimitiveArray(Boolean[] a)
  8. toPrimitiveArray(Byte[] array)
  9. toPrimitiveArray(Byte[] bytes)