IOJavaClassWriterpublic final class IOJavaClassWriter extends Object implements com.sun.jdo.spi.persistence.utility.generator.JavaClassWriterThis implementation of the {@link JavaClassWriter} interface is based on
simple {@link java.lang.StringBuffer} "println" type statements.
The order of the generated code in this implementation depends on the
initialization. The default order is to accept fields and methods in any
order and generate all fields together and all methods together (by member
category). Specifying the ordered parameter in the constructor will lead
to a slightly different order: generation of fields and methods
interspersed among each other in exactly the order they were added (no
member categories). |
Fields Summary |
---|
private static final String | FIELD | private static final String | INITIALIZER | private static final String | CONSTRUCTOR | private static final String | METHOD | private static final String | INNER_CLASS | private static final String | MIXED | private static final String | COMMA_SEPARATOR | private boolean | _maintainCategories | private String | _superclass | private String | _classDeclarationBlock | private List | _interfaces | private Map | _members |
Constructors Summary |
---|
public IOJavaClassWriter()Creates a new instance of IOJavaClassWriter which maintains the
order of member input but groups them by category.
this(true);
| public IOJavaClassWriter(boolean maintainCategories)Creates a new instance of IOJavaClassWriter in which the order of the
generated code depends on the specified flag.
_maintainCategories = maintainCategories;
|
Methods Summary |
---|
public void | addClass(com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter classWriter)Adds an inner class to this class.
if (classWriter != null)
getMemberList(INNER_CLASS).add(classWriter);
| public void | addConstructor(java.lang.String name, int modifiers, java.lang.String[] parameterNames, java.lang.String[] parameterTypes, java.lang.String[] exceptions, java.lang.String[] body, java.lang.String[] comments)Adds a constructor to this class. Note that the type format in the
parameter type strings must be package style (that is - it can contain
. but not / or $).
addMethod(name, modifiers, null, parameterNames, parameterTypes,
exceptions, body, comments, getMemberList(CONSTRUCTOR));
| public void | addField(java.lang.String name, int modifiers, java.lang.String type, java.lang.String initialValue, java.lang.String[] comments)Adds a field to the list of those declared by this class. Note
that the type format must be package style (that is - it can contain
. but not / or $).
final FormattedWriter writerHelper = new FormattedWriter();
final String fieldString =
Modifier.toString(modifiers) + ' " + type + ' " + name;
writerHelper.writeComments(comments);
writerHelper.writeln(fieldString + ((initialValue != null) ?
(" = " + initialValue) : "") + ';"); // NOI18N
getMemberList(FIELD).add(writerHelper.toString());
| public void | addInitializer(boolean isStatic, java.lang.String[] body, java.lang.String[] comments)Adds an initializer to this class.
final FormattedWriter writerHelper = new FormattedWriter();
final int n = (body != null ? body.length : 0);
writerHelper.writeComments(comments);
// header
writerHelper.writeln(isStatic ? "static" : ""); // NOI18N
writerHelper.writeln("{"); // NOI18N
// implementation
for (int i = 0; i < n; i++)
writerHelper.writeln(1, body[i]);
// end
writerHelper.writeln("}"); // NOI18N
getMemberList(INITIALIZER).add(writerHelper.toString());
| public void | addInterface(java.lang.String name)Adds an interface to the list of those implemented by this class.
if (!StringHelper.isEmpty(name))
_interfaces.add(name);
| private void | addMethod(java.lang.String name, int modifiers, java.lang.String returnType, java.lang.String[] parameterNames, java.lang.String[] parameterTypes, java.lang.String[] exceptions, java.lang.String[] body, java.lang.String[] comments, java.util.List methodList)
final String signature = createMethodSignature(name, modifiers,
returnType, parameterNames, parameterTypes, exceptions);
final FormattedWriter writerHelper = new FormattedWriter();
final int n = (body != null ? body.length : 0);
writerHelper.writeComments(comments);
// signature==null if we have an instance initializer
if (signature.length() > 0)
writerHelper.writeln(signature);
writerHelper.writeln("{"); // NOI18N
// implementation
for (int i = 0; i < n; i++)
writerHelper.writeln(1, body[i]);
// end
writerHelper.writeln("}"); // NOI18N
methodList.add(writerHelper.toString());
| public void | addMethod(java.lang.String name, int modifiers, java.lang.String returnType, java.lang.String[] parameterNames, java.lang.String[] parameterTypes, java.lang.String[] exceptions, java.lang.String[] body, java.lang.String[] comments)Adds a method to this class. Note that the type format in the
return type and parameter type strings must be package style
(that is - it can contain . but not / or $).
addMethod(name, modifiers, returnType, parameterNames, parameterTypes,
exceptions, body, comments, getMemberList(METHOD));
| private static java.lang.String | createMethodSignature(java.lang.String name, int modifiers, java.lang.String returnType, java.lang.String[] parameterNames, java.lang.String[] parameterTypes, java.lang.String[] exceptions)
int i, count = (parameterNames != null ? parameterNames.length : 0);
final FormattedWriter writerHelper = new FormattedWriter();
if (modifiers != 0)
writerHelper.write(Modifier.toString(modifiers) + ' ");
writerHelper.write(((returnType != null) ?
returnType + " " : "") + name); // NOI18N
// parameters
writerHelper.write(" ("); // NOI18N
for (i = 0; i < count; i++)
{
writeListElement(i, count, parameterTypes[i] + ' " +
parameterNames[i], writerHelper);
}
writerHelper.write(")"); // NOI18N
// exceptions
count = (exceptions != null ? exceptions.length : 0);
if (count > 0)
{
writerHelper.writeln();
writerHelper.write(1, "throws "); // NOI18N
for (i = 0; i < count; i++)
writeListElement(i, count, exceptions[i], writerHelper);
}
return writerHelper.toString();
| private java.util.List | getMemberList(java.lang.String memberType)
Object memberList = null;
if (!_maintainCategories)
memberType = MIXED;
memberList = _members.get(memberType);
if (memberList == null)
{
memberList = new ArrayList();
_members.put(memberType, memberList);
}
return (List)memberList;
| public void | setClassDeclaration(int modifiers, java.lang.String className, java.lang.String[] comments)Sets the information for the class declaration including modifiers,
name, and comments. Note that the name must not be fully qualified.
final FormattedWriter writerHelper = new FormattedWriter();
final String modifierString = Modifier.toString(modifiers);
writerHelper.writeComments(comments);
writerHelper.writeln(modifierString + ((modifierString.length() > 0)
? " " : "") + "class " + className); // NOI18N
_classDeclarationBlock = writerHelper.toString();
| public void | setSuperclass(java.lang.String name)Sets the superclass of this class. Note that the name format must
be package style (that is - it can contain . but not / or $).
_superclass = name;
| public java.lang.String | toString()Returns a string representation of this object.
final FormattedWriter writerHelper = new FormattedWriter();
writeClassDeclaration(writerHelper); // class declaration
writeMembers(writerHelper); // members
writerHelper.writeln("}"); // NOI18N // closing
writerHelper.writeln();
return writerHelper.toString();
| private void | writeClassDeclaration(FormattedWriter writerHelper)
// class declaration block
writerHelper.write(_classDeclarationBlock);
// extends
if (_superclass != null)
writerHelper.writeln(1, "extends " + _superclass); // NOI18N
// implements
if ((_interfaces != null) && (_interfaces.size() > 0))
{
writerHelper.write(1, "implements "); // NOI18N
writerHelper.write(StringHelper.arrayToSeparatedList(
_interfaces, COMMA_SEPARATOR));
writerHelper.writeln();
}
writerHelper.writeln("{"); // NOI18N
| private static void | writeListElement(int i, int count, java.lang.String string, FormattedWriter writerHelper)
int indent = ((i == 0) ? 0 : 1);
if (i == (count - 1))
writerHelper.write(indent, string);
else
writerHelper.writeln(indent, string + COMMA_SEPARATOR);
| private void | writeMembers(FormattedWriter writerHelper)
if (_maintainCategories)
{
writerHelper.writeList(1, getMemberList(FIELD));
writerHelper.writeList(1, getMemberList(INITIALIZER));
writerHelper.writeList(1, getMemberList(CONSTRUCTOR));
writerHelper.writeList(1, getMemberList(METHOD));
writerHelper.writeList(1, getMemberList(INNER_CLASS));
}
else
writerHelper.writeList(1, getMemberList(MIXED), true);
|
|