Having an extremely long Java statement with many chained method calls can cause
compilation to fail with a StackOverflowError
when the compiler tries to
recursively process it.
This is a common problem in generated code.
As an alternative to extremely long chained method calls, e.g. for builders, consider something like the following for collections with hundreds or thousands of entries:
private static final ImmutableList<String> FEATURES = createFeatures();
private static final ImmutableList<String> createFeatures() {
ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
builder.add("foo");
builder.add("bar");
...
return builder.build();
}
over code like this:
private static final ImmutableList<String> FEATURES =
ImmutableList.<String>builder()
.add("foo")
.add("bar")
...
.build();
Suppress false positives by adding the suppression annotation @SuppressWarnings("DeeplyNested")
to the enclosing element.