JAVA-反射常用方法
类相关
Class<?> clazz = obj.getClass();
属性相关
Field[] fields = clazz.getDeclaredFields();
field.setAccessible(true);
String type = field.getGenericType().toString();
String methodName = field.getName().replaceFirst(field.getName().substring(0, 1), field.getName().substring(0, 1).toUpperCase());
Method methodGet = clazz.getMethod("get" + methodName);
String str = (String) methodGet.invoke(obj);
field.set(obj, 0.00);
注解相关
boolean isAnnotationPresent = field.isAnnotationPresent(PropertyCheck.class);
PropertyCheck propertyCheck = field.getAnnotation(PropertyCheck.class);
boolean notBlank = propertyCheck.notBlank();
String msg = propertyCheck.msg();
System.out.println(field.getName() + " notBlank = " + b + ", msg = " + msg);