java--reflection to get object information

Source: Internet
Author: User

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanism of the Java language.

Learning Design patterns, we saw a word: "Reflection reflection, the programmer's happiness", at that time not very deep understanding of this sentence, to learn struts, spring and so on, feel the reflection is really very powerful existence.

We present today how to get information about an object through the reflection of Java. The requirement is this: there is an entity, there are multiple attributes in the entity, the property of the entity has the value of the search condition, that is, if a property has a value, then this property is a search condition, if an attribute has no value, then this property is not as a search condition. Such a method compares and, to a large extent, improves the reuse of code, so what if we get all of its properties through an entity? The answer is--reflex!

 Public List querybyentity (Object model,string cont) throws Nosuchmethodexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{map<serializable, serializable> map=new HashMap<   Serializable, serializable> ();        field[] field = Model.getclass (). Getdeclaredfields (); Gets all properties of the entity class, returns the field array for (int j=0; j<field.length; j + +) {//Traversal all properties String name = Field[j].    GetName ();                    Gets the name of the property System.out.println ("attribute name:" +name);                String Anothername=name; Name = name.substring (0,1). toUpperCase () +name.substring (1);    Capitalize the first character of the property to make it easy to construct the Get,set method String type = Field[j].getgenerictype (). toString ();                    Gets the type of the property if (Type.equals ("Class java.lang.String")) {//if type is a class type, preceded by "class", followed by the class name                    Method m = Model.getclass (). GetMethod ("get" +name);    String value = (string) m.invoke (model);                  Call getter method to get property value  if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value);                        }} if (Type.equals ("Class Java.lang.Integer")) {                    Method m = Model.getclass (). GetMethod ("get" +name);                    Integer value = (integer) m.invoke (model);                                           if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value);                        }} if (Type.equals ("Class Java.lang.Short")) {                    Method m = Model.getclass (). GetMethod ("get" +name);                    Short value = (short) m.invoke (model);                    if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value); }} if (Type.equals ("Class java.lang.Double")) {Method m = model . GetclaSS (). GetMethod ("get" +name);                    Double value = (double) m.invoke (model);                                           if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value);                    }} if (Type.equals ("Class Java.lang.Boolean")) {                       Method m = Model.getclass (). GetMethod ("get" +name);                    Boolean value = (Boolean) m.invoke (model);                                           if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value);                    }} if (Type.equals ("Class Java.util.Date")) {                                       Method m = Model.getclass (). GetMethod ("get" +name);                    Date value = (date) m.invoke (model);      if (value! = NULL) {//Put the property name and value in map; Map.put (anothername, value);                                      }                }                           }} 

with this code, we have been able to get all the non-null attributes in the entity, and have it in the map. After we get to these, we can make the search very convenient.


Summarize:

The reflection of Java is not only reflected in this point, but the reflection of Java also plays an important role in many places, because of the reflection, we can improve the flexibility of the system. As in struts, the dispatch control of the request. When the request arrives, struts finds the action for the request by querying the configuration file, then instantiates the action by reflection and invokes the response method. If reflection is not applied, then it can only be written to the code.



java--reflection to get object information

Related Article

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.