Skip to content

CLI: Cannot override default Checker #3515

@rnveach

Description

@rnveach

The first thing we define in any configuration file is the top level module, which is usually Checker.

<module name="Checker">

I thought if I changed this to something else, I could override the default Checker and use my own custom class.
Unfortunately, it is not possible to override Checker as the CLI hardcodes Checker as the class to run.
final Checker checker = new Checker();

It was already implied this was possible here, but it is actually not.

If logic is complicated and does not satisfy already configured Checker, create a new Checker and custom Check and do whatever user want.

I think we should get the main module to run from the configuration XML so it can be customized without having to also rewrite the CLI.

Example override configuration:

<?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="com.company.test.CustomChecker">
</module>

Example override class:

package com.company.test;

import java.io.File;
import java.util.List;

import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.RootModule;

public class CustomChecker extends AutomaticBean implements RootModule {
    @Override
    public void destroy() {
        // example
    }

    @Override
    public int process(List<File> files) throws CheckstyleException {
        // example
        return 0;
    }

    @Override
    public void addListener(AuditListener listener) {
        // example
    }

    @Override
    public void setModuleClassLoader(ClassLoader moduleClassLoader) {
        // example
    }
}

The main module will also need an interface (or base class) for atleast the following method calls that all main modules will need to implement, as required by the CLI:

  • public int process(List files) throws CheckstyleException
  • public void setModuleClassLoader(ClassLoader moduleClassLoader)
  • public void addListener(AuditListener listener)
  • public void destroy()


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions