1 33 34 package net.percederberg.grammatica.code.java; 35 36 import java.io.PrintWriter ; 37 import java.util.LinkedList ; 38 39 import net.percederberg.grammatica.code.CodeElement; 40 import net.percederberg.grammatica.code.CodeStyle; 41 42 49 public class JavaVariable extends CodeElement { 50 51 54 public static final int PUBLIC = JavaModifier.PUBLIC; 55 56 59 public static final int PROTECTED = JavaModifier.PROTECTED; 60 61 64 public static final int PACKAGE_LOCAL = JavaModifier.PACKAGE_LOCAL; 65 66 69 public static final int PRIVATE = JavaModifier.PRIVATE; 70 71 74 public static final int STATIC = JavaModifier.STATIC; 75 76 79 public static final int FINAL = JavaModifier.FINAL; 80 81 84 public static final int TRANSIENT = JavaModifier.TRANSIENT; 85 86 89 public static final int VOLATILE = JavaModifier.VOLATILE; 90 91 94 private int modifiers; 95 96 99 private String type; 100 101 104 private String name; 105 106 113 private String initValue; 114 115 121 private LinkedList initValueList; 122 123 126 private JavaComment comment; 127 128 134 public JavaVariable(String type, String name) { 135 this(PUBLIC, type, name); 136 } 137 138 146 public JavaVariable(int modifiers, String type, String name) { 147 this(modifiers, type, name, null); 148 } 149 150 158 public JavaVariable(String type, String name, String initValue) { 159 this(PUBLIC, type, name, initValue); 160 } 161 162 171 public JavaVariable(int modifiers, 172 String type, 173 String name, 174 String initValue) { 175 176 this.modifiers = modifiers; 177 this.type = type; 178 this.name = name; 179 this.initValue = initValue; 180 this.initValueList = new LinkedList (); 181 this.comment = null; 182 } 183 184 189 public void addComment(JavaComment comment) { 190 this.comment = comment; 191 } 192 193 201 public void addArrayInit(String elementValue) { 202 if (this.initValue != null) { 203 this.initValueList.add(this.initValue); 204 this.initValue = null; 205 } 206 this.initValueList.add(elementValue); 207 } 208 209 217 public int category() { 218 return ((modifiers & STATIC) > 0) ? 4 : 5; 219 } 220 221 228 public void print(PrintWriter out, CodeStyle style, int indent) { 229 String indentStr = style.getIndent(indent); 230 String prefix = JavaModifier.createModifierDecl(modifiers); 231 String init; 232 233 if (comment != null) { 234 comment.print(out, style, indent); 235 } 236 init = getInitCode(style, indent); 237 if (init == null) { 238 out.println(indentStr + prefix + type + " " + name + ";"); 239 } else { 240 out.println(indentStr + prefix + type + " " + name + " = " + 241 init + ";"); 242 } 243 } 244 245 254 private String getInitCode(CodeStyle style, int indent) { 255 String indentStr = style.getIndent(indent); 256 String codeIndentStr = style.getIndent(indent + 1); 257 StringBuffer res; 258 259 if (initValueList.size() == 0 && initValue == null) { 261 return null; 262 } else if (initValue != null) { 263 return initValue; 264 } 265 266 res = new StringBuffer ("{\n"); 268 for (int i = 0; i < initValueList.size(); i++) { 269 res.append(codeIndentStr); 270 res.append(initValueList.get(i).toString()); 271 if (i + 1 < initValueList.size()) { 272 res.append(",\n"); 273 } else { 274 res.append("\n"); 275 } 276 } 277 res.append(indentStr); 278 res.append("}"); 279 280 return res.toString(); 281 } 282 } 283 | Popular Tags |