Java 5 core basic generics (2): Application of generics in reflection

Source: Internet
Author: User

How does one obtain the constructor of the specified parameter type through reflection?

The Code is as follows:

Package highBasic. generic;/*** entry to generics */import java. lang. reflect. constructor; import java. lang. reflect. method; import java. util. arrayList; import java. util. collection; public class GenericTest {public static void main (String [] args) throws Exception {ArrayList collection = new ArrayList (); // Add the element collection to the list. add (1); collection. add (1L); collection. add ("abc"); int I = (Integer) collection. get (0); // int i1 = (Integer) collection. get (1); // ---- The ArrayList statement with an exception
 
  
Collection2 = new ArrayList
  
   
(); // Add an element to the list // collection2.add (1); // collection2.add (1L); // ---- the compiler reports an error, the reason is that only the string object collection2.add ("abc"); String element = collection2.get (0); System. out. println (element); // ----- application of generics in = reflection =: Get the Constructor of the specified type through reflection --------------- 1 Constructor
   
    
Constructor = String. class. getConstructor (StringBuffer. class); // after passing the generic limitation, you will be able to know that this object must be a String-Type constructor, if you do not need a generic result, // It is similar to the preceding Definition of a set object. String string2 = constructor. newInstance (new StringBuffer ("abc"); System. out. println (string2.charAt (0);/** conclusion: Where can I use generics? * You can refer to the technical documentation to see the definition of the type.
    
     
This form can be generic. * // ---- Generics work in the compiler stage and solve possible exceptions during runtime. ArrayList
     
      
Collection3 = new ArrayList
      
        (); // If you want to wait, collection2 and collection3 point to the same bytecode. System. out. println (collection2.getClass () = collection. getClass (); // The printed result is true // ------- can I add a String object to collection3 through the compiler? ---------------------------------- // Collection3.add ("abc"); ------ error Method = collection3.getClass (). getMethod ("add", Object. class); method. invoke (collection3, "abc"); System. out. println ("transmits the String object to the Integer collection object through the compiler, and the output result is:" + collection3.get (0); // output: "Through the compiler, upload the String object to the set object of the Integer type. The output result is "abc" // describes the above implementation. // The problem is, what is the purpose of this practice ----------------------------?}}
      
     
    
   
  
 


Note the following code: the constructor of the String class with the StringBuffer parameter is obtained using the reflection mechanism, and then the newInstance () of the constructor () method passed in StringBuffer type parameters to create a String object. Finally, point the String object string2 to it.

The code above successfully obtains the constructor of the String class of the specified parameter type through 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.