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;
}
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);
}
}
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;
}
Aggregations