01: package org.jicengine.operation;
02:
03: /**
04: *
05: * <p>
06: * Copyright (C) 2004 Timo Laitinen
07: * </p>
08: * @author .timo
09: */
10:
11: public class FieldValueOperation implements Operation {
12:
13: private Operation actor;
14: private String field;
15: private String signature;
16:
17: public FieldValueOperation(String signature, Operation actor,
18: String field) {
19: this .signature = signature;
20: this .actor = actor;
21: this .field = field;
22: }
23:
24: public boolean needsParameters() {
25: return this .actor.needsParameters();
26: }
27:
28: public boolean needsParameter(String name) {
29: return this .actor.needsParameter(name);
30: }
31:
32: public Object execute(Context context) throws OperationException {
33: Object actorObject = this .actor.execute(context);
34:
35: try {
36: if (actorObject instanceof Class) {
37: // invoke a static method.
38: return ReflectionUtils.getFieldValue(null,
39: (Class) actorObject, this .field);
40: } else {
41: // invoke a method of the actor-instance.
42: return ReflectionUtils.getFieldValue(actorObject,
43: actorObject.getClass(), this .field);
44: }
45: } catch (java.lang.reflect.InvocationTargetException e) {
46: // InvocationTargetException means that the invoked method threw an exception
47: // catch it and throw a CDLElementException that has the correct application-exception..
48: throw new OperationException("'" + toString()
49: + "' resulted in exception", e.getTargetException());
50: } catch (Exception e2) {
51: throw new OperationException("'" + toString() + "': "
52: + e2.getMessage(), e2);
53: }
54: }
55:
56: public String toString() {
57: return this.signature;
58: }
59: }
|