KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > MethodNut


1 package jfun.yan.xml.nuts;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import jfun.yan.Component;
6 import jfun.yan.Components;
7 import jfun.yan.Functions;
8 import jfun.yan.util.ReflectionUtil;
9 /**
10  * Nut class for <method> tag.
11  * <p>
12  * @author Ben Yu
13  * Nov 9, 2005 11:42:15 PM
14  */

15 public class MethodNut extends ArgumentsAndPropertiesNut
16 implements LifecycleDeclaration{
17   private Class JavaDoc hostclass;
18   private Component cc;
19   private String JavaDoc method_name;
20   private boolean private_access = false;
21
22   public void add(Component cn){
23     checkDuplicate("component", cc);
24     this.cc = cn;
25   }
26
27   /*
28   private void targetComponentElement(ComponentNut cn)
29   throws Exception{
30     checkDuplicate("component", cc);
31     this.cc = cn.eval();
32   }
33   public void addBean(BeanNut bn)
34   throws Exception{
35     targetComponentElement(bn);
36   }
37   public void addCtor(ConstructorNut cn)
38   throws Exception{
39     targetComponentElement(cn);
40   }
41   public void addMethod(MethodNut mn)
42   throws Exception{
43     targetComponentElement(mn);
44   }
45   public void addField(FieldNut fn)
46   throws Exception{
47     targetComponentElement(fn);
48   }
49   public void addGetter(GetterNut gn)
50   throws Exception{
51     targetComponentElement(gn);
52   }*/

53   public boolean isPrivate_access() {
54     return private_access;
55   }
56   public void setPrivate_access(boolean private_access) {
57     this.private_access = private_access;
58   }
59   public void setClass(Class JavaDoc type){
60     //checkDuplicate("component", cc);
61
this.hostclass = type;
62   }
63   public void setComponent(Component c){
64     //checkDuplicate("class", hostclass);
65
this.cc = c;
66   }
67   public void setName(String JavaDoc name){
68     method_name = name.trim();
69   }
70   public String JavaDoc getName(){
71     return method_name;
72   }
73   private String JavaDoc initializer;
74   private String JavaDoc starter;
75   private String JavaDoc stopper;
76   private String JavaDoc disposer;
77   public String JavaDoc getInitializer() {
78     return initializer;
79   }
80
81   public void setInitializer(String JavaDoc initializer) {
82     this.initializer = initializer;
83   }
84
85   public String JavaDoc getStarter() {
86     return starter;
87   }
88
89   public void setStarter(String JavaDoc starter) {
90     this.starter = starter;
91   }
92
93   public String JavaDoc getStopper() {
94     return stopper;
95   }
96
97   public void setStopper(String JavaDoc stopper) {
98     this.stopper = stopper;
99   }
100
101   public String JavaDoc getDisposer() {
102     return disposer;
103   }
104
105   public void setDisposer(String JavaDoc disposer) {
106     this.disposer = disposer;
107   }
108   private Method JavaDoc lookupMethod(Class JavaDoc[] param_types){
109     if(param_types==null){
110       final int argcount = getMaxArgsCount();
111       if(argcount <0){
112         if(this.getParameterAutowireMode()==null){
113           return ReflectionUtil.getMethod(hostclass, method_name,
114               null, private_access);
115         }
116         else return ReflectionUtil.getMethod(hostclass, method_name,
117             private_access);
118       }
119       else{
120         return ReflectionUtil.getMethod(hostclass, method_name,
121             argcount, private_access);
122       }
123     }
124     else{
125       return ReflectionUtil.getMethod(hostclass, method_name,
126           param_types, private_access);
127     }
128   }
129   private Component lookupComponent(Class JavaDoc[] param_types){
130     if(param_types==null){
131       final int argcount = getMaxArgsCount();
132       if(argcount<0){
133         return cc.method(method_name,
134             private_access);
135       }
136       else{
137         return Components.bindMethod(cc, method_name,
138             argcount, private_access);
139       }
140     }
141     else{
142       return cc.method(method_name, param_types, private_access);
143     }
144   }
145   private Method JavaDoc lookupStaticMethod(Class JavaDoc[] param_types){
146     if(param_types==null){
147       final int argcount = getMaxArgsCount();
148       if(argcount < 0){
149         return ReflectionUtil.getStaticMethod(hostclass, method_name,
150             private_access);
151       }
152       else{
153         return ReflectionUtil.getStaticMethod(hostclass, method_name,
154             argcount, private_access);
155       }
156     }
157     else{
158       return ReflectionUtil.getMethod(hostclass, method_name, param_types,
159           private_access);
160     }
161   }
162   /**
163    * Evaluate the Component without applying life cycle.
164    * Subclass can call this method and apply customized lifecycle.
165    */

166   protected Component evaluateNoLifecycle(){
167     checkMandatory("class", hostclass, "component", cc);
168     checkMandatory("name", method_name);
169     Component result;
170     final Class JavaDoc[] param_types = getParameterTypes();
171     if(cc != null){
172       if(hostclass != null){
173         final Method JavaDoc mtd = lookupMethod(param_types);
174         result = cc.method(mtd);
175       }
176       else{
177         result = lookupComponent(param_types);
178         /*
179         result = param_types==null?
180             Components.bindMethod(cc, method_name,
181                 getMaxArgsCount(), private_access)
182             :cc.method(method_name, param_types, private_access);
183             */

184       }
185     }
186     else{
187       final Method JavaDoc mtd = lookupStaticMethod(param_types);
188       result = Components.fun(Functions.static_method(mtd));
189       /*
190       if(param_types==null)
191         setParams(mtd.getParameterTypes());*/

192     }
193     return decorateComponent(result);
194     //return applyArguments(result);
195
}
196   public Component eval(){
197     return Util.wrapLifecycle(evaluateNoLifecycle(), this);
198   }
199 }
200
Popular Tags