How to add custom validation message to custom validation checkStyle from configuration window

I'm going to add custom validation as an eclipse-cs plugin and get stuck with the problem.

I created a java file with custom validation. Validation works, but I can’t change the user validation message because it is not in the field.

The Java file is as follows:

package myCheck.checks; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class MethodLimitCheck extends Check { private int max = 30; public int[] getDefaultTokens() { return new int[] { TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF }; } public void setMax(int limit) { max = limit; } public void visitToken(DetailAST ast) { // find the OBJBLOCK node below the CLASS_DEF/INTERFACE_DEF DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); // count the number of direct children of the OBJBLOCK // that are METHOD_DEFS int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF); // report error if limit is reached if (methodDefs > max) { log(ast.getLineNo(), "methodlimit", max); } } } 

The configuration block looks like this:

enter image description here

I can not change this message. I want Box to look like this, so I can install a custom message from the configuration window:

enter image description here

What changes have been made to my code or any file to do this?

+4
source share
1 answer

You can simply add a special message to your checkstyle.xml file:

 <module name="MethodLimitCheck"> <property name="max" value="42"/> <message key="methodlimit" value="my test message default"/> </module> 

It will then appear in the eclipse-cs dialog box. In your check, you can access custom messages using the getCustomMessages() ( javadoc ) method.

Update: To show your custom messages in the default eclipse-cs dialog box without adding the <message> element to your checkstyle.xml, you must create message.properties for your custom check as described here in the "Registration Errors" section . Then, in your Eclipse plugin containing custom validation, you add an element to your checkstyle-metadata.xml:

 <rule-metadata name="MethodLimit" internal-name="MethodLimitCheck" parent="TreeWalker"> <alternative-name internal-name="com.foo.bar.MethodLimitCheck"/> <description>...</description> <!-- property-metadata --> <message-key key="methodlimit" /> </rule-metadata> 
+1
source

Source: https://habr.com/ru/post/1496961/


All Articles