Understanding the nature of generics through Java reflection

Source: Internet
Author: User

The collection framework often uses generics to specify the type of elements that are stored in the collection, to guarantee the unity of the collection, to remove elements from the collection, and to avoid type casts, so we use the usual way to store elements in and out of the collection, and if we specify generics, we can only add objects of the generic type to the collection. , if you do not specify a generic, you can add any type of object to the collection because the default element is the object class, and the type cast is required when it is removed, as in the following code:

 1  ArrayList list = new   ArrayList ();  2  list.add (1    3  List.add ("s"); //  Insert object type  4  system.out.println (list);  5  6  arraylist<string> list1 = new  arraylist<string> ();  7  List1.add ("s"     8  List1.add (2); //  This is the wrong  

This will show the difference between generics, the last line of code will be the compiler error, the following to get the list and List1 class type, and compare:

1 Class c1 = list.getclass (); 2 Class c2 = list1.getclass (); 3 System.out.println (c1 = = C2);

  Because list and list1 belong to two different objects, we infer that C1 and C2 are also unequal two class types, but actually the result output is true, because the reflection gets to the class type equivalent to the execution phase of the bytecode, then C1 and C2 certainly belong to the execution phase of the comparison, So we get the conclusion that the generic of the set after compilation is de-generalized, all the class types of the collection classes are equal, generics do not exist, generics are just the type of the constraint element at compile time, only valid at compile stage, so we can use the principle of reflection, bypass the compilation, Allows List1 to store different types of elements:

1         Try {2Method m = C2.getmethod ("Add", Object.class);3M.invoke (List1, 100);//take advantage of reflection to bypass the compiled operation during the run phase4 System.out.println (List1.size ());5 System.out.println (list1);6             //You cannot use foreach to traverse7             //Traverse with iterator8Iterator it =list1.iterator ();9              while(It.hasnext ()) {TenObject obj1 =It.next (); One System.out.println (obj1); A             } -             //Use for traversal -              for(inti = 0;i < List1.size (); i++) { theObject Obj2 =List1.get (i); - System.out.println (OBJ2); -             } -}Catch(Nosuchmethodexception |SecurityException e) { + e.printstacktrace (); -}Catch(illegalaccessexception e) { + e.printstacktrace (); A}Catch(IllegalArgumentException e) { at e.printstacktrace (); -}Catch(InvocationTargetException e) { - e.printstacktrace (); -}

The above code obtains the method object through the Getmothod method, and then executes the method through the Invoke method, so that different elements can be stored in a collection with generics, which bypasses the compilation limit by using reflection, because it is not possible to determine whether the specified method exists and therefore throws an exception While traversing, we can traverse with the iterator iterator or for loop, but cannot traverse with foreach because of the reason of the type inconsistency

Understanding the nature of generics through Java reflection

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.