KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > operation > FieldValueOperation


1 package org.jicengine.operation;
2
3
4 /**
5  *
6  * <p>
7  * Copyright (C) 2004 Timo Laitinen
8  * </p>
9  * @author .timo
10  */

11
12 public class FieldValueOperation implements Operation {
13
14   
15     private Operation actor;
16     private String JavaDoc field;
17     private String JavaDoc signature;
18
19     public FieldValueOperation(String JavaDoc signature, Operation actor, String JavaDoc field)
20     {
21         this.signature = signature;
22         this.actor = actor;
23         this.field = field;
24     }
25
26     public boolean needsParameters()
27     {
28         return this.actor.needsParameters();
29     }
30
31     public boolean needsParameter(String JavaDoc name)
32     {
33         return this.actor.needsParameter(name);
34     }
35
36     public Object JavaDoc execute(Context context) throws OperationException
37     {
38         Object JavaDoc actorObject = this.actor.execute(context);
39
40         try {
41             if( actorObject instanceof Class JavaDoc ){
42                 // invoke a static method.
43
return ReflectionUtils.getFieldValue(null, (Class JavaDoc)actorObject, this.field);
44             }
45             else {
46                 // invoke a method of the actor-instance.
47
return ReflectionUtils.getFieldValue(actorObject, actorObject.getClass(), this.field);
48             }
49         } catch (java.lang.reflect.InvocationTargetException JavaDoc e){
50             // InvocationTargetException means that the invoked method threw an exception
51
// catch it and throw a CDLElementException that has the correct application-exception..
52
throw new OperationException("'" + toString() + "' resulted in exception", e.getTargetException());
53         } catch (Exception JavaDoc e2){
54             throw new OperationException("'" + toString() + "': " + e2.getMessage(), e2);
55         }
56     }
57
58     public String JavaDoc toString()
59     {
60         return this.signature;
61     }
62 }
63
Popular Tags