Problems with run-time type checking for Java generic functions

Source: Internet
Author: User

A generic function that defines data preservation and reading is defined in a data persistence process, but a type conversion error occurs at run time, the type does not match, the location of the error is not the Load method, but after the load method is called, the list data is obtained when the list data is used. The element in the result list is actually a type, and the load method is called Class B, but the load is still successful.

is very confused, eventually modify the code after debugging, solve the problem.

Import Android.content.context;import Android.text.textutils;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.objectinputstream;import java.util.arraylist;/** * Load object from private file * @param context * @param key value (file name) * @return */public Stati C Object Loadfromprivatefile (context context, String key) {if (context = = NULL | |        Textutils.isempty (key)) {return null;        } ObjectInputStream objectin = null;        Object result = null;            try {fileinputstream Filein = context.openfileinput (key);            Objectin = new ObjectInputStream (Filein);        result = Objectin.readobject ();        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();        } catch (ClassNotFoundException e) {e.printstacktrace (); } finally {if (objectin! = null) {try{Objectin.close ();    } catch (IOException e) {}}} return result; /** * Load Entity Object * @param context * @param key value (file name) * @param clazzoft class type */public static < T> T Loadentityobject (context context, String key, class<t> clazzoft) {Object object = Loadfromprivatefil        E (context, key); if (Object! = NULL && Clazzoft! = null && clazzoft.isinstance (object)) {return Clazzoft.cast (o        Bject);        } try {return (T) clazzoft.newinstance ();        } catch (Exception e) {e.printstacktrace ();    } return null; /** * Load Array list * @param context * @param key value (file name) * @param clazzoft class type * @return */@Sup Presswarnings ("unchecked") public static <T> arraylist<t> loadarraylist (context context, String key, class& Lt T> clazzoft) {Object object = LoadfroMprivatefile (context, key);            If (object instanceof arraylist<?>) {try {return (arraylist<t>) object;    } catch (Exception e) {}} return null; /** * Load Array list * @param context * @param key value (file name) * @param clazzoft class type * @return */@Sup Presswarnings ("unchecked") public static <T> arraylist<t> LoadArrayList2 (context context, String key, Class        <T> clazzoft) {arraylist<t> result = null;        Object object = Loadentityobject (context, key, Object.class);            If (object instanceof arraylist<?>) {result = new arraylist<t> ();            arraylist<?> list = (arraylist<?>) object;                try {final String className = Clazzoft. GetName (); for (Object item:list) {if (item. getclass (). GetName (). Equals (ClassName)) {R Esult. Add ((T) item);                   }}} catch (Exception e) {e.printstacktrace ();    }} return result; }

The Loadarraylist method is the wrong implementation, and the following loadArrayList2 is the correct implementation.

Cause analysis: Generic type information is discarded at run time, exactly called erase (Erasure), only at compile time to play the role of grammar check. The initial Loadarraylist method simply checks the list type and does not check the type of the elements in the list, so it is not rigorous.

Problems with run-time type checking for Java generic functions

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.