KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > Method


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import org.objectweb.jac.util.Strings;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 public class Method extends Member {
27   
28    public String JavaDoc getPrototype() {
29       String JavaDoc prototype =
30          ((type==null)?"void":type.getGenerationFullName())+
31          " "+getGenerationName()+"("+getParametersString()+")";
32       if (!exceptions.isEmpty()) {
33          prototype += " throws "+Strings.join(exceptions,",");
34       }
35       return prototype;
36    }
37
38    public String JavaDoc getModifiers() {
39       if ((parent instanceof Interface)) {
40          return "";
41       } else {
42          String JavaDoc modifiers = super.getModifiers();
43          if (isAbstract)
44             modifiers += " abstract";
45          if (isSynchronized)
46             modifiers += " synchronized";
47          return modifiers;
48       }
49    }
50
51    /**
52     * Returns the prototypes (type and name) of the parameters, separated by a comma
53     */

54    public String JavaDoc getParametersString() {
55       String JavaDoc result="";
56       if (parameters == null)
57          return result;
58       Iterator JavaDoc it = parameters.iterator();
59       while (it.hasNext()) {
60          Parameter cur = (Parameter)it.next();
61          result = result+cur.getPrototype();
62          if (it.hasNext()) {
63             result = result+", ";
64          }
65       }
66       return result;
67    }
68
69    Vector JavaDoc parameters=new Vector JavaDoc();
70    
71    /**
72     * Get the value of parameters.
73     * @return value of parameters.
74     */

75    public List JavaDoc getParameters() {
76       return parameters;
77    }
78
79    public void addParameter(Parameter p) {
80       parameters.add(p);
81    }
82
83    public void removeParameter(Parameter p) {
84       parameters.remove(p);
85    }
86
87    public void clearParameters() {
88       parameters.clear();
89    }
90
91    /**
92     * Returns an array containing the types of the parameters
93     */

94    public Type[] getParameterTypes() {
95       Type[] types = new Type[parameters.size()];
96       int i=0;
97       Iterator JavaDoc it = parameters.iterator();
98       while (it.hasNext()) {
99          types[i] = ((Parameter)it.next()).getType();
100          i++;
101       }
102       return types;
103    }
104
105    String JavaDoc body;
106    
107    /**
108     * Get the value of body.
109     * @return value of body.
110     */

111    public String JavaDoc getBody() {
112       return body;
113    }
114    
115    /**
116     * Set the value of body.
117     * @param v Value to assign to body.
118     */

119    public void setBody(String JavaDoc v) {
120       this.body = v;
121    }
122
123    boolean isAbstract = false;
124    public boolean isAbstract() {
125       return isAbstract;
126    }
127    public void setAbstract(boolean value) {
128       isAbstract = value;
129    }
130
131    boolean isSynchronized = false;
132    public boolean isSynchronized() {
133       return isSynchronized;
134    }
135    public void setSynchronized(boolean value) {
136       isSynchronized = value;
137    }
138
139    /**
140     * Returns the name with the type of the parameters between
141     * parenthesis.
142     */

143    public String JavaDoc getUniqueName() {
144       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
145       result.append(getGenerationName());
146       result.append('(');
147       Iterator JavaDoc it = parameters.iterator();
148       while (it.hasNext()) {
149          Parameter param = (Parameter)it.next();
150          result.append(param.getTypeName());
151          if (it.hasNext())
152             result.append(',');
153       }
154       result.append(')');
155       return result.toString();
156    }
157
158    /**
159     * Returns the names of all parameters, separated by a comma
160     */

161    public String JavaDoc getParameterNames() {
162       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
163       Iterator JavaDoc it = getParameters().iterator();
164       while (it.hasNext()) {
165          Parameter parameter = (Parameter)it.next();
166          text.append(parameter.getName());
167          if (it.hasNext())
168             text.append(",");
169       }
170       return text.toString();
171    }
172
173    Vector JavaDoc exceptions = new Vector JavaDoc();
174    public List JavaDoc getExceptions() {
175       return exceptions;
176    }
177    public void addException(String JavaDoc exception) {
178       exceptions.add(exception);
179    }
180    public void removeException(String JavaDoc exception) {
181       exceptions.remove(exception);
182    }
183
184    /**
185     * Clone this method
186     * @return a new method with the same name, return type and same parameters
187     */

188    public Method cloneMethod() {
189       Method method = new Method();
190       method.setName(getName());
191       method.setType(getType());
192       method.setArray(isArray());
193       Iterator JavaDoc params = getParameters().iterator();
194       while (params.hasNext()) {
195          Parameter param = (Parameter)params.next();
196          Parameter newParam = new Parameter();
197          newParam.setName(param.getName());
198          newParam.setType(param.getType());
199          newParam.setArray(param.isArray());
200          method.addParameter(newParam);
201       }
202       Iterator JavaDoc exceptions = getExceptions().iterator();
203       while (exceptions.hasNext()) {
204          String JavaDoc exception = (String JavaDoc)exceptions.next();
205          method.addException(exception);
206       }
207       return method;
208    }
209 }
210
Popular Tags