Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
*/
public class AutoBeanFactoryGenerator extends Generator {

private static final int MAX_ENUMS_PER_METHOD = 2000;

private GeneratorContext context;
private String simpleSourceName;
private TreeLogger logger;
Expand Down Expand Up @@ -372,14 +374,37 @@ private void writeEnumSetup(SourceWriter sw) {
list.add(entry.getKey());
}

sw.println("@Override protected void initializeEnumMap() {");
sw.indent();
int methodCount = 0;
int enumPerMethodCount = MAX_ENUMS_PER_METHOD + MAX_ENUMS_PER_METHOD;
for (Map.Entry<JEnumConstant, String> entry : model.getEnumTokenMap().entrySet()) {
if (enumPerMethodCount >= MAX_ENUMS_PER_METHOD) {
if (methodCount != 0) {
sw.outdent();
sw.println("}");
}
enumPerMethodCount = 0;
methodCount++;
sw.println("private void initializeEnumMap_%d() {", methodCount);
sw.indent();
}

// enumToStringMap.put(Enum.FOO, "FOO");
sw.println("enumToStringMap.put(%s.%s, \"%s\");", entry.getKey().getEnclosingType()
.getQualifiedSourceName(), entry.getKey().getName(), entry.getValue());

enumPerMethodCount++;
}

for (Map.Entry<String, List<JEnumConstant>> entry : map.entrySet()) {
if (enumPerMethodCount >= MAX_ENUMS_PER_METHOD) {
sw.outdent();
sw.println("}");
enumPerMethodCount = 0;
methodCount++;
sw.println("private void initializeEnumMap_%d() {", methodCount);
sw.indent();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication in these two sections bothers me... but I think not enough to ask for it to be factored out into a lambda or class that keeps the code more readable.

(Unless you were looking for a nudge to clean that up further.)

Pre-submit edit: Okay two more things that bother me here (but again not enough to require a change before merge):

  • the first initializedEnumMap_N call always has a zero suffix, but still uses %d, which is a little silly.
  • It is possible to emit a new method at the end of a loop, thus leaving it empty.

It could make slightly more sense to remove the first method creation call, and move each "do i need to start a new method" check to the top of the loops they belong to. Then, initialize enumPerMethodCount to greater than maxEnumsPerMethod and you'll always get an method created if needed. Last, the final outdent+println(}) can be conditional - don't print if methodCount==0.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct it could have created a new method at the end of a loop, thus leaving it empty. I moved the closing of the previous method and creation of a new method to the top of the loops.

One quick note the initializedEnumMap_N now start at 1 so the last loop to call the initializeEnumMap() now starts at 1 too.


String listExpr;
if (entry.getValue().size() == 1) {
JEnumConstant e = entry.getValue().get(0);
Expand All @@ -405,6 +430,19 @@ private void writeEnumSetup(SourceWriter sw) {
listExpr = sb.toString();
}
sw.println("stringsToEnumsMap.put(\"%s\", %s);", entry.getKey(), listExpr);

enumPerMethodCount += entry.getValue().size();
}

if (methodCount != 0) {
sw.outdent();
sw.println("}");
}

sw.println("@Override protected void initializeEnumMap() {");
sw.indent();
for (int i = 1; i <= methodCount; i++) {
sw.println("initializeEnumMap_%d();", i);
}
sw.outdent();
sw.println("}");
Expand Down