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 JavaMethod 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 ABSTRACT = JavaModifier.ABSTRACT; 80 81 84 public static final int FINAL = JavaModifier.FINAL; 85 86 89 public static final int SYNCHRONIZED = JavaModifier.SYNCHRONIZED; 90 91 94 public static final int NATIVE = JavaModifier.NATIVE; 95 96 99 public static final int STRICTFP = JavaModifier.STRICTFP; 100 101 104 private int modifiers; 105 106 109 private String name; 110 111 114 private String args; 115 116 119 private String returnType; 120 121 124 private LinkedList throwList; 125 126 129 private LinkedList code; 130 131 134 private JavaComment comment; 135 136 139 private boolean printCode; 140 141 147 public JavaMethod(String name) { 148 this(name, ""); 149 } 150 151 158 public JavaMethod(String name, String args) { 159 this(name, args, "void"); 160 } 161 162 169 public JavaMethod(String name, String args, String returnType) { 170 this(PUBLIC, name, args, returnType); 171 } 172 173 181 public JavaMethod(int modifiers, 182 String name, 183 String args, 184 String returnType) { 185 186 this.modifiers = modifiers; 187 this.name = name; 188 this.args = args; 189 this.returnType = returnType; 190 this.throwList = new LinkedList (); 191 this.code = new LinkedList (); 192 this.comment = null; 193 this.printCode = true; 194 } 195 196 201 public void addThrows(String className) { 202 this.throwList.add(className); 203 } 204 205 210 public void addCode(String codeLines) { 211 int pos; 212 213 pos = codeLines.indexOf('\n'); 214 while (pos >= 0) { 215 code.add(codeLines.substring(0, pos)); 216 codeLines = codeLines.substring(pos + 1); 217 pos = codeLines.indexOf('\n'); 218 } 219 code.add(codeLines); 220 } 221 222 227 public void addComment(JavaComment comment) { 228 this.comment = comment; 229 } 230 231 239 public int category() { 240 return ((modifiers & STATIC) > 0) ? 6 : 8; 241 } 242 243 251 public boolean canPrintCode() { 252 return printCode && (modifiers & ABSTRACT) == 0; 253 } 254 255 260 public void setPrintCode(boolean value) { 261 this.printCode = value; 262 } 263 264 271 public void print(PrintWriter out, CodeStyle style, int indent) { 272 String indentStr = style.getIndent(indent); 273 String codeIndentStr = style.getIndent(indent + 1); 274 StringBuffer res = new StringBuffer (); 275 String str; 276 boolean brokenThrows = false; 277 278 if (comment != null) { 280 comment.print(out, style, indent); 281 } 282 283 res.append(indentStr); 285 res.append(JavaModifier.createModifierDecl(modifiers)); 286 res.append(returnType); 287 res.append(" "); 288 res.append(name); 289 res.append("("); 290 res.append(args); 291 res.append(")"); 292 str = getThrowDecl(); 293 if (str.length() > 0) { 294 if (res.length() + str.length() < style.getMargin()) { 295 res.append(" "); 296 } else { 297 res.append("\n"); 298 res.append(codeIndentStr); 299 brokenThrows = true; 300 } 301 res.append(str); 302 } 303 304 if (canPrintCode()) { 306 res.append(" {\n"); 307 if (brokenThrows && code.size() > 0) { 308 res.append("\n"); 309 } 310 for (int i = 0; i < code.size(); i++) { 311 if (code.get(i).toString().length() > 0) { 312 res.append(codeIndentStr); 313 res.append(code.get(i).toString()); 314 res.append("\n"); 315 } else { 316 res.append("\n"); 317 } 318 } 319 res.append(indentStr); 320 res.append("}"); 321 } else { 322 res.append(";"); 323 } 324 325 out.println(res.toString()); 327 } 328 329 336 private String getThrowDecl() { 337 StringBuffer res = new StringBuffer ("throws "); 338 339 if (throwList.size() == 0) { 340 return ""; 341 } 342 Collections.sort(throwList); 343 for (int i = 0; i < throwList.size(); i++) { 344 res.append(throwList.get(i).toString()); 345 if (i < throwList.size() - 1) { 346 res.append(", "); 347 } 348 } 349 return res.toString(); 350 } 351 } 352 | Popular Tags |