Sometimes a static method needs to be supported by generics, and this static method must be modified to be a generic method. That is, add a <t> before the return value;.
For a static method, the type parameters of the generic class cannot be accessed, so if the static method requires the use of generic capabilities, it must be made a generic method.
/*** Document converted to class * * @Title: GetObject *@paramDoc *@paramCLS *@return * @authorLihui *@param<T> *@throwsException *@throwsSecurityException *@throwsnosuchmethodexception * @date September 18, 2015 PM 1:18:29*/ Public Static<T>voidGetObject (Document doc, T CLS)throwsnosuchmethodexception, SecurityException, Exception {Field fd[]=Cls.getclass (). Getdeclaredfields (); Object Object=CLS; for(Field field:fd) {System.out.println (Field.getgenerictype (). toString ()); Object value=Doc.get (Field.getname ()); if(Value! =NULL&&!value.tostring (). Equals ("") ) {PropertyDescriptor pd=NewPropertyDescriptor (Field.getname (), Cls.getclass ()); Method m= Pd.getwritemethod ();//Get Write Method if(Field.getgenerictype (). toString (). Equals ("Class java.lang.String") {M.invoke (object, value.tostring ()); } Else if(Field.getgenerictype (). toString (). Equals ("Class Java.lang.Integer") {M.invoke (object, Integer.valueof (Value.tostring ())); } Else if(Field.getgenerictype (). toString (). Equals ("Class java.lang.Double") {M.invoke (object, Double.valueof (Value.tostring ())); } Else if(Field.getgenerictype (). toString (). Equals ("Class Java.lang.Long") {M.invoke (object, Long.valueof (Value.tostring ())); } Else if(Field.getgenerictype (). toString (). Equals ("Class Java.lang.Boolean") {M.invoke (object, Boolean.valueof (Value.tostring ())); } Else if(Field.getgenerictype (). toString (). Equals ("Class Java.util.Date") ) {SimpleDateFormat df=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); M.invoke (object, Df.parse (Value.tostring ())); } } } }
Java Generic methods