How groovy explores the simplification of Java language reflection

Source: Internet
Author: User
Tags reflection

The reflection function of the Java language is the function that we have to use, but in fact its use is rather tedious but functionally more single function.

For example, the most common reflection we use in the Java language is the invocation of the "set" and "get" methods of the Pojo object or domain object at run time because the object properties are private, and the values of the object properties are obtained and set by the corresponding "gets" and "set" methods. The following is an example of a "get" method call:

public static Object getFieldValue(Object bean,Field field)
{
try
{
String type = field.getType().getName();
if(type.equals("boolean")||type.equals("java.lang.Boolean"))
{
Method m1 = bean.getClass().getMethod(StrUtil.getIsMethodName(field.getName()),null);
return m1.invoke(bean, null);
}
else
{
Method m1 = bean.getClass().getMethod(StrUtil.getGetMethodName(field.getName()),null);
return m1.invoke(bean, null);
}
}
catch(Exception e)
{
e.printStackTrace();
if(logger.isDebugEnabled())
{
logger.debug("getFieldValue", e);
}
returnnull;
}
}

The code above first determines whether the property of the field is Boolean and, if so, calls the Isxxxx () method, otherwise the Getxxxx () method is invoked.

At the time of the call, the name of the field first gets the corresponding "is" or "get" method name, and then the methods object is obtained, and finally the Invoke method of the object is invoked to get the return value.

The whole process is quite tedious.

But in the groovy language, we set or get property values for an object that can be obtained directly from the object name. property name, so setting and getting the value of the object property does not go directly through the "set" and "get" methods.

The above Java code can be transformed into the following code in a groovy program:

defstatic getFieldValue(Object bean,String fieldName)
{
bean."$fieldName"
}

Yes, in the previous article, I have suggested that the dynamics of groovy language has a lot to do with gstring, and now you can see that dynamically setting or getting object property values is done by Gstring objects.

It's incredibly simple, if you have a domain class like this:

class Man
{
String name
String age
String addr
}

We use this class for testing:

Man man = new Man()
man.name = 'Mike'
man.age = '22'
man.addr = 'Shenzhen'
println getFieldValue(man,'age')

Printing results are:

22

To do one more test:

Man man = new Man()
man.name = 'Mike'
man.age = '22'
man.addr = 'Shenzhen'
man.metaClass.properties.each
{
println"property name: ${it.name}, property value: ${man."${it.name}"}"
}

The results are:

property name: class, property value: class base.Man
property name: addr, property value: Shenzhen
property name: age, property value: 22
property name: metaClass, property value: groovy.lang.MetaClassImpl@1749757[class base.Man]
property name: name, property value: Mike

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.