Search in sources :

Example 1 with MethodObject

use of org.apache.ofbiz.minilang.method.MethodObject in project ofbiz-framework by apache.

the class CallObjectMethod method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    Object methodObject = this.objFieldFma.get(methodContext.getEnvMap());
    if (methodObject == null) {
        throw new MiniLangRuntimeException("Object not found: " + this.objFieldFma, this);
    }
    MiniLangUtil.callMethod(this, methodContext, parameters, methodObject.getClass(), methodObject, methodName, retFieldFma);
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) StringObject(org.apache.ofbiz.minilang.method.StringObject) FieldObject(org.apache.ofbiz.minilang.method.FieldObject) MethodObject(org.apache.ofbiz.minilang.method.MethodObject)

Example 2 with MethodObject

use of org.apache.ofbiz.minilang.method.MethodObject in project ofbiz-framework by apache.

the class MiniLangUtil method callMethod.

/**
 * Calls an object method.
 * @param operation A reference to the <code>MethodOperation</code> calling this method
 * @param methodContext
 * @param parameters
 * @param methodClass
 * @param methodObject
 * @param methodName
 * @param retFieldFma
 * @throws MiniLangRuntimeException
 */
public static void callMethod(MethodOperation operation, MethodContext methodContext, List<MethodObject<?>> parameters, Class<?> methodClass, Object methodObject, String methodName, FlexibleMapAccessor<Object> retFieldFma) throws MiniLangRuntimeException {
    Object[] args = null;
    Class<?>[] parameterTypes = null;
    if (parameters != null) {
        args = new Object[parameters.size()];
        parameterTypes = new Class<?>[parameters.size()];
        int i = 0;
        for (MethodObject<?> methodObjectDef : parameters) {
            args[i] = methodObjectDef.getObject(methodContext);
            Class<?> typeClass = null;
            try {
                typeClass = methodObjectDef.getTypeClass(methodContext);
            } catch (ClassNotFoundException e) {
                throw new MiniLangRuntimeException(e, operation);
            }
            parameterTypes[i] = typeClass;
            i++;
        }
    }
    try {
        Method method = methodClass.getMethod(methodName, parameterTypes);
        Object retValue = method.invoke(methodObject, args);
        if (!retFieldFma.isEmpty()) {
            retFieldFma.put(methodContext.getEnvMap(), retValue);
        }
    } catch (Exception e) {
        throw new MiniLangRuntimeException(e, operation);
    }
}
Also used : MethodObject(org.apache.ofbiz.minilang.method.MethodObject) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Example 3 with MethodObject

use of org.apache.ofbiz.minilang.method.MethodObject in project ofbiz-framework by apache.

the class CreateObject method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    Object[] args = null;
    Class<?>[] parameterTypes = null;
    if (parameters != null) {
        args = new Object[parameters.size()];
        parameterTypes = new Class<?>[parameters.size()];
        int i = 0;
        for (MethodObject<?> methodObjectDef : parameters) {
            args[i] = methodObjectDef.getObject(methodContext);
            Class<?> typeClass = null;
            try {
                typeClass = methodObjectDef.getTypeClass(methodContext);
            } catch (ClassNotFoundException e) {
                String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]";
                Debug.logWarning(e, errMsg, module);
                simpleMethod.addErrorMessage(methodContext, errMsg);
                return false;
            }
            parameterTypes[i] = typeClass;
            i++;
        }
    }
    try {
        Constructor<?> constructor = targetClass.getConstructor(parameterTypes);
        fieldFma.put(methodContext.getEnvMap(), constructor.newInstance(args));
    } catch (Exception e) {
        throw new MiniLangRuntimeException(e, this);
    }
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) StringObject(org.apache.ofbiz.minilang.method.StringObject) FieldObject(org.apache.ofbiz.minilang.method.FieldObject) MethodObject(org.apache.ofbiz.minilang.method.MethodObject) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Aggregations

MethodObject (org.apache.ofbiz.minilang.method.MethodObject)3 MiniLangRuntimeException (org.apache.ofbiz.minilang.MiniLangRuntimeException)2 FieldObject (org.apache.ofbiz.minilang.method.FieldObject)2 StringObject (org.apache.ofbiz.minilang.method.StringObject)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 MiniLangException (org.apache.ofbiz.minilang.MiniLangException)1