'Weird indentation with anonymous class as parameter of method
Java code indentation is weird after "Reformat Code" in IntelliJ IDEA(2021.2).
Code format config as below:
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="2" />
<option name="USE_TAB_CHARACTER" value="false" />
<option name="SMART_TABS" value="false" />
<option name="LABEL_INDENT_SIZE" value="0" />
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
<option name="USE_RELATIVE_INDENTS" value="false" />
</value>
</option>
The style config is from google-code-format. Detail config file.
The formatted code will be:
@Test
public void testMethod() {
... // other codes
Mockito.doAnswer(
new Answer() { // weird indentation, more 4 spaces.
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Files.touch(new File(localFile));
return null;
}
})
.when(strategyDownloadManager)
.download(Mockito.any(StrategyDownloadTask.class));
... // other codes
}
Why not format as below:
@Test
public void testMethod() {
... // other codes
Mockito.doAnswer(
new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Files.touch(new File(localFile));
return null;
}
})
.when(strategyDownloadManager)
.download(Mockito.any(StrategyDownloadTask.class));
... // other codes
}
As the other anonymous class is:
Thread thread =
new Thread(
new Runnable() { // annoymous class as parameter.
@Override
public void run() {}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
