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
package org.jbpm.pvm.internal.wire.operation;
 
import java.lang.reflect.Field;
 
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.util.ReflectUtil;
import org.jbpm.pvm.internal.wire.Descriptor;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireException;
 
 
/**
 * injects another object into a field.
 *
 * @author Tom Baeyens
 * @author Guillaume Porcher (documentation)
 *
 */
public class FieldOperation extends AbstractOperation {
 
  private static final long serialVersionUID = 1L;
 
  String fieldName = null;
  Descriptor descriptor = null;
 
  transient Field field = null;
 
  public FieldOperation() {
  }
 
  public void apply(Object target, WireContext wireContext) {
    if (target!=null) {
 
      // Get field
      synchronized(this) {
        if (field==null) {
          Class<?> clazz = target.getClass();
          field = ReflectUtil.findField(clazz, fieldName);
        }
      }
 
      // Create value
      Object value = wireContext.create(descriptor, true);
      // Set the field value
      try {
        ReflectUtil.set(field, target, value);
      } catch (Exception e) {
        throw new WireException("couldn't set "+fieldName+" to "+value, e);
      }
    }
  }
 
  /**
   * Gets the name of the field that should be updated by this operation.
   */
  public String getFieldName() {
    return fieldName;
  }
 
  /**
   * Sets the name of the field that should be updated by this operation.
   * @param fieldName
   */
  public synchronized void setFieldName(String fieldName) {
    this.fieldName = fieldName;
  }
 
  /**
   * Gets the descriptor used to create the field's value.
   */
  public Descriptor getDescriptor() {
    return descriptor;
  }
  /**
   * Sets the descriptor used to create the field's value
   * @param valueDescriptor
   */
  public synchronized void setDescriptor(Descriptor valueDescriptor) {
    this.descriptor = valueDescriptor;
  }
 
  private static Log log = Log.getLog(FieldOperation.class.getName());
}