How to add a leading statement for each message in checkStyle

Viewed 24

I want to add a leading statement for each message in checkStyle, whether it is a custom message or a default message ,in order to identify that the prompt was sent by the checker

I use idea-plugins so I need to define the check rules in the xml file

For example, I added the following statement to my configuration file

<module name="ParameterName " />

When I write a code that doesn't match this rule, I want the format of the prompt message to be Custom Prompt + Default Prompt

I know it is possible to write it this way but it is too redundant, so I am looking for an efficient solution.

 <module name="ParameterName"> 
  <message key="name.invalidPattern" value="some custom messages"/> 
 </module>
1 Answers

As of Checkstyle 5 all checks can be configured to report custom, configuration specific messages instead of the Checkstyle default messages. This can be useful in cases where the check message should reference corresponding sections in a coding style document or the default is too generic for developers to understand.

An example usage is:

<module name="MemberName">
    <property name="format" value="^m[a-zA-Z0-9]*$"/>
    <message key="name.invalidPattern" value="Member ''{0}'' must start with a lowercase ''m'' (checked pattern ''{1}'')."/>
</module>
  

Each check configuration element can have zero or more message elements. Every check uses one or more distinct message keys to log violations. If you want to customize a certain message you need to specify the message key in the key attribute of the message element.

The value attribute specifies the custom message pattern, as shown in the example above. Placeholders used in the default message can also be used in the custom message. Note that the message pattern must be a valid java.text.MessageFormat style pattern, so be careful about curly braces outside a placeholder definition.

The obvious question is how do you know which message keys a Check uses, so that you can override them? You can examine all keys in the check's specific configuration documentation. Each check has a section called 'Violation Messages'. This section lists every key the check uses and links to the default message used by checkstyle.

Related