Here you can find the source of toPrimitive(Object o)
public static Object toPrimitive(Object o)
//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); } }