Import java. lang. reflect. invocationTargetException; import java. lang. reflect. method; import java. math. bigDecimal; import java. util. arrayList; import java. util. list; import com. upward. hbnx. model. EChannel;/***** @ author xiaoyang avoid NULL pointer * avoid the object class from reporting the get method of the NULL pointer attribute, assign the **/public class InitBeanUtil {private static InitBeanUtil init = null; private final String START_SET = "set"; private final String START_GET = "get "; private final String START_IS = "is"; private final int START_THREE = 3; // "set" String length 3 // initial value of the parameter --- begin private final String INIT_STRING = ""; private final int INIT_INT = 0; private final double INIT_DOUBLE = 0.0; private final float INIT_FLOAT = 0; private final boolean INIT_BOOLEAN = true; private final char INIT_CHAR = ''; private final BigDecimal INIT_BigDecimal = new BigDecimal ("0.0"); private final Integer INIT_Integer = new Integer (0); private final Double INIT_Double = new Double (0.0 ); private final Float INIT_Float = new Float (0.0); private final Boolean INIT_Boolean = new Boolean (true); private final Character INIT_Character = new Character (''); private final java. SQL. date INIT_DateS = new java. SQL. date (System. currentTimeMillis (); private final java. util. date INIT_DateU = new java. util. date (System. currentTimeMillis (); private final java. SQL. timestamp INIT_Timestamp = new java. SQL. timestamp (System. currentTimeMillis (); private final long INIT_Long = new Long (0); @ SuppressWarnings ("rawtypes") private final java. util. list INIT_List = new ArrayList (); // initial value of the parameter --- end // parameter type --- begin encapsulation type. The complete name is private final String TYPE_String = "java. lang. string "; private final String TYPE_int =" int "; private final String TYPE_double =" double "; private final String TYPE_float =" float "; private final String TYPE_boolean =" boolean "; private final String TYPE_char = "char"; private final String TYPE_long = "long"; private final String TYPE_BigDecimal = "java. math. bigDecimal "; private final String TYPE_Integer =" java. lang. integer "; private final String TYPE_Double =" java. lang. double "; private final String TYPE_Float =" java. lang. float "; private final String TYPE_Boolean =" java. lang. boolean "; private final String TYPE_Character =" java. lang. character "; private final String TYPE_DateS =" java. SQL. date "; private final String TYPE_DateU =" java. util. date "; private final String TYPE_Timestamp =" java. SQL. timestamp "; private final String TYPE_List =" java. util. list "; // parameter type --- end @ SuppressWarnings (" rawtypes ") private List setList = new ArrayList (); // store all setter Methods @ SuppressWarnings (" rawtypes ") private List getList = new ArrayList (); // store all getter Methods private InitBeanUtil () {// No parameter constructor} // single-instance public static Object FZ (Object bean) {if (init = null) {init = new InitBeanUtil ();} return init. initBean (bean);}/*** main method of initializing attributes */@ SuppressWarnings ({"unchecked", "rawtypes"}) private Object initBean (Object bean) {Object object = null; try {// instantiate bean object = bean. getClass (). newInstance (); // obtain the bean Class name Class beanClass = bean. getClass (); // obtain the public setter Method of the Bean [] beanMethods = beanClass. getMethods (); for (int I = 0; I <beanMethods. length; I ++) {if (beanMethods [I]. getName (). startsWith (START_SET) {this. setList. add (beanMethods [I]);} else if (beanMethods [I]. getName (). startsWith (START_GET) | beanMethods [I]. getName (). startsWith (START_IS) {this. getList. add (beanMethods [I]) ;}}for (int I = 0; I <this. setList. size (); I ++) {Method setMethod = (Method) this. setList. get (I); String getMethod = getter (setMethod. getName (); // determines whether the attribute has the get Method if (hasMethod (getMethod) {Method m = (Method) getList. get (I); Object o = m. invoke (bean); // execute the get method to return an Object if (o = null) {// set method parameter type Class [] typeSet = setMethod. getParameterTypes (); // The set Method of javaBean has only one parameter. You only need to take the first Object [] oarray = initValue (typeSet [0]); setMethod. invoke (object, oarray);} else {Object [] oarray = new Object [1]; oarray [0] = o; setMethod. invoke (object, oarray) ;}}} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (InvocationTargetException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (InstantiationException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} return object;}/*** get a getter method attribute get method name */private String getter (String setter) {String getMethod = START_GET + setter. substring (START_THREE); if (hasMethod (getMethod) {// returns return getMethod if the getter method is obtained ;} else {// getter method is not obtained. The parameter may be boolean type getMethod = START_IS + setter. substring (START_THREE); if (hasMethod (getMethod) {return getMethod ;}} return getMethod ;} /*** <> determine whether the get Method exists <> */private boolean hasMethod (String MethodName) {boolean flag = false; String tempName = null; Method tempMethod = null; for (int k = 0; k <getList. size (); k ++) {tempMethod = (Method) getList. get (k); tempName = tempMethod. getName (); if (MethodName. equals (tempName) {flag = true; break;} return flag;}/*** main method for Attribute initialization <br> * can be changed as needed, for example, initialize a certain type of attribute <br> */private Object [] initValue (@ SuppressWarnings ("rawtypes") Class type) {Object [] oarray = new Object [1]; // The setter method of javaBean has only one parameter, and only one-dimensional Object array String typeStr = type is required. getName (); if (TYPE_String.equals (typeStr) {// String type initialization oarray [0] = INIT_STRING;} else if (TYPE_int.equals (typeStr )) {// initialization of the int type oarray [0] = new Integer (INIT_INT);} else if (TYPE_double.equals (typeStr )) {// double type initialization oarray [0] = new Double (INIT_DOUBLE);} else if (TYPE_float.equals (typeStr )) {// float Type initialization oarray [0] = new Float (INIT_FLOAT);} else if (String. valueOf (TYPE_char ). equals (typeStr) {// char type initialization oarray [0] = new Character (INIT_CHAR);} else if (TYPE_BigDecimal.equals (typeStr )) {// initialize oarray of the BigDecima type [0] = INIT_BigDecimal;} else if (TYPE_Integer.equals (typeStr) {// initialize oarray of the Integer type [0] = INIT_Integer ;} else if (TYPE_Double.equals (typeStr) {// Double type initialization oarray [0] = INIT_Double;} else if (TYPE_Float.equals (typeStr )) {// Float Type initialization oarray [0] = INIT_Float;} else if (TYPE_boolean.equals (typeStr) {// boolean Type initialization oarray [0] = new Boolean (INIT_BOOLEAN );} else if (TYPE_Boolean.equals (typeStr) {// Boolean Type initialization oarray [0] = INIT_Boolean;} else if (TYPE_Character.equals (typeStr )) {// Character type initialization oarray [0] = INIT_Character;} else if (TYPE_DateS.equals (typeStr) {// Date type initialization (java. SQL. date) oarray [0] = INIT_DateS;} else if (TYPE_DateU.equals (typeStr) {// Date type initialization (java. util. date) oarray [0] = INIT_DateU;} else if (TYPE_Timestamp.equals (typeStr) {// initialize the Timestamp type (java. SQL. timestamp) oarray [0] = INIT_Timestamp;} else if (TYPE_List.equals (typeStr) {// List type initialization (java. util. list) oarray [0] = INIT_List;} else if (TYPE_long.equals (typeStr) {// List type initialization (java. util. list) oarray [0] = INIT_Long;} else {try {oarray [0] = type. newInstance ();} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace () ;}} return oarray;} public static void main (String [] args) {EChannel test = new EChannel (); test. setBizType ("12321"); test = (EChannel) InitBeanUtil. FZ (test );}}