声明注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conversion {
String saveColumn();
String saveProperties();
String byTable();
String showColumn();
String showProperties();
boolean saveFlag() default false;
}
声明对象
public class A07TbEquipmentMaintenance implements Serializable {
、
/**
* 设备Id(外键,关联设备资产管理台账)
*/
@Conversion(saveColumn = "tea_id", saveProperties = "tbEquipmentAssetsId",byTable="tb_equipment_assets", showColumn = "equipment_code", showProperties ="equipmentCode" )
@ApiModelProperty(value = "设备Id(外键,关联设备资产管理台账)")
private String tbEquipmentAssetsId;
private String equipmentCode;
private String teiId;
private Integer peopleNum;
private static final long serialVersionUID = 1L;
@Override
public String toString() {
return "A07TbEquipmentMaintenance{}";
}
public String toString(String str) {
return str;
}
}
使用反射,获取对象中的属性和方法
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
Class clazz = Class.forName("com.enercomn.web.A07EquipmentMaintenance.bean.A07TbEquipmentMaintenance");
//实例化对象
Object instance = clazz.newInstance();
//所有的属性 private格式的也在内
Field[] declaredFields = clazz.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field field = declaredFields[i];
if(field.isAnnotationPresent(Conversion.class)){
Conversion conversion = field.getDeclaredAnnotation(Conversion.class);
//获取对象注解中的内容
String saveColumn = conversion.saveColumn();
System.out.println(saveColumn);
}
//私密的属性打开访问权。
field.setAccessible(true);
//设置对象属性信息
if(String.valueOf(field.getType()).indexOf("String") != -1){
field.set(instance,"string"+i);
String value = String.valueOf(field.get(instance)) ;
System.out.println(value);
}
}
Method[] methods = clazz.getMethods();
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
System.out.println(method.getName());
}
String[] string ={"333333"};
Method toString1 = clazz.getMethod("toString", String.class);
System.out.println( toString1.invoke(instance,string));;
Method toString2 = clazz.getMethod("toString");
System.out.println( toString2.invoke(instance));;
}