Catalog
Difference between tool class and tool method:
- Tool method is applicable to set and get the same object less times
- Tool class is applicable to set and get the same object more times
Both methods have their own exception handling mechanism, which will not affect the subsequent operation of the code. An exception in setter method is equivalent to an invalid statement, and an exception in getter method returns a null value
Note: the following methods called by reflection ignore private permission, but can only be implemented by this class
Tool method ------ reflection calling setter
usage method:
Person person= new Person (); reflectSetter(person, "id", 1);
java code:
/** * @param obj Object to be assigned * @param attr Property name of the object * @param value Assigned value */ public static void reflectSetter(Object obj, String attr, Object value) { //Get object class Class cls = obj.getClass(); //Get method set Method[] methods = cls.getDeclaredMethods(); Method setter = null; String setterRegex = "^set" + attr.toLowerCase(); //Find and execute setter methods for (Method x : methods) { String metdName = x.getName().toLowerCase(); if (metdName.matches(setterRegex)) { setter = x; } } if (setter == null) System.err.println("No corresponding property name found setter"); else{ boolean flag = setter.isAccessible(); try { setter.setAccessible(true); setter.invoke(obj, value); } catch (Exception e) { System.err.println("Failed to set property value(May be value type mismatch)"); e.printStackTrace(); }finally { setter.setAccessible(flag); } } }
Tool method ------ reflection calls getter
usage method:
Person person = new Person (); Integer value = reflectGetter(person, "id",Integer.class);
java code:
/** * @param obj Object whose value needs to be obtained * @param attr Property name of the object * @param valueType Type of property value */ public static <T>T reflectGetter(Object obj, String attr, Class<T> valueType) { //Get object class Class cls = obj.getClass(); //Get method set Method[] methods = cls.getDeclaredMethods(); Method getter = null; String getterRegex = "^get" + attr.toLowerCase(); //Find and execute getter methods for (Method x : methods) { String metdName = x.getName().toLowerCase(); if (metdName.matches(getterRegex)) { getter = x; } } T value=null; if (getter == null) System.err.println("No corresponding property name found getter(Return null)"); else{ boolean flag = getter.isAccessible(); try { getter.setAccessible(true); value= valueType.cast(getter.invoke(obj)); } catch (ClassCastException e) { e.printStackTrace(); System.err.println("Incorrect type of property value(Return null)"); } catch (Exception e) { e.printStackTrace(); System.err.println("Failed to get property value(Return null)"); }finally { getter.setAccessible(flag); } } return value; }
Tool class ------ reflection calls getter and setter
usage method:
Person person= new Person (); ReflectTool handler = ReflectTool.getReflectHandler(person);//Get access object handler.setter("ID", 12);//setter Integer n = handler.getter("ID", Integer.class);//getter
java code:
public class ReflectTool { private Class cls; private Object obj; private Map<String, Method> getterMethods; private Map<String, Method> setterMethods; public static ReflectTool reflectToolFactory(Object obj) { return new ReflectTool(obj); } private ReflectTool(Object obj) { this.obj = obj; this.cls = obj.getClass(); this.getterMethods = new HashMap<>(); this.setterMethods = new HashMap<>(); //Get method set Method[] methods = cls.getDeclaredMethods(); String setterRegex = "^set[a-zA-Z_$0-9]+"; String getterRegex = "^get[a-zA-Z_$0-9]+"; //Initializing getter and setter method sets for (Method x : methods) { boolean flag=x.isAccessible(); x.setAccessible(true); String metdName = x.getName().toLowerCase(); if (metdName.matches(setterRegex)) { String attrName = metdName.replaceFirst("set", ""); this.setterMethods.put(attrName, x); } else if (metdName.matches(getterRegex)) { String attrName = metdName.replaceFirst("get", ""); this.getterMethods.put(attrName, x); } x.setAccessible(flag); } } /** * @param attr Property name of the object * @param value Assigned value */ public void setter(String attr, Object value) { Method setter = setterMethods.get(attr.toLowerCase()); if (setter == null) System.err.println("No corresponding property name found setter"); else { boolean flag = setter.isAccessible(); try { setter.setAccessible(true); setter.invoke(obj, value); } catch (Exception e) { System.err.println("Failed to set property value(May be value type mismatch)"); e.printStackTrace(); } finally { setter.setAccessible(flag); } } } /** * @param attr Property name of the object * @param valueType Type of property value */ public <T> T getter(String attr, Class<T> valueType) { Method getter = getterMethods.get(attr.toLowerCase()); T value = null; if (getter == null) System.err.println("No corresponding property name found getter(Return null)"); else { boolean flag = getter.isAccessible(); try { getter.setAccessible(true); value = valueType.cast(getter.invoke(obj)); } catch (ClassCastException e) { e.printStackTrace(); System.err.println("Incorrect type of property value(Return null)"); } catch (Exception e) { e.printStackTrace(); System.err.println("Failed to get property value(Return null)"); } finally { getter.setAccessible(flag); } } return value; } }