Java uses the reflection API to optimize code

Source: Internet
Author: User
Tags class definition reflection

Summary

Developers try to avoid tedious programming in a variety of ways. Some programming rules, such as inheritance, polymorphism, or design models, can help developers avoid generating unwanted code. However, because of the uncertainties in software development, these rules do not eliminate the need for code maintenance and rewriting. Maintenance is unavoidable in many cases, and only software that cannot be operated is never required to be maintained. However, this article describes the functionality of the Java Reflection API that you can use to reduce the monotony of code writing, and you can use the active code generation to overcome reflection limitations.

Data configuration (data from external sources and loaded into a Java object) can use the benefits of reflection to create a reusable scenario. The problem is simple: Load data from one file into the field of an object. Now suppose the target Java class used as data changes once a week? There is a straightforward solution, but you have to constantly maintain the loading process to reflect any changes. In more complex environments, the same problem can crash the system. For a person who has handled a large system that uses XML, he has encountered this problem. It is often tedious to write a loading process, and you need to update and rewrite the code frequently because of changes in the data source or the target Java class. Here I want to introduce another solution, which is to use mappings, which typically use less coding and can update themselves after the target Java class has changed.

Initially, I would like to introduce a scenario that uses reflection to configure data during run time. In the beginning, a dynamic, map-based program is much more appealing than a simple method. Then, I want to reveal the complexity and risk-taking of the run-time reflection. This article will introduce the code generated by the runtime's reflection to the activity.

From simple to complex

My first scenario loads data from a file into an object using a load class. My source code contains multiple calls to the next node method of the StringTokenizer object. After many changes, my coding logic becomes very straightforward and systematic. This class constructs a specialized code. In this initial scenario, I only need to use 3 basic objects:

1, Strings

2, Objects

3, Arrays of objects

You can insinuate objects of the class to produce blocks of code, as shown in the following table:

Objects that are mapped to produce blocks of code

Field type Code Block
String Fileiterator.nextstring ();
Object[] Vector collector = new vector (); while (Fileiterator.hasmoredataforarray ()) {Object data = Initializeobject (fileiterator) collector.add (data);} object[] Objarray = new object[collector.size ()]; Collector.copyinto (Objarray);
Object Initializeobject (Fileiterator);

Table A **************

I've used this scheme several times to encode, so I already know the structure of the program and the code before I write the code. The difficulty is that the class is variable. The name, composition, and structure of a class can change at any time, and any change you have to rewrite the code. Although these changes will occur, the structure and download process are still the same; before writing the code, I still know the structure and composition of the code. I need a way to transform the coding process in my mind into a reusable, automatic form. Since I was an efficient programmer, I quickly got tired of writing almost the same code, when I thought of mapping.

Data configuration typically requires a mapping of source to destination data. Innuendo can be a diagram, a DTD (document type definition, a doc-style definitions), a file format, and so on. In this example, the mapping interprets the class definition of an object as the process we want to map. Mapping enables you to copy the functionality of your code at run time. When you need to rewrite the code, I replace the loaded process with a mapping, which takes the same time as the rewrite.

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.