Java reflection, ReflectUtils, javareflectutils
1 public class ReflectUtils {2/** 3 * instantiate object 4 * @ param className class full path name 5 * @ param parameterTypes parameter type 6 * @ param initargs parameter value 7 * @ return 8 */9 @ SuppressWarnings ("rawtypes ") 10 public static Object constructorNewInstance (String className, Class [] parameterTypes, Object [] initargs) {11 try {12 Constructor <?> Constructor = (Constructor <?>) Class 13. forName (className ). getDeclaredConstructor (parameterTypes); 14 constructor. setAccessible (true); 15 return constructor. newInstance (initargs); 16} catch (Exception ex) {17 throw new RuntimeException (); 18} 19 20} 21 22 23/** 24 * Get field value 25 * @ param propertyName attribute name 26 * @ param object instance object 27 * @ return field value 28 */29 public static Object getProperty (String propertyName, object) {30 Try {31 32 PropertyDescriptor pd = new PropertyDescriptor (propertyName, object. getClass (); 33 Method method = pd. getReadMethod (); 34 return method. invoke (object); 35} catch (Exception ex) {36 throw new RuntimeException (); 37} 38} 39 40/** 41 * obtain the field value through reflection in the BeanUtils toolkit, note that this value exists as a string. It supports attribute concatenation operations: for example ,. object. property 42 * @ param propertyName property name 43 * @ param object instance object 44 * @ return field value 45 */46 public stati C Object getBeanInfoProperty (String propertyName, Object object) {47 try {48 return BeanUtils. getProperty (object, propertyName); 49} catch (Exception ex) {50 throw new RuntimeException (); 51} 52} 53 54/** 55 * obtain the field value through the BeanUtils toolkit for reflection, note that this value is a string of 56 * @ param object instance object 57 * @ param propertyName attribute name 58 * @ param value Field value 59 * @ return 60 */61 public static void setBeanInfoProperty (Object o Bject, String propertyName, String value) {62 try {63 BeanUtils. setProperty (object, propertyName, value); 64} catch (Exception ex) {65 throw new RuntimeException (); 66} 67} 68 69/** 70 * obtain the field value through the BeanUtils toolkit for reflection, note that this value is based on the actual type of the object Property 71 * @ param propertyName attribute name 72 * @ param Object instance object 73 * @ return field value 74 */75 public static Object getPropertyUtilByName (String propertyName, object object) {76 try {77 return PropertyUtils. getProperty (object, propertyName); 78} catch (Exception ex) {79 throw new RuntimeException (); 80} 81} 82 83/** 84 * obtain the field value through the BeanUtils toolkit for reflection. Note that this value is of the actual type of the object property, this is the fundamental difference between PropertyUtils and BeanUtils 85 * @ param object instance object 86 * @ param propertyName attribute name 87 * @ param value Field value 88 * @ return 89 */90 public static void setPropertyUtilByName (Object object, string propertyName, Object value) {91 try {92 PropertyUtils. setProperty (object, propertyName, value); 93} catch (Exception ex) {94 throw new RuntimeException (); 95} 96} 97 98/** 99 * set the field value 100 * @ param obj Instance Object 101 * @ param propertyName attribute name 102 * @ param value new field value 103 *@ return 104 */105 public static void setProperties (Object object, string propertyName, Object value) throws IntrospectionException, 106 IllegalAccessExcep Tion, InvocationTargetException {107 PropertyDescriptor pd = new PropertyDescriptor (propertyName, object. getClass (); 108 Method methodSet = pd. getWriteMethod (); 109 methodSet. invoke (object, value ); 110} 111 112 113/** 114 * set the field value 115 * @ param className class full path name 116 * @ param methodName call method name 117 * @ param parameterTypes parameter type 118 * @ param values parameter value 119 * @ param object instance object 120 * @ return 121 */122 @ SuppressWarnings ("rawtypes") 123 public static Object methodInvoke (String className, String methodName, Class [] parameterTypes, Object [] values, Object object Object) {124 try {125 Method method = Class. forName (className ). getDeclaredMethod (methodName, parameterTypes); 126 method. setAccessible (true); 127 return method. invoke (object, values); 128} catch (Exception ex) {129 throw new RuntimeException (); 13 0} 131} 132 133/** 134 * @ param <T> a specific object 135 * @ param fileds needs to compare the set of Bean object property values (using the property value as the key, the attribute is commented as value, and the set is extracted from the database) 136 * @ param oldBean source object 137 * @ param newBean new object 138 * @ return returns the similarities and differences between the two Bean object attribute values 139 */140 @ SuppressWarnings ("unused ") 141 public static <T> String compareBeanValue (Map <String, String> fileds, T oldBean, T newBean) {142 143 StringBuilder compares = new StringBuilder (); 144 String propertyN Ame = null; 145 Object oldPropertyValue = null; 146 Object newPropertyValue = null; 147 148 StringBuilder descrips = new StringBuilder (); 149 for (Map. entry <String, String> entity: fileds. entrySet () {150 propertyName = entity. getKey (). toLowerCase (); 151 oldPropertyValue = getProperty (propertyName, oldBean); 152 newPropertyValue = getProperty (propertyName, newBean); 153 154 if (null = oldPropertyV Alue & null = newPropertyValue) {155 continue; 156} 157 if ("". equals (oldPropertyValue )&&"". equals (newPropertyValue) {158 continue; 159} 160 if (null = oldPropertyValue) {161 oldPropertyValue = ""; 162} 163 if (null = newPropertyValue) {164 newPropertyValue = ""; 165} 166 167 if (oldPropertyValue. equals (newPropertyValue) {168 continue; 169} 170 compares. append ("field comment :"). append (entity. getV Alue ()). append ("]"). append ("original property value \" "); 171 if (StringUtils. isEmpty (oldPropertyValue + "") {172 oldPropertyValue = ""; 173} 174 compares. append (oldPropertyValue ). append ("\" current property value \ ""); 175 if (StringUtils. isEmpty (newPropertyValue + "") {176 newPropertyValue = ""; 177} 178 compares. append (newPropertyValue ). append ("\"; "); 179} 180 return compares. toString (); 181} 182 183 184/*** 185 * obtain the field value of brute force reflection 186 *@ Param obj Instance Object 187 * @ param fieldName attribute name 188 * @ return attribute value 189 */190 public static Object getFieldValue (Object obj, String fieldName) {191 if (obj = null) {192 return null; 193} 194 Field targetField = getTargetField (obj. getClass (), fieldName); 195 196 try {197 return FieldUtils. readField (targetField, obj, true); 198} catch (IllegalAccessException e) {199 e. printStackTrace (); 200} 201 retu Rn null; 202} 203 204 public static Field getTargetField (Class <?> TargetClass, String fieldName) {205 Field field = null; 206 207 try {208 if (targetClass = null) {209 return field; 210} 211 212 if (Object. class. equals (targetClass) {213 return field; 214} 215 216 field = FieldUtils. getDeclaredField (targetClass, fieldName, true); 217 if (field = null) {218 field = getTargetField (targetClass. getSuperclass (), fieldName); 219} 220} catch (Exception e) {221} 222 return field; 224} 225 226/** 227 * set the field value 228 * @ param propertyName field name 229 * @ param obj Instance Object 230 * @ param value new field value 231 * @ return 232*/233 public static void setFieldValue (Object obj, string fieldName, Object value) {234 if (null = obj) {return;} 235 Field targetField = getTargetField (obj. getClass (), fieldName); 236 try {237 FieldUtils. writeField (targetField, obj, value); 238} catch (IllegalAccessException e) {239 e. printStackTrace (); 240} 241}