Java Object Array Convert To convertObject(final Object obj, final Class clazz)

Here you can find the source of convertObject(final Object obj, final Class clazz)

Description

Converts an given object to the given class type.

License

Open Source License

Parameter

Parameter Description
obj the object to convert
clazz the destination class type

Return

the converted object of the given class type or null if the conversion failed

Declaration

public static final Object convertObject(final Object obj, final Class<?> clazz) 

Method Source Code

//package com.java2s;
/** /*from  w  w  w  .ja v  a  2s . c o  m*/
 * ============================================================================
 * Xirp 2: eXtendable interface for robotic purposes.
 * ============================================================================
 * 
 * Copyright (C) 2005-2007, by Authors and Contributors listed in CREDITS.txt
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at:
 *
 *             http://www.opensource.org/licenses/cpl1.0.php
 *
 * ----------------------------
 * Util.java
 * ----------------------------
 *
 * Original Author:  Matthias Gernand [matthias.gernand AT gmx.de]
 * Contributor(s):   Rabea Gransberger [rgransberger AT web.de]
 *
 * Changes
 * -------
 * 08.05.2006:      Created by Matthias Gernand.
 */

public class Main {
    /**
     * Converts an given object to the given class type.
     * 
     * @param obj
     *            the object to convert
     * @param clazz
     *            the destination class type
     * @return the converted object of the given class type or
     *         <code>null</code> if the conversion failed
     */
    public static final Object convertObject(final Object obj, final Class<?> clazz) {
        // the object has already the correct type
        if (clazz.isAssignableFrom(obj.getClass())) {
            return obj;
        } else {
            // use to string to convert to an object to a string
            if (String.class.isAssignableFrom(clazz)) {
                return obj.toString();
            }
            // use the parse methods to convert from String to a
            // number
            // or the according number method to convert from
            // number to an other number
            else if (Integer.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Integer i = Integer.parseInt(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.intValue();
                }
            } else if (Double.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Double i = Double.parseDouble(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.doubleValue();
                }
            } else if (Long.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Long i = Long.parseLong(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.longValue();
                }
            } else if (Float.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Float i = Float.parseFloat(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.floatValue();
                }
            } else if (Short.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Short i = Short.parseShort(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.shortValue();
                }
            } else if (Byte.class.isAssignableFrom(clazz)) {
                if (obj instanceof String) {
                    String strg = (String) obj;
                    try {
                        Byte i = Byte.parseByte(strg);
                        return i;
                    } catch (NumberFormatException e) {
                        // do nothing
                    }
                } else if (obj instanceof Number) {
                    Number d = (Number) obj;
                    return d.byteValue();
                }
            }
            // return null if conversion fails
            return null;
        }
    }
}

Related

  1. convertObjectArrayToByte(Object[] array)
  2. convertObjectGUIToByteString(byte[] objectGUID)
  3. convertObjectToBean(Object obj, Class clazz)
  4. convertObjectToBoolean(Object value)