View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
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  package org.mybatis.generator.api.dom.java;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  import java.util.ListIterator;
22  
23  import org.mybatis.generator.api.dom.OutputUtilities;
24  
25  /**
26   * The Class Method.
27   *
28   * @author Jeff Butler
29   */
30  public class Method extends JavaElement {
31  
32      /** The body lines. */
33      private List<String> bodyLines;
34  
35      /** The constructor. */
36      private boolean constructor;
37  
38      /** The return type. */
39      private FullyQualifiedJavaType returnType;
40  
41      /** The name. */
42      private String name;
43  
44      /** The parameters. */
45      private List<Parameter> parameters;
46  
47      /** The exceptions. */
48      private List<FullyQualifiedJavaType> exceptions;
49      
50      /** The is synchronized. */
51      private boolean isSynchronized;
52      
53      /** The is native. */
54      private boolean isNative;
55  
56      /**
57       * Instantiates a new method.
58       */
59      public Method() {
60          // use a default name to avoid malformed code
61          this("bar"); //$NON-NLS-1$
62      }
63      
64      /**
65       * Instantiates a new method.
66       *
67       * @param name
68       *            the name
69       */
70      public Method(String name) {
71          super();
72          bodyLines = new ArrayList<String>();
73          parameters = new ArrayList<Parameter>();
74          exceptions = new ArrayList<FullyQualifiedJavaType>();
75          this.name = name;
76      }
77      
78      /**
79       * Copy constructor. Not a truly deep copy, but close enough for most purposes.
80       *
81       * @param original
82       *            the original
83       */
84      public Method(Method original) {
85          super(original);
86          bodyLines = new ArrayList<String>();
87          parameters = new ArrayList<Parameter>();
88          exceptions = new ArrayList<FullyQualifiedJavaType>();
89          this.bodyLines.addAll(original.bodyLines);
90          this.constructor = original.constructor;
91          this.exceptions.addAll(original.exceptions);
92          this.name = original.name;
93          this.parameters.addAll(original.parameters);
94          this.returnType = original.returnType;
95          this.isNative = original.isNative;
96          this.isSynchronized = original.isSynchronized;
97      }
98  
99      /**
100      * Gets the body lines.
101      *
102      * @return Returns the bodyLines.
103      */
104     public List<String> getBodyLines() {
105         return bodyLines;
106     }
107 
108     /**
109      * Adds the body line.
110      *
111      * @param line
112      *            the line
113      */
114     public void addBodyLine(String line) {
115         bodyLines.add(line);
116     }
117 
118     /**
119      * Adds the body line.
120      *
121      * @param index
122      *            the index
123      * @param line
124      *            the line
125      */
126     public void addBodyLine(int index, String line) {
127         bodyLines.add(index, line);
128     }
129 
130     /**
131      * Adds the body lines.
132      *
133      * @param lines
134      *            the lines
135      */
136     public void addBodyLines(Collection<String> lines) {
137         bodyLines.addAll(lines);
138     }
139 
140     /**
141      * Adds the body lines.
142      *
143      * @param index
144      *            the index
145      * @param lines
146      *            the lines
147      */
148     public void addBodyLines(int index, Collection<String> lines) {
149         bodyLines.addAll(index, lines);
150     }
151 
152     /**
153      * Gets the formatted content.
154      *
155      * @param indentLevel
156      *            the indent level
157      * @param interfaceMethod
158      *            the interface method
159      * @param compilationUnit the compilation unit
160      * @return the formatted content
161      */
162     public String getFormattedContent(int indentLevel, boolean interfaceMethod, CompilationUnit compilationUnit) {
163         StringBuilder sb = new StringBuilder();
164 
165         addFormattedJavadoc(sb, indentLevel);
166         addFormattedAnnotations(sb, indentLevel);
167 
168         OutputUtilities.javaIndent(sb, indentLevel);
169 
170         if (!interfaceMethod) {
171             sb.append(getVisibility().getValue());
172 
173             if (isStatic()) {
174                 sb.append("static "); //$NON-NLS-1$
175             }
176 
177             if (isFinal()) {
178                 sb.append("final "); //$NON-NLS-1$
179             }
180             
181             if (isSynchronized()) {
182                 sb.append("synchronized "); //$NON-NLS-1$
183             }
184             
185             if (isNative()) {
186                 sb.append("native "); //$NON-NLS-1$
187             } else if (bodyLines.size() == 0) {
188                 sb.append("abstract "); //$NON-NLS-1$
189             }
190         }
191 
192         if (!constructor) {
193             if (getReturnType() == null) {
194                 sb.append("void"); //$NON-NLS-1$
195             } else {
196                 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, getReturnType()));
197             }
198             sb.append(' ');
199         }
200 
201         sb.append(getName());
202         sb.append('(');
203 
204         boolean comma = false;
205         for (Parameter parameter : getParameters()) {
206             if (comma) {
207                 sb.append(", "); //$NON-NLS-1$
208             } else {
209                 comma = true;
210             }
211 
212             sb.append(parameter.getFormattedContent(compilationUnit));
213         }
214 
215         sb.append(')');
216 
217         if (getExceptions().size() > 0) {
218             sb.append(" throws "); //$NON-NLS-1$
219             comma = false;
220             for (FullyQualifiedJavaType fqjt : getExceptions()) {
221                 if (comma) {
222                     sb.append(", "); //$NON-NLS-1$
223                 } else {
224                     comma = true;
225                 }
226 
227                 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, fqjt));
228             }
229         }
230 
231         // if no body lines, then this is an abstract method
232         if (bodyLines.size() == 0 || isNative()) {
233             sb.append(';');
234         } else {
235             sb.append(" {"); //$NON-NLS-1$
236             indentLevel++;
237 
238             ListIterator<String> listIter = bodyLines.listIterator();
239             while (listIter.hasNext()) {
240                 String line = listIter.next();
241                 if (line.startsWith("}")) { //$NON-NLS-1$
242                     indentLevel--;
243                 }
244 
245                 OutputUtilities.newLine(sb);
246                 OutputUtilities.javaIndent(sb, indentLevel);
247                 sb.append(line);
248 
249                 if ((line.endsWith("{") && !line.startsWith("switch")) //$NON-NLS-1$ //$NON-NLS-2$
250                         || line.endsWith(":")) { //$NON-NLS-1$
251                     indentLevel++;
252                 }
253 
254                 if (line.startsWith("break")) { //$NON-NLS-1$
255                     // if the next line is '}', then don't outdent
256                     if (listIter.hasNext()) {
257                         String nextLine = listIter.next();
258                         if (nextLine.startsWith("}")) { //$NON-NLS-1$
259                             indentLevel++;
260                         }
261 
262                         // set back to the previous element
263                         listIter.previous();
264                     }
265                     indentLevel--;
266                 }
267             }
268 
269             indentLevel--;
270             OutputUtilities.newLine(sb);
271             OutputUtilities.javaIndent(sb, indentLevel);
272             sb.append('}');
273         }
274 
275         return sb.toString();
276     }
277 
278     /**
279      * Checks if is constructor.
280      *
281      * @return Returns the constructor.
282      */
283     public boolean isConstructor() {
284         return constructor;
285     }
286 
287     /**
288      * Sets the constructor.
289      *
290      * @param constructor
291      *            The constructor to set.
292      */
293     public void setConstructor(boolean constructor) {
294         this.constructor = constructor;
295     }
296 
297     /**
298      * Gets the name.
299      *
300      * @return Returns the name.
301      */
302     public String getName() {
303         return name;
304     }
305 
306     /**
307      * Sets the name.
308      *
309      * @param name
310      *            The name to set.
311      */
312     public void setName(String name) {
313         this.name = name;
314     }
315 
316     /**
317      * Gets the parameters.
318      *
319      * @return the parameters
320      */
321     public List<Parameter> getParameters() {
322         return parameters;
323     }
324 
325     /**
326      * Adds the parameter.
327      *
328      * @param parameter
329      *            the parameter
330      */
331     public void addParameter(Parameter parameter) {
332         parameters.add(parameter);
333     }
334 
335     /**
336      * Adds the parameter.
337      *
338      * @param index
339      *            the index
340      * @param parameter
341      *            the parameter
342      */
343     public void addParameter(int index, Parameter parameter) {
344         parameters.add(index, parameter);
345     }
346 
347     /**
348      * Gets the return type.
349      *
350      * @return Returns the returnType.
351      */
352     public FullyQualifiedJavaType getReturnType() {
353         return returnType;
354     }
355 
356     /**
357      * Sets the return type.
358      *
359      * @param returnType
360      *            The returnType to set.
361      */
362     public void setReturnType(FullyQualifiedJavaType returnType) {
363         this.returnType = returnType;
364     }
365 
366     /**
367      * Gets the exceptions.
368      *
369      * @return Returns the exceptions.
370      */
371     public List<FullyQualifiedJavaType> getExceptions() {
372         return exceptions;
373     }
374 
375     /**
376      * Adds the exception.
377      *
378      * @param exception
379      *            the exception
380      */
381     public void addException(FullyQualifiedJavaType exception) {
382         exceptions.add(exception);
383     }
384 
385     /**
386      * Checks if is synchronized.
387      *
388      * @return true, if is synchronized
389      */
390     public boolean isSynchronized() {
391         return isSynchronized;
392     }
393 
394     /**
395      * Sets the synchronized.
396      *
397      * @param isSynchronized
398      *            the new synchronized
399      */
400     public void setSynchronized(boolean isSynchronized) {
401         this.isSynchronized = isSynchronized;
402     }
403 
404     /**
405      * Checks if is native.
406      *
407      * @return true, if is native
408      */
409     public boolean isNative() {
410         return isNative;
411     }
412 
413     /**
414      * Sets the native.
415      *
416      * @param isNative
417      *            the new native
418      */
419     public void setNative(boolean isNative) {
420         this.isNative = isNative;
421     }
422 }