1 33 34 package net.percederberg.grammatica.code.java; 35 36 import java.io.PrintWriter ; 37 import java.util.Collections ; 38 import java.util.LinkedList ; 39 40 import net.percederberg.grammatica.code.CodeElement; 41 import net.percederberg.grammatica.code.CodeStyle; 42 43 49 public class JavaConstructor 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 private int modifiers; 75 76 79 private JavaClass cls; 80 81 84 private String args; 85 86 89 private LinkedList throwList; 90 91 94 private LinkedList code; 95 96 99 private JavaComment comment; 100 101 104 public JavaConstructor() { 105 this(""); 106 } 107 108 113 public JavaConstructor(String args) { 114 this(PUBLIC, args); 115 } 116 117 123 public JavaConstructor(int modifiers, String args) { 124 this.modifiers = modifiers; 125 this.cls = null; 126 this.args = args; 127 this.throwList = new LinkedList (); 128 this.code = new LinkedList (); 129 this.comment = null; 130 } 131 132 138 public JavaClass getJavaClass() { 139 return this.cls; 140 } 141 142 147 void setJavaClass(JavaClass cls) { 148 this.cls = cls; 149 } 150 151 156 public void addThrows(String className) { 157 this.throwList.add(className); 158 } 159 160 165 public void addCode(String codeLines) { 166 int pos; 167 168 pos = codeLines.indexOf('\n'); 169 while (pos >= 0) { 170 this.code.add(codeLines.substring(0, pos)); 171 codeLines = codeLines.substring(pos + 1); 172 pos = codeLines.indexOf('\n'); 173 } 174 this.code.add(codeLines); 175 } 176 177 182 public void addComment(JavaComment comment) { 183 this.comment = comment; 184 } 185 186 194 public int category() { 195 return 7; 196 } 197 198 205 public void print(PrintWriter out, CodeStyle style, int indent) { 206 String indentStr = style.getIndent(indent); 207 String codeIndentStr = style.getIndent(indent + 1); 208 StringBuffer res = new StringBuffer (); 209 String str; 210 boolean brokenThrows = false; 211 212 if (comment != null) { 214 comment.print(out, style, indent); 215 } 216 217 res.append(indentStr); 219 res.append(JavaModifier.createModifierDecl(modifiers)); 220 res.append(cls.toString()); 221 res.append("("); 222 res.append(args); 223 res.append(")"); 224 str = getThrowDecl(); 225 if (str.length() > 0) { 226 if (res.length() + str.length() < style.getMargin()) { 227 res.append(" "); 228 } else { 229 res.append("\n"); 230 res.append(codeIndentStr); 231 brokenThrows = true; 232 } 233 res.append(str); 234 } 235 res.append(" {\n"); 236 237 if (brokenThrows && code.size() > 0) { 239 res.append("\n"); 240 } 241 for (int i = 0; i < code.size(); i++) { 242 if (code.get(i).toString().length() > 0) { 243 res.append(codeIndentStr); 244 res.append(code.get(i).toString()); 245 res.append("\n"); 246 } else { 247 res.append("\n"); 248 } 249 } 250 res.append(indentStr); 251 res.append("}"); 252 253 out.println(res.toString()); 255 } 256 257 264 private String getThrowDecl() { 265 StringBuffer res = new StringBuffer ("throws "); 266 267 if (throwList.size() == 0) { 268 return ""; 269 } 270 Collections.sort(throwList); 271 for (int i = 0; i < throwList.size(); i++) { 272 res.append(throwList.get(i).toString()); 273 if (i < throwList.size() - 1) { 274 res.append(", "); 275 } 276 } 277 return res.toString(); 278 } 279 } 280 | Popular Tags |