KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jicengine.operation;
2
3 import java.util.List JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 /**
7  * <p>
8  * Either a method or constructor invocation.
9  * </p>
10  * <p>
11  * Copyright (C) 2004 Timo Laitinen
12  * </p>
13  * @author .timo
14  */

15
16 public abstract class InvocationOperation implements Operation {
17
18     Operation actor;
19     Operation[] parameters;
20
21     String JavaDoc signature;
22
23     public InvocationOperation(String JavaDoc signature, Operation actor, Operation[] parameters)
24     {
25         this.actor = actor;
26         this.parameters = parameters;
27         this.signature = signature;
28     }
29
30     public boolean needsParameters()
31     {
32         return this.parameters.length > 0;
33     }
34
35     public boolean needsParameter(String JavaDoc name)
36     {
37         if( this.actor.needsParameter(name) ){
38             return true;
39         }
40
41         for (int i = 0; i < this.parameters.length; i++) {
42             if( this.parameters[i].needsParameter(name) ){
43                 return true;
44             }
45         }
46
47         return false;
48     }
49
50     public Object JavaDoc execute(Context context) throws OperationException
51     {
52         Object JavaDoc actorObject = this.actor.execute(context);
53         Object JavaDoc[] arguments = evaluateParameters(this.parameters, context);
54         return execute(actorObject, arguments);
55     }
56
57     public String JavaDoc toString()
58     {
59         return this.signature;
60     }
61
62     protected abstract Object JavaDoc execute(Object JavaDoc actor, Object JavaDoc[] arguments) throws OperationException;
63
64     public static Object JavaDoc[] evaluateParameters(Operation[] parameters, Context context) throws OperationException
65     {
66         Object JavaDoc[] params = new Object JavaDoc[parameters.length];
67         for (int i = 0; i < parameters.length; i++) {
68             params[i] = parameters[i].execute(context);
69         }
70         return params;
71     }
72
73 }
74
Popular Tags