1

I am trying to write custom check and use it from Eclipse.

I have wrote one MethodLimitCheck.java and one myChecker.xml file for that.


java file : MethodLimitCheck.java

package myCheck;

import com.puppycrawl.tools.checkstyle.api.*;

public class MethodLimitCheck extends Check
{
    private static final int DEFAULT_MAX = 10;
    private int max = DEFAULT_MAX;

    public void setMax(int limit)
    {
        max = limit;
    }

    @Override
    public int[] getDefaultTokens()
    {
        return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
    }

    @Override
    public void visitToken(DetailAST ast)
    {
        DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);

        int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF);

        if (methodDefs > this.max) 
        {
            log(ast.getLineNo(),
                    "too many methods, only " + this.max + " are allowed");
        }
    }
}

and xml file : myChecker.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="myCheckStyle.MethodLimitCheck">
      <property name="max" value="5" />
    </module>
  </module>
</module>

how can I use this check in eclipse?

I have taken this example from CheckStyle site. But how to use that in Eclipse?

I need to get the warnings in eclipse like when I use the available checks and form a custom configuration, That can be used from project->property->checkStyle.

I am new with this tool, so if I'm missing something basic, please correct me.

2 Answers 2

3

You need eclipse-cs plugin. It integrates checkstyle to eclipse and allows providing custom checks. Also, look at sevntu.checkstyle. It is extension to the eclipse-cs plugin. Sevntu.checkstyle adds many useful checks and allows to append your own checks(see wiki on github). Maybe it contains check that you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, using sevntu-checkstyle extension you can easily add custom Checkstyle checks to list of available checks of eclipse-cs plugin. It is the easiest and fastest solution. We tested sevntu-checkstyle extension widely by writing our own checks for Checkstyle, integrating them into it and checking huge production code amounts in Eclipse with eclipse-cs plugin + sevntu-checkstyle extension installed.
1

You must basically write your own little Eclipse plugin to do that. When the plugin works, you will see your custom check in the Checkstyle dialog provided by eclipse-cs.

Here's a tutorial on how to do that courtesy of the eclipse-cs documentation. There is also sample code.

Once it's done, the whole thing seems to be simple. But be patient, it may take a whole day to really get the hang of this if you are new to Eclipse plugins.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.