Skip to content

Commit

Permalink
Issue #11604: allows 3rd parties to expand module identification
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Oct 29, 2022
1 parent 3bbdf40 commit 058bdd4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
Expand Up @@ -46,7 +46,7 @@ private ModuleReflectionUtil() {
}

/**
* Gets checkstyle's modules (directly, not recursively) in the given packages.
* Gets checkstyle's modules classes (directly, not recursively) in the given packages.
*
* @param packages the collection of package names to use
* @param loader the class loader used to load Checkstyle package names
Expand All @@ -65,32 +65,14 @@ public static Set<Class<?>> getCheckstyleModules(
}

/**
* Checks whether a class may be considered as a checkstyle module. Checkstyle's modules are
* non-abstract classes, which are either checkstyle's checks, file sets, filters, file filters,
* {@code TreeWalker} filters, audit listener, or root module.
*
* @param clazz class to check.
* @return true if the class may be considered as the checkstyle module.
*/
public static boolean isCheckstyleModule(Class<?> clazz) {
return isValidCheckstyleClass(clazz)
&& (isCheckstyleTreeWalkerCheck(clazz)
|| isFileSetModule(clazz)
|| isFilterModule(clazz)
|| isFileFilterModule(clazz)
|| isTreeWalkerFilterModule(clazz)
|| isAuditListener(clazz)
|| isRootModule(clazz));
}

/**
* Checks whether a class extends 'AutomaticBean', is non-abstract, and has a default
* constructor.
* Checks whether a class may be considered as a checkstyle module.
* Checkstyle's modules are classes which extend 'AutomaticBean', is
* non-abstract, and has a default constructor.
*
* @param clazz class to check.
* @return true if a class may be considered a valid production class.
*/
public static boolean isValidCheckstyleClass(Class<?> clazz) {
public static boolean isCheckstyleModule(Class<?> clazz) {
return AutomaticBean.class.isAssignableFrom(clazz)
&& !Modifier.isAbstract(clazz.getModifiers())
&& hasDefaultConstructor(clazz)
Expand Down
Expand Up @@ -441,7 +441,7 @@ public void testGetShortFromFullModuleNamesThirdParty() {
* to initialize private field {@code PackageObjectFactory.thirdPartyNameToFullModuleNames}.
* Since the method and the field both are private, the {@link TestUtil} is required to ensure
* that the field is changed. Also, the expected exception should be thrown from the static
* method {@link ModuleReflectionUtil#getCheckstyleModules}, so {@link Mockito#mockStatic}
* method {@link ModuleReflectionUtil#isCheckstyleModule}, so {@link Mockito#mockStatic}
* is required to mock this exception.
*
* @throws Exception when the code tested throws an exception
Expand Down
Expand Up @@ -100,7 +100,7 @@ public void testChecksShouldHaveAllowedAbstractClassAsSuperclass() {
@Override
public boolean test(JavaClass input) {
final Class<?> clazz = input.reflect();
return ModuleReflectionUtil.isValidCheckstyleClass(clazz)
return ModuleReflectionUtil.isCheckstyleModule(clazz)
&& (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(clazz)
|| ModuleReflectionUtil.isFileSetModule(clazz));
}
Expand Down
Expand Up @@ -197,7 +197,7 @@ public class ImmutabilityTest {
@Override
public boolean test(JavaClass input) {
final Class<?> clazz = input.reflect();
return ModuleReflectionUtil.isValidCheckstyleClass(clazz)
return ModuleReflectionUtil.isCheckstyleModule(clazz)
&& (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(clazz)
|| ModuleReflectionUtil.isFileSetModule(clazz));
}
Expand Down
Expand Up @@ -78,22 +78,22 @@ public void testIsCheckstyleModule() {
@Test
public void testIsValidCheckstyleClass() {
assertWithMessage("Should return true when valid checkstyle class is passed")
.that(ModuleReflectionUtil.isValidCheckstyleClass(ValidCheckstyleClass.class))
.that(ModuleReflectionUtil.isCheckstyleModule(ValidCheckstyleClass.class))
.isTrue();
assertWithMessage("Should return false when invalid class is passed")
.that(
ModuleReflectionUtil.isValidCheckstyleClass(InvalidNonAutomaticBeanClass.class))
ModuleReflectionUtil.isCheckstyleModule(InvalidNonAutomaticBeanClass.class))
.isFalse();
assertWithMessage("Should return false when invalid class is passed")
.that(ModuleReflectionUtil.isValidCheckstyleClass(AbstractInvalidClass.class))
.that(ModuleReflectionUtil.isCheckstyleModule(AbstractInvalidClass.class))
.isFalse();
assertWithMessage("Should return false when invalid class is passed")
.that(ModuleReflectionUtil
.isValidCheckstyleClass(InvalidNonDefaultConstructorClass.class))
.isCheckstyleModule(InvalidNonDefaultConstructorClass.class))
.isFalse();
assertWithMessage("Should return false when forced invalid class is passed")
.that(
ModuleReflectionUtil.isValidCheckstyleClass(XpathFileGeneratorAstFilter.class))
ModuleReflectionUtil.isCheckstyleModule(XpathFileGeneratorAstFilter.class))
.isFalse();
}

Expand Down

0 comments on commit 058bdd4

Please sign in to comment.