package com.tojoy.customer.behaviorcollector.utils;
import com.tojoy.customer.behaviorcollector.domain.BehaviorMessage;
import java.lang.reflect.Field;public class BehaviorMessageUtil {
public static BehaviorMessage behaviorMessageChange(BehaviorMessage behaviorMessage){ Field[] declaredFields = behaviorMessage.getClass().getDeclaredFields(); for (int i =0;i<declaredFields.length;i++){ Field declaredField = declaredFields[i]; declaredField.setAccessible(true); // String name = declaredField.getName(); try { Object object = declaredField.get(behaviorMessage); if (object == null){ declaredField.set(behaviorMessage,""); } } catch (IllegalAccessException e) { e.printStackTrace(); } } return behaviorMessage; } public static void main(String[] args){ BehaviorMessage behaviorMessage = new BehaviorMessage(); behaviorMessage.setEsId("1"); behaviorMessage.setBeiyong13("3213"); BehaviorMessage behaviorMessage1 = behaviorMessageChange(behaviorMessage); System.out.println(behaviorMessage1); } }
java 反射取值和重新赋值
最新推荐文章于 2023-01-04 20:06:05 发布