1 23 24 29 30 package com.sun.jdo.spi.persistence.utility.generator.io; 31 32 import java.util.*; 33 import java.lang.reflect.Modifier ; 34 35 import com.sun.jdo.spi.persistence.utility.StringHelper; 36 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter; 37 38 52 public final class IOJavaClassWriter implements JavaClassWriter 53 { 54 private static final String FIELD = "FIELD"; private static final String INITIALIZER = "INITIALIZER"; private static final String CONSTRUCTOR = "CONSTRUCTOR"; private static final String METHOD = "METHOD"; private static final String INNER_CLASS = "INNER_CLASS"; private static final String MIXED = "MIXED"; private static final String COMMA_SEPARATOR = ", "; 62 private boolean _maintainCategories; 63 private String _superclass; 64 private String _classDeclarationBlock; 65 private List _interfaces = new ArrayList(); 66 private Map _members = new HashMap(); 67 68 72 public IOJavaClassWriter () 73 { 74 this(true); 75 } 76 77 84 public IOJavaClassWriter (boolean maintainCategories) 85 { 86 _maintainCategories = maintainCategories; 87 } 88 89 99 public void setClassDeclaration (final int modifiers, 100 final String className, final String [] comments) 101 { 102 final FormattedWriter writerHelper = new FormattedWriter(); 103 final String modifierString = Modifier.toString(modifiers); 104 105 writerHelper.writeComments(comments); 106 107 writerHelper.writeln(modifierString + ((modifierString.length() > 0) 108 ? " " : "") + "class " + className); 110 _classDeclarationBlock = writerHelper.toString(); 111 } 112 113 117 public void setSuperclass (final String name) 118 { 119 _superclass = name; 120 } 121 122 125 public void addInterface (final String name) 126 { 127 if (!StringHelper.isEmpty(name)) 128 _interfaces.add(name); 129 } 130 131 145 public void addField (final String name, final int modifiers, String type, 146 final String initialValue, final String [] comments) 147 { 148 final FormattedWriter writerHelper = new FormattedWriter(); 149 final String fieldString = 150 Modifier.toString(modifiers) + ' ' + type + ' ' + name; 151 152 writerHelper.writeComments(comments); 153 writerHelper.writeln(fieldString + ((initialValue != null) ? 154 (" = " + initialValue) : "") + ';'); 156 getMemberList(FIELD).add(writerHelper.toString()); 157 } 158 159 169 public void addInitializer (boolean isStatic, String [] body, 170 String [] comments) 171 { 172 final FormattedWriter writerHelper = new FormattedWriter(); 173 final int n = (body != null ? body.length : 0); 174 175 writerHelper.writeComments(comments); 176 177 writerHelper.writeln(isStatic ? "static" : ""); writerHelper.writeln("{"); 181 for (int i = 0; i < n; i++) 183 writerHelper.writeln(1, body[i]); 184 185 writerHelper.writeln("}"); 188 getMemberList(INITIALIZER).add(writerHelper.toString()); 189 } 190 191 209 public void addConstructor (final String name, final int modifiers, 210 final String [] parameterNames, final String [] parameterTypes, 211 final String [] exceptions, final String [] body, final String [] comments) 212 { 213 addMethod(name, modifiers, null, parameterNames, parameterTypes, 214 exceptions, body, comments, getMemberList(CONSTRUCTOR)); 215 } 216 217 235 public void addMethod (final String name, final int modifiers, 236 final String returnType, final String [] parameterNames, 237 final String [] parameterTypes, final String [] exceptions, 238 final String [] body, final String [] comments) 239 { 240 addMethod(name, modifiers, returnType, parameterNames, parameterTypes, 241 exceptions, body, comments, getMemberList(METHOD)); 242 } 243 244 247 public void addClass (final JavaClassWriter classWriter) 248 { 249 if (classWriter != null) 250 getMemberList(INNER_CLASS).add(classWriter); 251 } 252 253 256 public String toString () 257 { 258 final FormattedWriter writerHelper = new FormattedWriter(); 259 260 writeClassDeclaration(writerHelper); writeMembers(writerHelper); writerHelper.writeln("}"); writerHelper.writeln(); 264 265 return writerHelper.toString(); 266 } 267 268 private void addMethod (final String name, final int modifiers, 269 final String returnType, final String [] parameterNames, 270 final String [] parameterTypes, final String [] exceptions, 271 final String [] body, final String [] comments, List methodList) 272 { 273 final String signature = createMethodSignature(name, modifiers, 274 returnType, parameterNames, parameterTypes, exceptions); 275 final FormattedWriter writerHelper = new FormattedWriter(); 276 final int n = (body != null ? body.length : 0); 277 278 writerHelper.writeComments(comments); 279 280 if (signature.length() > 0) 282 writerHelper.writeln(signature); 283 284 writerHelper.writeln("{"); 286 for (int i = 0; i < n; i++) 288 writerHelper.writeln(1, body[i]); 289 290 writerHelper.writeln("}"); 293 methodList.add(writerHelper.toString()); 294 } 295 296 static private String createMethodSignature (final String name, 297 final int modifiers, String returnType, final String [] parameterNames, 298 final String [] parameterTypes, final String [] exceptions) 299 { 300 int i, count = (parameterNames != null ? parameterNames.length : 0); 301 final List parameterPairs = new ArrayList(); 302 final FormattedWriter writerHelper = new FormattedWriter(); 303 304 if (modifiers != 0) 305 writerHelper.write(Modifier.toString(modifiers) + ' '); 306 307 writerHelper.write(((returnType != null) ? 308 returnType + " " : "") + name); 310 writerHelper.write(" ("); 313 for (i = 0; i < count; i++) 314 { 315 writeListElement(i, count, parameterTypes[i] + ' ' + 316 parameterNames[i], writerHelper); 317 } 318 writerHelper.write(")"); 320 count = (exceptions != null ? exceptions.length : 0); 322 if (count > 0) 323 { 324 writerHelper.writeln(); 325 writerHelper.write(1, "throws "); 327 for (i = 0; i < count; i++) 328 writeListElement(i, count, exceptions[i], writerHelper); 329 } 330 331 return writerHelper.toString(); 332 } 333 334 static private void writeListElement (int i, int count, String string, 335 FormattedWriter writerHelper) 336 { 337 int indent = ((i == 0) ? 0 : 1); 338 339 if (i == (count - 1)) 340 writerHelper.write(indent, string); 341 else 342 writerHelper.writeln(indent, string + COMMA_SEPARATOR); 343 } 344 345 private List getMemberList (String memberType) 346 { 347 Object memberList = null; 348 349 if (!_maintainCategories) 350 memberType = MIXED; 351 352 memberList = _members.get(memberType); 353 if (memberList == null) 354 { 355 memberList = new ArrayList(); 356 357 _members.put(memberType, memberList); 358 } 359 360 return (List)memberList; 361 } 362 363 private void writeClassDeclaration (FormattedWriter writerHelper) 364 { 365 writerHelper.write(_classDeclarationBlock); 367 368 if (_superclass != null) 370 writerHelper.writeln(1, "extends " + _superclass); 372 if ((_interfaces != null) && (_interfaces.size() > 0)) 374 { 375 writerHelper.write(1, "implements "); writerHelper.write(StringHelper.arrayToSeparatedList( 377 _interfaces, COMMA_SEPARATOR)); 378 writerHelper.writeln(); 379 } 380 381 writerHelper.writeln("{"); } 383 384 private void writeMembers (FormattedWriter writerHelper) 385 { 386 if (_maintainCategories) 387 { 388 writerHelper.writeList(1, getMemberList(FIELD)); 389 writerHelper.writeList(1, getMemberList(INITIALIZER)); 390 writerHelper.writeList(1, getMemberList(CONSTRUCTOR)); 391 writerHelper.writeList(1, getMemberList(METHOD)); 392 writerHelper.writeList(1, getMemberList(INNER_CLASS)); 393 } 394 else 395 writerHelper.writeList(1, getMemberList(MIXED), true); 396 } 397 } 398 | Popular Tags |