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.HashSet;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.mybatis.generator.api.dom.OutputUtilities;
25  
26  /**
27   * This class encapsulates the idea of an inner class - it has methods that make
28   * it easy to generate inner classes.
29   * 
30   * @author Jeff Butler
31   */
32  public class InnerClass extends JavaElement {
33      
34      /** The fields. */
35      private List<Field> fields;
36  
37      /** The inner classes. */
38      private List<InnerClass> innerClasses;
39  
40      /** The inner enums. */
41      private List<InnerEnum> innerEnums;
42  
43      /** The super class. */
44      private FullyQualifiedJavaType superClass;
45  
46      /** The type. */
47      private FullyQualifiedJavaType type;
48  
49      /** The super interface types. */
50      private Set<FullyQualifiedJavaType> superInterfaceTypes;
51  
52      /** The methods. */
53      private List<Method> methods;
54  
55      /** The is abstract. */
56      private boolean isAbstract;
57      
58      /** The initialization blocks. */
59      private List<InitializationBlock> initializationBlocks;
60  
61      /**
62       * Instantiates a new inner class.
63       *
64       * @param type
65       *            the type
66       */
67      public InnerClass(FullyQualifiedJavaType type) {
68          super();
69          this.type = type;
70          fields = new ArrayList<Field>();
71          innerClasses = new ArrayList<InnerClass>();
72          innerEnums = new ArrayList<InnerEnum>();
73          superInterfaceTypes = new HashSet<FullyQualifiedJavaType>();
74          methods = new ArrayList<Method>();
75          initializationBlocks = new ArrayList<InitializationBlock>();
76      }
77  
78      /**
79       * Instantiates a new inner class.
80       *
81       * @param typeName
82       *            the type name
83       */
84      public InnerClass(String typeName) {
85          this(new FullyQualifiedJavaType(typeName));
86      }
87  
88      /**
89       * Gets the fields.
90       *
91       * @return Returns the fields.
92       */
93      public List<Field> getFields() {
94          return fields;
95      }
96  
97      /**
98       * Adds the field.
99       *
100      * @param field
101      *            the field
102      */
103     public void addField(Field field) {
104         fields.add(field);
105     }
106 
107     /**
108      * Gets the super class.
109      *
110      * @return Returns the superClass.
111      */
112     public FullyQualifiedJavaType getSuperClass() {
113         return superClass;
114     }
115 
116     /**
117      * Sets the super class.
118      *
119      * @param superClass
120      *            The superClass to set.
121      */
122     public void setSuperClass(FullyQualifiedJavaType superClass) {
123         this.superClass = superClass;
124     }
125 
126     /**
127      * Sets the super class.
128      *
129      * @param superClassType
130      *            the new super class
131      */
132     public void setSuperClass(String superClassType) {
133         this.superClass = new FullyQualifiedJavaType(superClassType);
134     }
135 
136     /**
137      * Gets the inner classes.
138      *
139      * @return Returns the innerClasses.
140      */
141     public List<InnerClass> getInnerClasses() {
142         return innerClasses;
143     }
144 
145     /**
146      * Adds the inner class.
147      *
148      * @param innerClass
149      *            the inner class
150      */
151     public void addInnerClass(InnerClass innerClass) {
152         innerClasses.add(innerClass);
153     }
154 
155     /**
156      * Gets the inner enums.
157      *
158      * @return the inner enums
159      */
160     public List<InnerEnum> getInnerEnums() {
161         return innerEnums;
162     }
163 
164     /**
165      * Adds the inner enum.
166      *
167      * @param innerEnum
168      *            the inner enum
169      */
170     public void addInnerEnum(InnerEnum innerEnum) {
171         innerEnums.add(innerEnum);
172     }
173 
174     /**
175      * Gets the initialization blocks.
176      *
177      * @return the initialization blocks
178      */
179     public List<InitializationBlock> getInitializationBlocks() {
180         return initializationBlocks;
181     }
182 
183     /**
184      * Adds the initialization block.
185      *
186      * @param initializationBlock
187      *            the initialization block
188      */
189     public void addInitializationBlock(InitializationBlock initializationBlock) {
190         initializationBlocks.add(initializationBlock);
191     }
192 
193     /**
194      * Gets the formatted content.
195      *
196      * @param indentLevel
197      *            the indent level
198      * @param compilationUnit the compilation unit      
199      * @return the formatted content
200      */
201     public String getFormattedContent(int indentLevel, CompilationUnit compilationUnit) {
202         StringBuilder sb = new StringBuilder();
203 
204         addFormattedJavadoc(sb, indentLevel);
205         addFormattedAnnotations(sb, indentLevel);
206 
207         OutputUtilities.javaIndent(sb, indentLevel);
208         sb.append(getVisibility().getValue());
209 
210         if (isAbstract()) {
211             sb.append("abstract "); //$NON-NLS-1$
212         }
213 
214         if (isStatic()) {
215             sb.append("static "); //$NON-NLS-1$
216         }
217 
218         if (isFinal()) {
219             sb.append("final "); //$NON-NLS-1$
220         }
221 
222         sb.append("class "); //$NON-NLS-1$
223         sb.append(getType().getShortName());
224 
225         if (superClass != null) {
226             sb.append(" extends "); //$NON-NLS-1$
227             sb.append(JavaDomUtils.calculateTypeName(compilationUnit, superClass));
228         }
229 
230         if (superInterfaceTypes.size() > 0) {
231             sb.append(" implements "); //$NON-NLS-1$
232 
233             boolean comma = false;
234             for (FullyQualifiedJavaType fqjt : superInterfaceTypes) {
235                 if (comma) {
236                     sb.append(", "); //$NON-NLS-1$
237                 } else {
238                     comma = true;
239                 }
240 
241                 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, fqjt));
242             }
243         }
244 
245         sb.append(" {"); //$NON-NLS-1$
246         indentLevel++;
247         
248         Iterator<Field> fldIter = fields.iterator();
249         while (fldIter.hasNext()) {
250             OutputUtilities.newLine(sb);
251             Field field = fldIter.next();
252             sb.append(field.getFormattedContent(indentLevel, compilationUnit));
253             if (fldIter.hasNext()) {
254                 OutputUtilities.newLine(sb);
255             }
256         }
257 
258         if (initializationBlocks.size() > 0) {
259             OutputUtilities.newLine(sb);
260         }
261 
262         Iterator<InitializationBlock> blkIter = initializationBlocks.iterator();
263         while (blkIter.hasNext()) {
264             OutputUtilities.newLine(sb);
265             InitializationBlock initializationBlock = blkIter.next();
266             sb.append(initializationBlock.getFormattedContent(indentLevel));
267             if (blkIter.hasNext()) {
268                 OutputUtilities.newLine(sb);
269             }
270         }
271 
272         if (methods.size() > 0) {
273             OutputUtilities.newLine(sb);
274         }
275 
276         Iterator<Method> mtdIter = methods.iterator();
277         while (mtdIter.hasNext()) {
278             OutputUtilities.newLine(sb);
279             Method method = mtdIter.next();
280             sb.append(method.getFormattedContent(indentLevel, false, compilationUnit));
281             if (mtdIter.hasNext()) {
282                 OutputUtilities.newLine(sb);
283             }
284         }
285 
286         if (innerClasses.size() > 0) {
287             OutputUtilities.newLine(sb);
288         }
289         Iterator<InnerClass> icIter = innerClasses.iterator();
290         while (icIter.hasNext()) {
291             OutputUtilities.newLine(sb);
292             InnerClass innerClass = icIter.next();
293             sb.append(innerClass.getFormattedContent(indentLevel, compilationUnit));
294             if (icIter.hasNext()) {
295                 OutputUtilities.newLine(sb);
296             }
297         }
298 
299         if (innerEnums.size() > 0) {
300             OutputUtilities.newLine(sb);
301         }
302 
303         Iterator<InnerEnum> ieIter = innerEnums.iterator();
304         while (ieIter.hasNext()) {
305             OutputUtilities.newLine(sb);
306             InnerEnum innerEnum = ieIter.next();
307             sb.append(innerEnum.getFormattedContent(indentLevel, compilationUnit));
308             if (ieIter.hasNext()) {
309                 OutputUtilities.newLine(sb);
310             }
311         }
312 
313         indentLevel--;
314         OutputUtilities.newLine(sb);
315         OutputUtilities.javaIndent(sb, indentLevel);
316         sb.append('}');
317 
318         return sb.toString();
319     }
320 
321     /**
322      * Gets the super interface types.
323      *
324      * @return Returns the superInterfaces.
325      */
326     public Set<FullyQualifiedJavaType> getSuperInterfaceTypes() {
327         return superInterfaceTypes;
328     }
329 
330     /**
331      * Adds the super interface.
332      *
333      * @param superInterface
334      *            the super interface
335      */
336     public void addSuperInterface(FullyQualifiedJavaType superInterface) {
337         superInterfaceTypes.add(superInterface);
338     }
339 
340     /**
341      * Gets the methods.
342      *
343      * @return Returns the methods.
344      */
345     public List<Method> getMethods() {
346         return methods;
347     }
348 
349     /**
350      * Adds the method.
351      *
352      * @param method
353      *            the method
354      */
355     public void addMethod(Method method) {
356         methods.add(method);
357     }
358 
359     /**
360      * Gets the type.
361      *
362      * @return Returns the type.
363      */
364     public FullyQualifiedJavaType getType() {
365         return type;
366     }
367 
368     /**
369      * Checks if is abstract.
370      *
371      * @return true, if is abstract
372      */
373     public boolean isAbstract() {
374         return isAbstract;
375     }
376 
377     /**
378      * Sets the abstract.
379      *
380      * @param isAbtract
381      *            the new abstract
382      */
383     public void setAbstract(boolean isAbtract) {
384         this.isAbstract = isAbtract;
385     }
386 }