Java Generic methods

Source: Internet
Author: User

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

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.