KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > AbstractJavaMethod


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.ws.jaxme.js.JavaSource.Protection;
25
26
27 /**
28  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
29  */

30 public abstract class AbstractJavaMethod
31     extends ConditionalIndentationJavaSourceObject {
32   private List JavaDoc exceptions = new ArrayList JavaDoc();
33   private List JavaDoc params = new ArrayList JavaDoc();
34   public AbstractJavaMethod(String JavaDoc pName, JavaQName pType,
35                             Protection pProtection) {
36     super(pName, pType, pProtection);
37   }
38
39   /** <p>Returns whether the method is throwing the given exception.
40    * Note that this method doesn't care for inheritance. For example,
41    * if the method declares to be throwing an {@link java.io.IOException},
42    * then the value <code>isThrowing(java.net.MalformedURLException.class)</code>
43    * is still false.</p>
44    */

45   public boolean isThrowing(JavaQName e) {
46     if (e == null) {
47       throw new NullPointerException JavaDoc("The exception argument must not be null.");
48     }
49     for (Iterator JavaDoc iter = exceptions.iterator(); iter.hasNext(); ) {
50       if (e.equals(iter.next())) {
51         return true;
52       }
53     }
54     return false;
55   }
56
57   /** <p>Returns whether the method is throwing the given exception.
58    * Note that this method doesn't care for inheritance. For example,
59    * if the method declares to be throwing an {@link java.io.IOException},
60    * then the value <code>isThrowing(java.net.MalformedURLException.class)</code>
61    * is still false.</p>
62    */

63   public boolean isThrowing(Class JavaDoc e) {
64     if (e == null) {
65       throw new NullPointerException JavaDoc("The exception argument must not be null.");
66     }
67     return isThrowing(JavaQNameImpl.getInstance(e));
68   }
69
70   /** <p>Adds an exception to this methods list of exceptions.</p>
71    *
72    * @see #getExceptions
73    */

74   public void addThrows(JavaQName e) {
75     if (e == null) {
76       throw new NullPointerException JavaDoc("The exception argument must not be null.");
77     }
78     exceptions.add(e);
79   }
80
81   /** <p>Adds an exception to this methods list of exceptions.</p>
82    *
83    * @see #getExceptions
84    */

85   public void addThrows(Class JavaDoc e) {
86     if (e == null) {
87       throw new NullPointerException JavaDoc("The exception argument must not be null.");
88     }
89     exceptions.add(JavaQNameImpl.getInstance(e));
90   }
91
92   /** <p>Adds an exception to this methods list of exceptions.</p>
93    *
94    * @see #getExceptions
95    * @deprecated Use {@link #addThrows(JavaQName)}
96    */

97   public void addThrows(String JavaDoc e) {
98     exceptions.add(JavaQNameImpl.getInstance(e, true));
99     if (e == null) {
100       throw new NullPointerException JavaDoc("The exception argument must not be null.");
101     }
102   }
103
104
105   /** <p>Adds a parameter that this method takes.</p>
106    *
107    * @see #getParams
108    * @deprecated Use {@link #addParam(JavaQName, String)}
109    */

110   public void addParam(String JavaDoc p) {
111     if (p == null) {
112       throw new NullPointerException JavaDoc("param argument must not be null");
113     }
114     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(p);
115     if (!st.hasMoreTokens()) {
116       throw new IllegalArgumentException JavaDoc("param argument must have two tokens: type name");
117     }
118     String JavaDoc type = st.nextToken();
119     if (!st.hasMoreTokens()) {
120       throw new IllegalArgumentException JavaDoc("param argument must have two tokens: type name");
121     }
122     String JavaDoc name = st.nextToken();
123     if (st.hasMoreTokens()) {
124       throw new IllegalArgumentException JavaDoc("param argument must have exactly two tokens: type name");
125     }
126     addParam(type, name);
127   }
128
129   /** <p>Adds a parameter that this method takes.</p>
130    *
131    * @see #getParams
132    * @deprecated Use {@link #addParam(JavaQName, String)}
133    */

134   public void addParam(String JavaDoc p, String JavaDoc v) {
135     if (p == null) {
136       throw new NullPointerException JavaDoc("param argument must not be null");
137     }
138     p = p.trim();
139     addParam(JavaQNameImpl.getInstance(p, true), v);
140   }
141
142   /** <p>Adds a parameter that this method takes.</p>
143    *
144    * @see #getParams
145    * @return An object to use for referencing the parameter inside the method.
146    */

147   public Parameter addParam(Class JavaDoc p, String JavaDoc v) {
148     return addParam(JavaQNameImpl.getInstance(p), v);
149   }
150
151   /** <p>Adds a parameter that this method takes.</p>
152    *
153    * @see #getParams
154    * @return An object to use for referencing the parameter inside the method.
155    */

156   public Parameter addParam(JavaQName pType, String JavaDoc pName) {
157     if (pType == null) {
158       throw new NullPointerException JavaDoc("Type argument must not be null");
159     }
160     if (pName == null) {
161       throw new NullPointerException JavaDoc("Parameter name argument must not be null");
162     }
163     Parameter p = new Parameter(pType, pName);
164     params.add(p);
165     return p;
166   }
167
168   /** <p>Adds a parameter that this method takes.</p>
169    *
170    * @see #getParams
171    * @return An object to use for referencing the parameter inside the method.
172    */

173   public Parameter addParam(Parameter pParam) {
174     return addParam(pParam.getType(), pParam.getName());
175   }
176
177   /** <p>Clears the list of parameters.</p>
178    */

179   public void clearParams() {
180     params.clear();
181   }
182
183   /** <p>Returns the list of exceptions thrown by this method.</p>
184    *
185    * @see #addThrows(JavaQName)
186    */

187   public JavaQName[] getExceptions() {
188     return (JavaQName[]) exceptions.toArray(new JavaQName[exceptions.size()]);
189   }
190
191
192   /** <p>Returns the list of parameters that this method takes. Any element
193    * in the list is an instance of {@link Parameter}.</p>
194    *
195    * @see #addParam(JavaQName, String)
196    */

197   public Parameter[] getParams() {
198          return (Parameter[]) params.toArray(new Parameter[params.size()]);
199   }
200 }
201
Popular Tags