Skip to content

codebox/checkstyle-import-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

checkstyle-import-checker

AbstractImportChecker.java simplifies the process of writing custom CheckStyle rules that examine class imports.

Note that Checkstyle contains a number of quite flexible built-in rules for managing imports, you should obviously use these if they meet your needs.

To implement your own custom import checker just sub-class AbstractImportChecker and provide an implementation for onImport():

public class ImportChecker extends AbstractImportChecker {
    @Override
    public void onImport(String packageName, String importName) {
        if (! isImportAllowedInPackage(packageName, importName)) {
            logError(String.format("Import %s not allowed in package %s", importName, packageName));
        }
    }

    private boolean isImportAllowedInPackage(String packageName, String importName) {
        return true; // custom logic here
    }
}

The DottedPath helper class can be used to check for sub-packages and package membership:

private boolean isImportAllowedInPackage(String packageName, String importName){
    DottedPath packagePath = new DottedPath(packageName);
    DottedPath importPath = new DottedPath(importName);

    return importPath.isChildOf(packagePath);
}

About

A Java class to simplify writing custom Checkstyle rules that examine class imports

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages