Java Reflection mechanism

Source: Internet
Author: User

Tag: Val new uses the attribute obj via Exce + + attr

Recently working on a project.

The requirement is that the front end passes a parameter param represents the parameters to be fetched from the server, and the backend needs to take the corresponding parameters out of the server.

I thought it was nothing, it should be quite simple. Results a look at the table, well, more than 40 parameters, if with if...else ... or switch to write an estimate to write me dead.

So I think there is no such method, you can automatically call the corresponding GetParam method through the incoming Param.

So I think of the reflection mechanism of java.

Reflection mechanism is in the running state, for any class, can know all the properties and methods of the class;

Any one of the objects can be called any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language. (Copy the words from someone else's blog).

In this case, I use Java's reflection mechanism to dynamically invoke the object's method based on the dynamic Param.

Words don't say much, here is my code.

Jsonobject JSON =NewJsonobject (); Field[] Fields=Msgparam.getclass (). Getdeclaredfields (); Try {                     for(inti=0;i<fields.length;i++) {String AttributeName= Fields[i].getname ();//Get Property nameString attributenameupper = attributename.substring (0,1). toUpperCase () +attributename.substring (1);//attribute name Capitalize first letter//String attributetype = Fields[i].getgenerictype (). toString ();//Get Property Type                        if(Attributename.tolowercase (). Equals (param)) {//if the property name and the name of the incoming parameter in the URL are the sameMethod method = Msgparam.getclass (). GetMethod ("get" +attributenameupper);//getting the Get methodString value = (string) method.invoke (Msgparam);//call the Get methodJson.element (attributename, value);//                        }                        }                    returnJSON; } Catch(Exception e) {//Todo:handle ExceptionJson.element ("Error", "Unknown");//JSONJson.element ("msg", "Unknown Error"); returnJSON; }

A bunch of code simplification can be like this, happy, hehe

Java Reflection mechanism

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.