Search in sources :

Example 1 with TaskParam

use of org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.TaskParam in project airavata-data-lake by apache.

the class TaskUtil method deserializeTaskData.

public static <T extends AbstractTask> void deserializeTaskData(T instance, Map<String, String> params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
    List<Field> allFields = new ArrayList<>();
    Class genericClass = instance.getClass();
    while (AbstractTask.class.isAssignableFrom(genericClass)) {
        Field[] declaredFields = genericClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            allFields.add(declaredField);
        }
        genericClass = genericClass.getSuperclass();
    }
    for (Field classField : allFields) {
        TaskParam param = classField.getAnnotation(TaskParam.class);
        if (param != null) {
            if (params.containsKey(param.name())) {
                classField.setAccessible(true);
                PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, classField.getName());
                Method writeMethod = PropertyUtils.getWriteMethod(propertyDescriptor);
                Class<?>[] methodParamType = writeMethod.getParameterTypes();
                Class<?> writeParameterType = methodParamType[0];
                if (writeParameterType.isAssignableFrom(String.class)) {
                    writeMethod.invoke(instance, params.get(param.name()));
                } else if (writeParameterType.isAssignableFrom(Integer.class) || writeParameterType.isAssignableFrom(Integer.TYPE)) {
                    writeMethod.invoke(instance, Integer.parseInt(params.get(param.name())));
                } else if (writeParameterType.isAssignableFrom(Long.class) || writeParameterType.isAssignableFrom(Long.TYPE)) {
                    writeMethod.invoke(instance, Long.parseLong(params.get(param.name())));
                } else if (writeParameterType.isAssignableFrom(Boolean.class) || writeParameterType.isAssignableFrom(Boolean.TYPE)) {
                    writeMethod.invoke(instance, Boolean.parseBoolean(params.get(param.name())));
                } else if (TaskParamType.class.isAssignableFrom(writeParameterType)) {
                    Constructor<?> ctor = writeParameterType.getConstructor();
                    Object obj = ctor.newInstance();
                    ((TaskParamType) obj).deserialize(params.get(param.name()));
                    writeMethod.invoke(instance, obj);
                }
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) TaskParam(org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.TaskParam)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 TaskParam (org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.TaskParam)1