Common Static Analysis Issue Remediation
In this guide we'll look at using OpenRewrite to perform an automated remediation for many issues identified by common static analysis tools.

Example Configuration

The Common Static Analysis Recipe consists of more than 50 types of issues and can be applied by including OpenRewrite's plug-in to your project and configuring the recipe:
Maven
Gradle
pom.xml
1
<plugin>
2
<groupId>org.openrewrite.maven</groupId>
3
<artifactId>rewrite-maven-plugin</artifactId>
4
<version>4.13.0</version>
5
<configuration>
6
<activeRecipes>
7
<recipe>org.openrewrite.java.cleanup.CommonStaticAnalysis</recipe>
8
</activeRecipes>
9
</configuration>
10
</plugin>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("org.openrewrite.rewrite") version("5.12.0")
4
}
5
6
rewrite {
7
activeRecipe("org.openrewrite.java.cleanup.CommonStaticAnalysis")
8
}
9
10
repositories {
11
mavenCentral() // rewrite is published to Maven Central
12
}
Copied!
At this point, you're ready to fix common static analysis issues by running mvn rewrite:run or gradlew rewriteRun.

Before and After

For the full list of changes this recipe will make, see its reference page.

Use explicit types on lambda arguments

Before
After
1
queue.findAll().forEach(msg -> {
2
WebSocketMessageBody toSend = conv.fromMap(msg.getMessage(), WebSocketMessageBody.class);
3
session.sendSync(toSend);
4
});
Copied!
1
queue.findAll().forEach((MessageQueue msg) -> {
2
WebSocketMessageBody toSend = conv.fromMap(msg.getMessage(), WebSocketMessageBody.class);
3
session.sendSync(toSend);
4
});
Copied!

No Double Brace Initialization

Before
After
1
class Menu {
2
static final List<String> menuItems = new ArrayList<>() {
3
{add("rice");add("beans");}
4
}
5
6
void newOrder(String main, String desert) {
7
List<String> menuItems = new ArrayList<>() {
8
{
9
add(main);
10
add(desert);
11
}
12
};
13
...
14
}
15
}
Copied!
1
class Menu {
2
static final List<String> menuItems;
3
static {
4
menuItems = new ArrayList<>();
5
menuItems.add("rice");
6
menuItems.add("beans");
7
}
8
9
void newOrder(String main, String desert) {
10
List<String> menuItems = new ArrayList<>();
11
menuItems.add(main);
12
menuItems.add(desert);
13
...
14
}
15
}
Copied!

Fields in a Serializable class should either be transient or serializable

Before
After
1
public class MessageExtBatch implements Serializable {
2
private ByteBuffer encodedBuff;
3
...
4
}
Copied!
1
public class MessageExtBatch implements Serializable {
2
private transient ByteBuffer encodedBuff;
3
...
4
}
Copied!

Known Limitations

We don't have OpenRewrite recipes implemented for all publicly available policies. If you find a violation you'd like automated, visit the rewrite repository and file an issue.
Last modified 10d ago