Java object converted to another class object generic

Source: Internet
Author: User
Tags object object

object is converted to another class object

Package com.mw.usims.fts.util;
Import Com.mw.usims.fts.model.BaseModel;

Import ORG.APACHE.POI.SS.FORMULA.FUNCTIONS.T;
Import Java.lang.reflect.Field;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.HashMap;

Import Java.util.Map;
 /** * Created by Lianan on 2015/2/3. */public class Test1 {public static <T> T Convertclass (Object obj,class<t> cla) throws Nosuchmethodexce  Ption, InvocationTargetException, Illegalaccessexception, instantiationexception {map<string,object> maps =
        New Hashmap<string,object> ();
        T Databean = null;
        if (null==obj) {return null;
        } class<?> CLS = Obj.getclass ();
        Databean = Cla.newinstance ();
        field[] fields = Cls.getdeclaredfields ();
        field[] Beanfields = Cla.getdeclaredfields (); for (Field Field:fields) {String fieldName = Field.getname ();
            String strget = "Get" + fieldname.substring (0, 1). toUpperCase () + fieldname.substring (1, Fieldname.length ());
            Method Methodget = Cls.getdeclaredmethod (strget);
            Object object = Methodget.invoke (obj); Maps.put (fieldname,object==null? "": O
        Bject);
            for (Field field:beanfields) {field.setaccessible (true);
            String fieldName = Field.getname ();
            class<?> FieldType = Field.gettype ();
            String fieldvalue = Maps.get (fieldName) ==null?null:maps.get (fieldName). toString ();
                        if (fieldvalue!=null) {try {if (String.class.equals (FieldType)) {
                    Field.set (Databean, fieldvalue);

                    }else if (byte.class.equals (FieldType)) {Field.setbyte (Databean, Byte.parsebyte (Fieldvalue));
    }else if (Byte.class.equals (FieldType)) {                    Field.set (Databean, byte.valueof (Fieldvalue)); }else if (boolean.class.equals (FieldType)) {Field.setboolean (Databean, Boolean.parseboolean (Fieldva

                    Lue));

                    }else if (Boolean.class.equals (FieldType)) {Field.set (Databean, boolean.valueof (Fieldvalue)); }else if (short.class.equals (FieldType)) {Field.setshort (Databean, Short.parseshor

                    T (Fieldvalue));

                    }else if (Short.class.equals (FieldType)) {Field.set (Databean, short.valueof (Fieldvalue)); }else if (int.class.equals (FieldType)) {Field.setint (Databean, Integer.parseint (FIELDV

                    Alue));

                    }else if (Integer.class.equals (FieldType)) {Field.set (Databean, integer.valueof (Fieldvalue)); }else if (long.class.equals (FieldType)) {field.seTlong (Databean, Long.parselong (Fieldvalue));

                    }else if (Long.class.equals (FieldType)) {Field.set (Databean, long.valueof (Fieldvalue)); }else if (float.class.equals (FieldType)) {field.setfloat (Databean, Float.parsefloat (fiel

                    Dvalue));

                    }else if (Float.class.equals (FieldType)) {Field.set (Databean, float.valueof (Fieldvalue)); }else if (double.class.equals (FieldType)) {field.setdouble (Databean, Double.parsedoubl

                    E (fieldvalue));

                    }else if (Double.class.equals (FieldType)) {Field.set (Databean, double.valueof (Fieldvalue)); }else if (Date.class.equals (FieldType)) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.")
                        SSS ");
                    Field.set (Databean, Sdf.parse (Fieldvalue)); }
                }catch (IllegalArgumentException e) {e.printstacktrace ();
                catch (ParseException e) {e.printstacktrace ();

    }} return Databean; public static void Main (String arg0[]) throws Nosuchmethodexception, Illegalaccessexception, invocationtargetexcept
        Ion, instantiationexception {Basemodel Basemodel = new Basemodel ();
        Basemodel.setid ("1");
        Basemodel.setdescription ("2");
        Basemodel.setsequence (3);
        Basemodel.settype ("4");
        Basemodel.setvalue ("5");
        Basemodel base = Convertclass (Basemodel, Basemodel.class);
    System.out.println (Base.getid ());
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.