Skip to content

Commit d5d3769

Browse files
committed
Document the way to enforce static final ThreadLocal fields using Checkstyle
1 parent 66a1c4a commit d5d3769

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,30 @@ If a usage of `ThreadLocal` doesn't fall into either of these categories, it can
15291529
There is an inspection "ThreadLocal field not declared static final" in IntelliJ IDEA which
15301530
corresponds to this item.
15311531

1532+
Static `ThreadLocal` fields could also be enforced using Checkstyle, using the following combination
1533+
of checks:
1534+
```xml
1535+
<!-- Enforce 'private static final' order of modifiers -->
1536+
<module name="ModifierOrder" />
1537+
1538+
<!-- Ensure all ThreadLocal fields are private -->
1539+
<!-- Requires https://github.com/sevntu-checkstyle/sevntu.checkstyle -->
1540+
<module name="AvoidModifiersForTypesCheck">
1541+
<property name="forbiddenClassesRegexpProtected" value="ThreadLocal"/>
1542+
<property name="forbiddenClassesRegexpPublic" value="ThreadLocal"/>
1543+
<property name="forbiddenClassesRegexpPackagePrivate" value="ThreadLocal"/>
1544+
</module>
1545+
1546+
<!-- Prohibit any ThreadLocal field which is not private static final -->
1547+
<module name="Regexp">
1548+
<property name="id" value="nonStaticThreadLocal"/>
1549+
<property name="format"
1550+
value="^\s*private\s+(ThreadLocal|static\s+ThreadLocal|final\s+ThreadLocal)"/>
1551+
<property name="illegalPattern" value="true"/>
1552+
<property name="message" value="Non-static final ThreadLocal"/>
1553+
</module>
1554+
```
1555+
15321556
<a name="threadlocal-design"></a>
15331557
[#](#threadlocal-design) TL.2. Doesn't a **`ThreadLocal` mask issues with the code, such as poor
15341558
control flow or data flow design?** Is it possible to redesign the system without using

0 commit comments

Comments
 (0)