Java Reflection Mechanism Series (iii) example analysis

Source: Internet
Author: User

In the previous article, we know how to use reflection mechanism to create objects, get class variables and call methods, and so on.  The statement that creates the object is the Class CLA = Class.forName (type); Object obj = cla.newinstance (); Here newinstance () is actually using the default parameterless construction method for the class. It would be a little more complicated if we were to invoke other construction methods. For example, we want to create a StringBuffer object, the new operator should be stringbuffer br = new StringBuffer ("example"), and the reflection mechanism should have the following steps.

First, get the description of the StringBuffer class.

Class CLA = Class.forName ("Java.lang.StringBuffer");

Second, to create an array of parameter types class[].

class[] Paratypes = new class[1];p Aratypes[0]=string.class;

Then, the constructor objects are obtained through CLA and Paratypes.

Constructor constructor = Cla.getconstructor (Paratypes);

Next, create the incoming argument list object[].

object[] paralists = new Object[1]; paralists[0]= "COLOR";

Finally, get what we have. Object obj = constructor.newinstance (paralists);

If we set the paratypes and paralists to null or the length is 0, we can use the above steps to invoke the StringBuffer constructor method. Similarly, we can invoke the parameter method in the object. For example, we do the following Operation Br.insert (4, ' U '), with the reflection mechanism to achieve the following.

Class[] paratypes = new Class[]{int.class,char.class};
Method method = cla.getMethod("insert", paratypes);
Object[] paralists = new Object[]{4,'u'};
method.invoke(obj, paralists);

The reflection mechanism gives us the convenience of the runtime to determine the object type, but it also has significant drawbacks.

1, the code is clumsy and tedious. such as the original sentence Br.insert (4, ' U '), can solve the problem now need to use four sentences.

2, the benefits of compile-time type checking are lost. This allows you to deal with more anomalies.

3, loss of performance. It takes longer to run with a reflection mechanism.

The advice given in <<effective Java >> is that "normal applications should not access objects in an image at run time, but only in very limited cases". So where do you use the reflex mechanism? Existing more familiar applications are our IDES and some frameworks. For example, Eclipse, the suggestion that ctrl+space pop up when programming is to use the reflection mechanism. For example, spring reads a configuration file and then generates the corresponding object. There are also RPC systems to use. For general application software, you can use it in the factory method.

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.