1 34 35 package net.percederberg.grammatica.code.visualbasic; 36 37 import java.io.PrintWriter ; 38 import java.util.LinkedList ; 39 40 import net.percederberg.grammatica.code.CodeElement; 41 import net.percederberg.grammatica.code.CodeStyle; 42 43 51 public class VisualBasicMethod extends CodeElement { 52 53 56 public static final int PUBLIC = VisualBasicModifier.PUBLIC; 57 58 61 public static final int PROTECTED_FRIEND = 62 VisualBasicModifier.PROTECTED_FRIEND; 63 64 67 public static final int PROTECTED = VisualBasicModifier.PROTECTED; 68 69 72 public static final int FRIEND = VisualBasicModifier.FRIEND; 73 74 78 public static final int PRIVATE = VisualBasicModifier.PRIVATE; 79 80 84 public static final int SHARED = VisualBasicModifier.SHARED; 85 86 89 public static final int SHADOWS = VisualBasicModifier.SHADOWS; 90 91 95 public static final int OVERRIDABLE = VisualBasicModifier.OVERRIDABLE; 96 97 101 public static final int NOT_OVERRIDABLE = 102 VisualBasicModifier.NOT_OVERRIDABLE; 103 104 108 public static final int OVERRIDES = VisualBasicModifier.OVERRIDES; 109 110 114 public static final int MUST_OVERRIDE = 115 VisualBasicModifier.MUST_OVERRIDE; 116 117 120 public static final int OVERLOADS = VisualBasicModifier.OVERLOADS; 121 122 125 private int modifiers; 126 127 130 private String name; 131 132 135 private String args; 136 137 140 private String returnType; 141 142 145 private LinkedList code; 146 147 150 private VisualBasicComment comment; 151 152 155 private boolean printCode; 156 157 163 public VisualBasicMethod(String name) { 164 this(name, ""); 165 } 166 167 174 public VisualBasicMethod(String name, String args) { 175 this(name, args, ""); 176 } 177 178 185 public VisualBasicMethod(String name, String args, String returnType) { 186 this(PUBLIC, name, args, returnType); 187 } 188 189 197 public VisualBasicMethod(int modifiers, 198 String name, 199 String args, 200 String returnType) { 201 202 this.modifiers = modifiers; 203 this.name = name; 204 this.args = args; 205 this.returnType = returnType; 206 this.code = new LinkedList (); 207 this.comment = null; 208 this.printCode = true; 209 } 210 211 216 public void addCode(String codeLines) { 217 int pos; 218 219 pos = codeLines.indexOf('\n'); 220 while (pos >= 0) { 221 code.add(codeLines.substring(0, pos)); 222 codeLines = codeLines.substring(pos + 1); 223 pos = codeLines.indexOf('\n'); 224 } 225 code.add(codeLines); 226 } 227 228 233 public void addComment(VisualBasicComment comment) { 234 this.comment = comment; 235 } 236 237 245 public int category() { 246 return ((modifiers & SHARED) > 0) ? 6 : 8; 247 } 248 249 257 public boolean canPrintCode() { 258 return printCode && (modifiers & MUST_OVERRIDE) == 0; 259 } 260 261 266 public void setPrintCode(boolean value) { 267 this.printCode = value; 268 } 269 270 277 public void print(PrintWriter out, CodeStyle style, int indent) { 278 String indentStr = style.getIndent(indent); 279 String codeIndentStr = style.getIndent(indent + 1); 280 StringBuffer res = new StringBuffer (); 281 282 if (comment != null) { 284 comment.print(out, style, indent); 285 } 286 287 res.append(indentStr); 289 res.append(VisualBasicModifier.createModifierDecl(modifiers)); 290 if (returnType.equals("")) { 291 res.append("Sub"); 292 } else { 293 res.append("Function"); 294 } 295 res.append(" "); 296 res.append(name); 297 res.append("("); 298 res.append(args); 299 if (returnType.equals("")) { 300 res.append(")"); 301 } else { 302 res.append(") As "); 303 res.append(returnType); 304 } 305 306 if (canPrintCode()) { 308 res.append("\n"); 309 for (int i = 0; i < code.size(); i++) { 310 if (code.get(i).toString().length() > 0) { 311 res.append(codeIndentStr); 312 res.append(code.get(i).toString()); 313 res.append("\n"); 314 } else { 315 res.append("\n"); 316 } 317 } 318 res.append(indentStr); 319 if (returnType.equals("")) { 320 res.append("End Sub"); 321 } else { 322 res.append("End Function"); 323 } 324 } 325 326 out.println(res.toString()); 328 } 329 } 330 | Popular Tags |