Java反射根据不同方法名动态调用不同的方法(实例)
list页面的字段要求可以根据用户的喜好进行排序,所以每个用户的字段都对应着不同的顺序(字段顺序存数据库),我们从数据库里取出来的值是对象,但是前台传值是用的ajax和json array,所以就面临着一个对象到json的转换问题:1. 每个用户的字段顺序不固定,代码不能写死, 2. 根据用户字段顺序去取值,如果用if判断每个值然后调用不同的方法,if条件语句太多。然后就看了下反射。
Model 类,跟正常model一样
public class Person { private String name; private int age; private String address; private String phoneNumber; private String sex; public String getName() { return name; } // 以下是get 和set方法,省略。 }
测试类
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class Test { // init person object. private Person initPerson() { Person p = new Person(); p.setName("name"); p.setAge(21); p.setAddress("this is my addrss"); p.setPhoneNumber("12312312312"); p.setSex("f"); return p; } public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Test test = new Test(); Person p = test.initPerson(); List<String> list = new ArrayList<String>(); // Add all get method. // There is no ‘()' of methods name. list.add("getName"); list.add("getAge"); list.add("getAddress"); list.add("getPhoneNumber"); list.add("getSex"); for (String str : list) { // Get method instance. first param is method name and second param is param type. // Because Java exits the same method of different params, only method name and param type can confirm a method. Method method = p.getClass().getMethod(str, new Class[0]); // First param of invoke method is the object who calls this method. // Second param is the param. System.out.println(str + "(): Get Value is " + method.invoke(p, new Object[0])); } } }
样就可以根据数据库获取的字段遍历从对象去取相应的值了
上面那个方法是要给list添加get方法名,才能根据相应的get方法名去获取值,如果前台传过来的只是一个属性名,那我们还要转换成相应的get方法,麻烦。
public static void getValueByProperty(Person p, String propertyName) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { // get property by the argument propertyName. PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass()); Method method = pd.getReadMethod(); Object o = method.invoke(p); System.out.println("propertyName: " + propertyName + "\t value is: " + o); } public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException { Test test = new Test(); Person p = test.initPerson(); // get all properties. Field[] fields = p.getClass().getDeclaredFields(); for (Field field : fields) { getValueByProperty(p, field.getName()); } }
这样就能直接通过传过来的propertyName获取对应的value值了
以上这篇Java反射根据不同方法名动态调用不同的方法(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
利用java+mysql递归实现拼接树形JSON列表的方法示例
这篇文章主要给大家介绍了关于利用java+mysql递归实现拼接树形JSON列表的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。2017-08-08mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
今天小编就为大家分享一篇关于mybatis查询出错解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-12-12使用java springboot设计实现的图书管理系统(建议收藏)
这篇文章主要介绍了使用java springboot设计实现的图书管理系统,包含了整个的开发过程,以及过程中遇到的问题和解决方法,对大家的学习和工作具有借鉴意义,建议收藏一下2021-08-08Spring Boot 深入分析AutoConfigurationImportFilter自动化条件
这篇文章主要分析了Spring Boot AutoConfigurationImportFilter自动化条件配置源码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧2022-07-07
最新评论