When I apply default google-checkstyle.xml format, the following will give a warning:
Map<String, Object> payment = new LinkedHashMap<>() {{
put("customer_number", "123456");
}};
'block' child has incorrect indentation level 12, expected level should be one of the following: 16, 20. (169:13) [Indentation]
This is because checkstyle expects the following format:
Map<String, Object> payment = new LinkedHashMap<>() {{
put("customer_number", "123456");
}};
But I'd prefer my custom format. How could I change this in checkstyle? The error comes from definition:
<module name="Indentation">
<property name="basicOffset" value="4"/>
....
</module>
But I cannot change this because the basicOffset also applies to all other lines.
Question: is it possible to somehow exclude static initialized Maps with double-brackets from this check? Like the new LinkedHashMap<>() {{.
If you don't care about the order, simply use
Map.oflike Joachim Sauer already suggested in his comment.If you need insertion order, you can use the following instead of the anonymous sub class:
This does look weird, using a
List<Map.Entry<String, Object>>as intermediate object, and thetoMapthat requires a merge function that's never used, but it gets the job done.