Reputation: 8768
We're using checkstyle and PMD in our project and I'm looking for a way to enforce newlines between multiple catchs in one row, e.g.
} catch(IOException | IllegalArgumentException ex) {
...
}
should fail the validation whereas
} catch(IOException
| IllegalArgumentException ex) {
...
}
should pass.
I'm not looking for alternatives like randomly working code formatters which introduce dependency on the IDE.
We're using checkstyle and PMD through Maven.
Upvotes: 0
Views: 50
Reputation: 1910
A simple XPath-based rule for PMD could use this XPath expression:
<rule name="MultipleCatchTypesOnSeparateLines"
language="java"
message="Use a separate line for each catch type"
class="net.sourceforge.pmd.lang.rule.XPathRule"
<description>TODO</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CatchStatement/FormalParameter[@SingleLine = true()]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
</rule>
A more complex one, that would detect cases, where you have e.g. 3 catchs on 2 lines (which should be on three lines):
<rule name="MultipleCatchTypesOnSeparateLines"
language="java"
message="Use a separate line for each catch type"
class="net.sourceforge.pmd.lang.rule.XPathRule"
<description>TODO</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
for $a in //CatchStatement/FormalParameter
return for $b in (1 to count($a/Type))
return $a[Type[$b][@BeginLine = $a/Type[$b + 1]/@BeginLine]]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
</rule>
Please note: With PMD you cannot verify that the |
character is on the next line. This token is not available in the AST.
Upvotes: 1