alibaba java coding guidelines idea inside write-action
时间: 2024-02-23 19:59:18 浏览: 24
The Alibaba Java coding guidelines recommend using `WriteCommandAction` when modifying documents in IntelliJ IDEA.
`WriteCommandAction` is a class provided by the IntelliJ IDEA API that allows developers to perform write actions on documents in a safe and thread-safe manner. When you use `WriteCommandAction`, IntelliJ IDEA will ensure that the action is performed in a write-safe context and that any necessary locks are acquired to prevent conflicts with other write actions.
To use `WriteCommandAction`, you can create an instance of the class and pass in a `Runnable` or `Computable` that performs the desired write action. For example:
```
WriteCommandAction.runWriteCommandAction(project, () -> {
// Perform write actions here
document.setText("Hello, world!");
});
```
This ensures that the `setText` method is performed in a write-safe context and that any necessary locks are acquired to prevent conflicts with other write actions.
Overall, using `WriteCommandAction` is a best practice for modifying documents in IntelliJ IDEA, as recommended by the Alibaba Java coding guidelines.