boolean hasConstants = false;
int length = fieldDeclarations != null ? fieldDeclarations.length : 0;
int enumConstantsLength = 0;
if (fieldDeclarations != null) {
for (int i = 0; i < length; i++) {
FieldDeclaration fieldDeclaration = fieldDeclarations[i];
if (fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
enumConstantsLength++;
} else {
break;
}
}
hasConstants = enumConstantsLength != 0;
if (enumConstantsLength > 1) {
Alignment enumConstantsAlignment = this.scribe.createAlignment(
Alignment.ENUM_CONSTANTS,
this.preferences.alignment_for_enum_constants,
enumConstantsLength,
this.scribe.scanner.currentPosition,
0, // we don't want to indent enum constants when splitting to a new line
false);
this.scribe.enterAlignment(enumConstantsAlignment);
boolean ok = false;
do {
try {
for (int i = 0; i < enumConstantsLength; i++) {
this.scribe.alignFragment(enumConstantsAlignment, i);
FieldDeclaration fieldDeclaration = fieldDeclarations[i];
fieldDeclaration.traverse(this, typeDeclaration.initializerScope);
if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_enum_declarations);
if (this.preferences.insert_space_after_comma_in_enum_declarations) {
this.scribe.space();
}
this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
if (fieldDeclaration.initialization instanceof QualifiedAllocationExpression) {
this.scribe.printNewLine();
}
}
}
ok = true;
} catch (AlignmentException e) {
this.scribe.redoAlignment(e);
}
} while (!ok);
this.scribe.exitAlignment(enumConstantsAlignment, true);
} else if (hasConstants) {
// only one enum constant
FieldDeclaration fieldDeclaration = fieldDeclarations[0];
fieldDeclaration.traverse(this, typeDeclaration.initializerScope);
if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_enum_declarations);
if (this.preferences.insert_space_after_comma_in_enum_declarations) {
this.scribe.space();
}