1 33 34 package net.percederberg.grammatica.code.csharp; 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 48 public class CSharpMethod extends CodeElement { 49 50 53 public static final int PUBLIC = CSharpModifier.PUBLIC; 54 55 58 public static final int PROTECTED_INTERNAL = 59 CSharpModifier.PROTECTED_INTERNAL; 60 61 64 public static final int PROTECTED = CSharpModifier.PROTECTED; 65 66 69 public static final int INTERNAL = CSharpModifier.INTERNAL; 70 71 75 public static final int PRIVATE = CSharpModifier.PRIVATE; 76 77 81 public static final int STATIC = CSharpModifier.STATIC; 82 83 86 public static final int NEW = CSharpModifier.NEW; 87 88 92 public static final int VIRTUAL = CSharpModifier.VIRTUAL; 93 94 97 public static final int SEALED = CSharpModifier.SEALED; 98 99 103 public static final int OVERRIDE = CSharpModifier.OVERRIDE; 104 105 109 public static final int ABSTRACT = CSharpModifier.ABSTRACT; 110 111 114 public static final int EXTERN = CSharpModifier.EXTERN; 115 116 119 private int modifiers; 120 121 124 private String name; 125 126 129 private String args; 130 131 134 private String returnType; 135 136 139 private LinkedList code; 140 141 144 private CSharpComment comment; 145 146 149 private boolean printCode; 150 151 157 public CSharpMethod(String name) { 158 this(name, ""); 159 } 160 161 168 public CSharpMethod(String name, String args) { 169 this(name, args, "void"); 170 } 171 172 179 public CSharpMethod(String name, String args, String returnType) { 180 this(PUBLIC, name, args, returnType); 181 } 182 183 191 public CSharpMethod(int modifiers, 192 String name, 193 String args, 194 String returnType) { 195 196 this.modifiers = modifiers; 197 this.name = name; 198 this.args = args; 199 this.returnType = returnType; 200 this.code = new LinkedList (); 201 this.comment = null; 202 this.printCode = true; 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(CSharpComment 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 276 if (comment != null) { 278 comment.print(out, style, indent); 279 } 280 281 res.append(indentStr); 283 res.append(CSharpModifier.createModifierDecl(modifiers)); 284 res.append(returnType); 285 res.append(" "); 286 res.append(name); 287 res.append("("); 288 res.append(args); 289 res.append(")"); 290 291 if (canPrintCode()) { 293 res.append(" {\n"); 294 for (int i = 0; i < code.size(); i++) { 295 if (code.get(i).toString().length() > 0) { 296 res.append(codeIndentStr); 297 res.append(code.get(i).toString()); 298 res.append("\n"); 299 } else { 300 res.append("\n"); 301 } 302 } 303 res.append(indentStr); 304 res.append("}"); 305 } else { 306 res.append(";"); 307 } 308 309 out.println(res.toString()); 311 } 312 } 313 | Popular Tags |