Checkstyle Unable to instantiate class

233 views Asked by At

I'm trying to build my project with gradle, which has submodule with checkstyle configured. Submodule building ends successfully. But running whole project gives error

Unable to instantiate 'com.company.CustomChecker' class, it is also not possible to instantiate it as null. Please recheck that class name is specified as canonical name or read how to configure short name usage http://checkstyle.sourceforge.net/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly.

My build.gradle of submodule:

plugins {
   id 'checkstyle'
}
dependencies {
       checkstyle files('build/libs/submodule-project-name-1.0-SNAPSHOT.jar')
}
checkstyle {
toolVersion = '8.11'
  config = rootProject.resources.text.fromFile('submodule-project-name/config/checkstyle/checkstyle.xml')
}

My submodule-project-name/config/checkstyle/checkstyle.xml which have CustomChecker using:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="com.company.CustomChecker"/>
    </module>
</module>

but seems like when I build root module, it's kinda trying to find it in root module src which is not exists, not in submodule src. But when I run submodule build.gradle it works fine and is able to find the class.

Maybe I need to somehow configure checkstyle for root and configure that my CustomChecker path?

My checkstyle:

package com.company;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class CustomChecker extends AbstractCheck {


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

    @Override
    public int[] getAcceptableTokens() {
        return new int[0];
    }

    @Override
    public int[] getRequiredTokens() {
        return new int[0];
    }

    @Override
    public void visitToken(DetailAST packageDef) {
    // impl of visit token
    }
// logic of my class

}

Thank you a lot!

1

There are 1 answers

0
DarkKnights22 On

I had this issue and adding the project/module that contains the Checker class as a dependency to CheckStyle worked for me.

Example in Gradle Kotlin:

checkstyle {
    toolVersion = "10.12.2"

    configFile = file("${rootDir}\\config\\checkstyle\\checkstyle.xml")
 
    // This part here - common is the module that contains my checker class (the one that extends AbstractCheck)
    dependencies {
        checkstyle("com.github.checkstyle:checkstyle:checkstyle-10.12.2")
        checkstyle(project(":common"))
    }
}