Change primitive types class to there 'boxed' compatible type. - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

Change primitive types class to there 'boxed' compatible type.

Demo Code


//package com.java2s;
import android.support.annotation.NonNull;

public class Main {
    /**/*from  w  w w.ja v a2  s.co  m*/
     * Change primitive types class to there 'boxed' compatible type.
     *
     * @param type check is provided type is primitive that can be converted to BOXED version.
     */
    public static Class<?> boxing(@NonNull final Class<?> type) {
        if (type.isPrimitive()) {
            if (boolean.class.equals(type))
                return Boolean.class;
            if (char.class.equals(type))
                return Character.class;
            if (byte.class.equals(type))
                return Byte.class;
            if (short.class.equals(type))
                return Short.class;
            if (int.class.equals(type))
                return Integer.class;
            if (long.class.equals(type))
                return Long.class;
            if (float.class.equals(type))
                return Float.class;
            if (double.class.equals(type))
                return Double.class;

            throw new AssertionError(
                    "Not implemented! primitive to 'boxed' convert failed for type:"
                            + type);
        }

        return type;
    }
}

Related Tutorials