ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package org.jbpm.pvm.internal.wire.operation;
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
 
import org.jbpm.pvm.internal.util.ReflectUtil;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireException;
import org.jbpm.pvm.internal.wire.descriptor.ArgDescriptor;
import org.jbpm.pvm.internal.wire.descriptor.ObjectDescriptor;
 
/**
 * invokes a method.
 *
 * @author Tom Baeyens
 * @author Guillaume Porcher (documentation)
 * @see ArgDescriptor
 * @see ObjectDescriptor
 */
 
public class InvokeOperation extends AbstractOperation {
 
  private static final long serialVersionUID = 1L;
 
  /** name of the method to invoke. */
  String methodName = null;
  /** list of descriptors for creating arguments supplied to the method */
  List<ArgDescriptor> argDescriptors = null;
 
  public void apply(Object target, WireContext wireContext) {
    try {
      Object[] args = ObjectDescriptor.getArgs(wireContext, argDescriptors);
      Class<?> clazz = target.getClass();
      Method method = ReflectUtil.findMethod(clazz, methodName, argDescriptors, args);
      if (method==null) {
        throw new WireException("method "+ReflectUtil.getSignature(methodName, argDescriptors, args)+" unavailable");
      }
      ReflectUtil.invoke(method, target, args);
    } catch (WireException e) {
      throw e;
    } catch (Exception e) {
      throw new WireException("couldn't invoke method "+methodName+": "+e.getMessage(), e);
    }
  }
 
  /**
   * Adds a descriptor to the list of arguments descriptors.
   */
  public void addArgDescriptor(ArgDescriptor argDescriptor) {
    if (argDescriptors==null) {
      argDescriptors = new ArrayList<ArgDescriptor>();
    }
    argDescriptors.add(argDescriptor);
  }
 
  /**
   * Gets the name of the method to invoke.
   */
  public String getMethodName() {
    return methodName;
  }
 
  /**
   * Sets the name of the method to invoke.
   * @param methodName the name of the method to invoke.
   */
  public synchronized void setMethodName(String methodName) {
    this.methodName = methodName;
  }
 
  /**
   * Gets the list of descriptor to create arguments supplied to the method .
   */
  public List<ArgDescriptor> getArgDescriptors() {
    return argDescriptors;
  }
 
  /**
   * Sets the list of descriptor to create arguments supplied to the method .
   */
  public void setArgDescriptors(List<ArgDescriptor> argDescriptors) {
    this.argDescriptors = argDescriptors;
  }
}