All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.util.module.query.instance.service.SetInstanceFieldValueFlow Maven / Gradle / Ivy

The newest version!
package cn.schoolwow.util.module.query.instance.service;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.module.query.instance.domain.InstanceQueryOption;

import java.lang.reflect.Field;

public class SetInstanceFieldValueFlow implements BusinessFlow {
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String fieldName = (String) flowContext.checkData("fieldName");
        Object fieldValue = flowContext.getData("fieldValue");
        InstanceQueryOption instanceQueryOption = (InstanceQueryOption) flowContext.checkData("instanceQueryOption");

        Class clazz = instanceQueryOption.instance.getClass();
        String initialClassName = clazz.getName();
        while(null!=clazz.getSuperclass()){
            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(instanceQueryOption.instance, fieldValue);
                return;
            } catch (NoSuchFieldException e) {
                clazz = clazz.getSuperclass();
            }
        }
        throw new IllegalArgumentException("设置字段值失败!字段不存在!字段名:"+fieldName+",类名:"+initialClassName);
    }

    public String name() {
        return "通过反射设置实例指定字段的值";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy