001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import org.objectweb.jac.util.Strings;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Vector;
025    
026    public class Method extends Member {
027      
028       public String getPrototype() {
029          String prototype = 
030             ((type==null)?"void":type.getGenerationFullName())+
031             " "+getGenerationName()+"("+getParametersString()+")";
032          if (!exceptions.isEmpty()) {
033             prototype += " throws "+Strings.join(exceptions,",");
034          }
035          return prototype;
036       }
037    
038       public String getModifiers() {
039          if ((parent instanceof Interface)) {
040             return "";
041          } else {
042             String modifiers = super.getModifiers();
043             if (isAbstract)
044                modifiers += " abstract";
045             if (isSynchronized)
046                modifiers += " synchronized";
047             return modifiers;
048          }
049       }
050    
051       /**
052        * Returns the prototypes (type and name) of the parameters, separated by a comma
053        */
054       public String getParametersString() {
055          String result="";
056          if (parameters == null) 
057             return result;
058          Iterator it = parameters.iterator();
059          while (it.hasNext()) {
060             Parameter cur = (Parameter)it.next();
061             result = result+cur.getPrototype();
062             if (it.hasNext()) {
063                result = result+", ";
064             }
065          }
066          return result;
067       }
068    
069       Vector parameters=new Vector();
070       
071       /**
072        * Get the value of parameters.
073        * @return value of parameters.
074        */
075       public List getParameters() {
076          return parameters;
077       }
078    
079       public void addParameter(Parameter p) {
080          parameters.add(p);
081       }
082    
083       public void removeParameter(Parameter p) {
084          parameters.remove(p);
085       }
086    
087       public void clearParameters() {
088          parameters.clear();
089       }
090    
091       /**
092        * Returns an array containing the types of the parameters
093        */
094       public Type[] getParameterTypes() {
095          Type[] types = new Type[parameters.size()];
096          int i=0;
097          Iterator it = parameters.iterator();
098          while (it.hasNext()) {
099             types[i] = ((Parameter)it.next()).getType();
100             i++;
101          }
102          return types;
103       }
104    
105       String body;
106       
107       /**
108        * Get the value of body.
109        * @return value of body.
110        */
111       public String 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  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 getUniqueName() {
144          StringBuffer result = new StringBuffer();
145          result.append(getGenerationName());
146          result.append('(');
147          Iterator 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 getParameterNames() {
162          StringBuffer text = new StringBuffer();
163          Iterator 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 exceptions = new Vector();
174       public List getExceptions() {
175          return exceptions;
176       }
177       public void addException(String exception) {
178          exceptions.add(exception);
179       }
180       public void removeException(String 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 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 exceptions = getExceptions().iterator();
203          while (exceptions.hasNext()) {
204             String exception = (String)exceptions.next();
205             method.addException(exception);
206          }
207          return method;
208       }
209    }