OperatorSelfDefineServiceFunction.java 3.8 KB
Newer Older
T
update  
tianqiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
package com.ql.util.express.instruction.op;

import java.lang.reflect.Method;

import com.ql.util.express.*;
import com.ql.util.express.instruction.OperateDataCacheManager;

/**
 * 用户自定义的服务函数操作
 * @author qhlhl2010@gmail.com
 *
 */
public class OperatorSelfDefineServiceFunction extends OperatorBase implements CanClone{
  Object serviceObject;
  String functionName;
  String[] parameterTypes;
  Class<?>[] parameterClasses ;
  transient Method method;
  boolean isReturnVoid;
  boolean maybeDynamicParams;
  
	public OperatorSelfDefineServiceFunction(String aOperName,
			Object aServiceObject, String aFunctionName,
			Class<?>[] aParameterClassTypes,String[] aParameterDesc,String[] aParameterAnnotation, String aErrorInfo) throws Exception {
		if (errorInfo != null && errorInfo.trim().length() == 0) {
			errorInfo = null;
		}
		this.name = aOperName;
		this.errorInfo = aErrorInfo;
		this.serviceObject = aServiceObject;
		this.functionName = aFunctionName;
		this.parameterClasses = aParameterClassTypes;
	    this.operDataDesc = aParameterDesc;
	    this.operDataAnnotation = aParameterAnnotation;
		this.parameterTypes = new String[this.parameterClasses.length];
		for (int i = 0; i < this.parameterClasses.length; i++) {
			this.parameterTypes[i] = this.parameterClasses[i].getName();
		}
		Class<?> operClass = serviceObject.getClass();
		method = operClass.getMethod(functionName, parameterClasses);
		this.isReturnVoid = method.getReturnType().equals(void.class);
		this.maybeDynamicParams = DynamicParamsUtil.maybeDynamicParams(parameterClasses);
	}
  
  public OperatorSelfDefineServiceFunction(String aOperName,Object aServiceObject, String aFunctionName,
                         String[] aParameterTypes,String[] aParameterDesc,String[] aParameterAnnotation,String aErrorInfo) throws Exception {
    
	if (errorInfo != null && errorInfo.trim().length() == 0) {
			errorInfo = null;
	}
	this.name = aOperName;
    this.errorInfo = aErrorInfo;
    this.serviceObject = aServiceObject;
    this.functionName = aFunctionName;
    this.parameterTypes = aParameterTypes;
    this.operDataDesc = aParameterDesc;
    this.operDataAnnotation = aParameterAnnotation;
    this.parameterClasses = new Class[this.parameterTypes.length];
    for(int i=0;i<this.parameterClasses.length;i++){
      this.parameterClasses[i] =ExpressUtil.getJavaClass(this.parameterTypes[i]);
    }
    Class<?> operClass = serviceObject.getClass();
    method = operClass.getMethod(functionName,parameterClasses);
	this.maybeDynamicParams = DynamicParamsUtil.maybeDynamicParams(parameterClasses);
  }

	public OperatorBase cloneMe(String opName, String errorInfo)
			throws Exception {
		OperatorBase result = new OperatorSelfDefineServiceFunction(opName,
				this.serviceObject, this.functionName, this.parameterClasses,
				this.operDataDesc, this.operDataAnnotation, errorInfo);
		return result;
	}
  public OperateData executeInner(InstructionSetContext context, ArraySwap list) throws Exception {
      Object[] parameres =  DynamicParamsUtil.transferDynamicParams(context, list, parameterClasses,this.maybeDynamicParams);
      Object obj = this.getMethod().invoke(this.serviceObject,ExpressUtil.transferArray(parameres,parameterClasses));
      if(obj != null){
         return OperateDataCacheManager.fetchOperateData(obj,obj.getClass());
      }
      if(this.isReturnVoid == true){
    	  return OperateDataCacheManager.fetchOperateDataAttr("null", void.class);
      }else{
    	  return OperateDataCacheManager.fetchOperateDataAttr("null", null);
      }
  }
    
    private Method getMethod() throws NoSuchMethodException {
        
        if(this.method!=null){
            return this.method;
        }
        Class<?> operClass = serviceObject.getClass();
        this.method = operClass.getMethod(functionName, parameterClasses);
        return this.method;
    }
}