public void updateConst() throws IllegalAccessException, NoSuchFieldException {
System.err.println(“更新配置文件+++++++++++++++++++++++++++++”);
Class clazz = socketConfig.getClass();
Field[] fields = ReflectUtil.getFields(clazz);
for (int i = 0; i < fields.length; i++) { Field field = fields[i]; //判断是否存在SetValueKey注解 if (field.isAnnotationPresent(SetValueKey.class)) { //如果存在注解则进行遍历赋值 SetValueKey setValueKey = field.getAnnotation(SetValueKey.class); String key = setValueKey.value(); if (Validator.isNotEmpty(key)) { field.setAccessible(true); Object value = field.get(socketConfig); Class cclazz = SocketConst.class;
Field constaField = cclazz.getDeclaredField(key);
constaField.setAccessible(true);
//去掉final修饰
Field modifiers = constaField.getClass().getDeclaredField(“modifiers”);
modifiers.setAccessible(true);
constaField.set(cclazz, value);
//设置final
modifiers.setInt(constaField, constaField.getModifiers() & ~Modifier.FINAL);
System.err.println(constaField.getName() + “:” + constaField.get(SocketConst.class));
}
}
}
System.err.println(“更新配置文件结束+++++++++++++++++++++++++++++”);
}