In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of the process of obtaining the attribute value of object VO by Java reflection through the Getter method. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
For example, give you a List and ask you to iterate through the properties of the object in this List, and the object in this List is not fixed. For example, this time User, next time may be Company.
E.g. This time I need to do a utility class exported by Excel. The exported batch data is passed in as List, and the objects in List are naturally different each time, depending on what information needs to be exported.
For ease of use, the property names and values of the object are stored in Map, and you can directly traverse the Map when you use it.
The idea this time is to get the value through reflection and Getter methods and record it in a Map.
Kick start...
The property name and value of the object are stored in Map in the form of key,value, while value does not want to exist as a single type (such as String) (because multiple types are involved), so use a custom class of FieldEntity (this class contains attribute name, attribute value, attribute value type, etc.)
FieldEntity
Package com.nicchagil.util.fields;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class FieldsCollector {public static Map getFileds (Object object) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {Class clazz = object.getClass (); Field [] fields = clazz.getDeclaredFields (); Map map = new HashMap (); for (int I = 0; I
< fields.length; i++) { Object resultObject = invokeMethod(object, fields[i].getName(), null); map.put(fields[i].getName(), new FieldEntity(fields[i].getName(), resultObject, fields[i].getType())); } return map; } public static Object invokeMethod(Object owner, String fieldname, Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class ownerClass = owner.getClass(); Method method = null; method = ownerClass.getMethod(GetterUtil.toGetter(fieldname)); Object object = null; object = method.invoke(owner); return object; }} 主类,通过这个类的静态方法获取结果Map FieldsCollector package com.nicchagil.util.fields;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class FieldsCollector { public static Map getFileds(Object object) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); Map map = new HashMap (); for (int i = 0; i < fields.length; i++) { Object resultObject = invokeMethod(object, fields[i].getName(), null); map.put(fields[i].getName(), new FieldEntity(fields[i].getName(), resultObject, fields[i].getType())); } return map; } public static Object invokeMethod(Object owner, String fieldname, Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class ownerClass = owner.getClass(); Method method = null; method = ownerClass.getMethod(GetterUtil.toGetter(fieldname)); Object object = null; object = method.invoke(owner); return object; }} 为了代码清楚些,将一些工具方法独立一下,如field name到getter name的转换方法 GetterUtil package com.nicchagil.util.fields;public class GetterUtil { /** * Get getter method name by field name * @param fieldname * @return */ public static String toGetter(String fieldname) { if (fieldname == null || fieldname.length() == 0) { return null; } /* If the second char is upper, make 'get' + field name as getter name. For example, eBlog ->GeteBlog * / if (fieldname.length () > 2) {String second = fieldname.substring (1,2); if (second.equals (second.toUpperCase () {return new StringBuffer ("get") .append (fieldname). ToString ();}} / * Common situation * / fieldname = new StringBuffer ("get") .append (fieldname.substring (0,1). ToUpperCase ()) .append (fieldname.substring (1)). ToString (); return fieldname;}}
It's done!
Now, write a VO as the simulation data
User
Import java.util.Date;public class User {private String username; private String password; private String eBlog; private Date registrationDate; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} public String geteBlog () {return eBlog;} public void seteBlog (String eBlog) {this.eBlog = eBlog;} public Date getRegistrationDate () {return registrationDate } public void setRegistrationDate (Date registrationDate) {this.registrationDate = registrationDate;}}
Finally, test the class, which calls FieldsCollector~~ directly
Call
Import java.util.Date;import java.util.Map;import com.nicchagil.util.fields.FieldEntity;import com.nicchagil.util.fields.FieldsCollector;public class Call {public static void main (String [] args) throws Exception {User user = new User (); user.setUsername ("user109"); user.setPassword ("pwd109"); user.seteBlog ("http://www.cnblogs.com/nick-huang/"); user.setRegistrationDate (new Date ()); Map map = FieldsCollector.getFileds (user); System.out.println (map)) }}
Oh year, it's successful.
This is the end of the article on "sample analysis of the process of Java reflection getting the attribute value of object VO through the Getter method". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.