View Javadoc
1   /**
2    *    Copyright 2006-2015 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 org.mybatis.generator.api.dom.OutputUtilities;
19  
20  /**
21   * @author Jeff Butler
22   */
23  public class Field extends JavaElement {
24      private FullyQualifiedJavaType type;
25      private String name;
26      private String initializationString;
27      private boolean isTransient;
28      private boolean isVolatile;
29  
30      /**
31       *  
32       */
33      public Field() {
34          // use a default name to avoid NPE
35          this("foo", FullyQualifiedJavaType.getIntInstance()); //$NON-NLS-1$
36      }
37      
38      public Field(String name, FullyQualifiedJavaType type) {
39          super();
40          this.name = name;
41          this.type = type;
42      }
43      
44      public Field(Field field) {
45          super(field);
46          this.type = field.type;
47          this.name = field.name;
48          this.initializationString = field.initializationString;
49      }
50  
51      /**
52       * @return Returns the name.
53       */
54      public String getName() {
55          return name;
56      }
57  
58      /**
59       * @param name
60       *            The name to set.
61       */
62      public void setName(String name) {
63          this.name = name;
64      }
65  
66      /**
67       * @return Returns the type.
68       */
69      public FullyQualifiedJavaType getType() {
70          return type;
71      }
72  
73      /**
74       * @param type
75       *            The type to set.
76       */
77      public void setType(FullyQualifiedJavaType type) {
78          this.type = type;
79      }
80  
81      /**
82       * @return Returns the initializationString.
83       */
84      public String getInitializationString() {
85          return initializationString;
86      }
87  
88      /**
89       * @param initializationString
90       *            The initializationString to set.
91       */
92      public void setInitializationString(String initializationString) {
93          this.initializationString = initializationString;
94      }
95  
96      public String getFormattedContent(int indentLevel) {
97          StringBuilder sb = new StringBuilder();
98  
99          addFormattedJavadoc(sb, indentLevel);
100         addFormattedAnnotations(sb, indentLevel);
101 
102         OutputUtilities.javaIndent(sb, indentLevel);
103         sb.append(getVisibility().getValue());
104 
105         if (isStatic()) {
106             sb.append("static "); //$NON-NLS-1$
107         }
108 
109         if (isFinal()) {
110             sb.append("final "); //$NON-NLS-1$
111         }
112 
113         if (isTransient()) {
114             sb.append("transient "); //$NON-NLS-1$
115         }
116         
117         if (isVolatile()) {
118             sb.append("volatile "); //$NON-NLS-1$
119         }
120         
121         sb.append(type.getShortName());
122 
123         sb.append(' ');
124         sb.append(name);
125 
126         if (initializationString != null && initializationString.length() > 0) {
127             sb.append(" = "); //$NON-NLS-1$
128             sb.append(initializationString);
129         }
130 
131         sb.append(';');
132 
133         return sb.toString();
134     }
135 
136     public boolean isTransient() {
137         return isTransient;
138     }
139 
140     public void setTransient(boolean isTransient) {
141         this.isTransient = isTransient;
142     }
143 
144     public boolean isVolatile() {
145         return isVolatile;
146     }
147 
148     public void setVolatile(boolean isVolatile) {
149         this.isVolatile = isVolatile;
150     }
151 }