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 Jul 1, 2022
1 parent c8c2d8c commit 4845b2c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.google.common.reflect.ClassPath;
Expand All @@ -41,10 +42,43 @@
*/
public final class ModuleReflectionUtil {

/** Default identification predicate. */
private static final Predicate<Class<?>> DEFAULT_IDENTIFICATION_PREDICATE = clazz -> {
return isCheckstyleTreeWalkerCheck(clazz)
|| isFileSetModule(clazz)
|| isFilterModule(clazz)
|| isFileFilterModule(clazz)
|| isTreeWalkerFilterModule(clazz)
|| isAuditListener(clazz)
|| isRootModule(clazz);
};

/** Current identification predicate. */
private static Predicate<Class<?>> identificationPredicate = DEFAULT_IDENTIFICATION_PREDICATE;

/** Prevent instantiation. */
private ModuleReflectionUtil() {
}

/**
* Allows Checkstyle to understand more modules to work with it via a
* predicate. This creates a chain of predicates for identification
* purposes. If a prior predicate fails to identify a module, it will
* continue next on the chain until all say the class cannot be identified.
*
* @param newPredicate The provided predicate that will return {@code true}
* if the class given to it is identified as a type of module,
* otherwise {@code false} if it does not.
*/
public static void addNewModuleIdentification(Predicate<Class<?>> newPredicate) {
identificationPredicate = newPredicate.or(identificationPredicate);
}

/** Resets the identification process Checkstyle uses to know if a class is a module. */
public static void resetIdentification() {
identificationPredicate = DEFAULT_IDENTIFICATION_PREDICATE;
}

/**
* Gets checkstyle's modules (directly, not recursively) in the given packages.
*
Expand Down Expand Up @@ -74,13 +108,7 @@ public static Set<Class<?>> getCheckstyleModules(
*/
public static boolean isCheckstyleModule(Class<?> clazz) {
return isValidCheckstyleClass(clazz)
&& (isCheckstyleTreeWalkerCheck(clazz)
|| isFileSetModule(clazz)
|| isFilterModule(clazz)
|| isFileFilterModule(clazz)
|| isTreeWalkerFilterModule(clazz)
|| isAuditListener(clazz)
|| isRootModule(clazz));
&& identificationPredicate.test(clazz);
}

/**
Expand Down
Expand Up @@ -179,6 +179,29 @@ public void testKeepEclipseHappy() {
.isEqualTo(1);
}

@Test
public void testIdentification() {
assertWithMessage("Should return false when invalid class is passed by default")
.that(ModuleReflectionUtil
.isCheckstyleModule(InvalidWithDefaultConstructorClass.class))
.isFalse();

ModuleReflectionUtil.addNewModuleIdentification(
clazz -> clazz == InvalidWithDefaultConstructorClass.class);

assertWithMessage("Should return true with a new identification")
.that(ModuleReflectionUtil
.isCheckstyleModule(InvalidWithDefaultConstructorClass.class))
.isTrue();

ModuleReflectionUtil.resetIdentification();

assertWithMessage("Should return false when identification is reset")
.that(ModuleReflectionUtil
.isCheckstyleModule(InvalidWithDefaultConstructorClass.class))
.isFalse();
}

private static class ValidCheckstyleClass extends AutomaticBean {

// empty, use default constructor
Expand Down Expand Up @@ -380,4 +403,12 @@ protected void finishLocalSetup() {

}

private static class InvalidWithDefaultConstructorClass extends AutomaticBean {
@Override
protected void finishLocalSetup() {
// dummy method
}

}

}

0 comments on commit 4845b2c

Please sign in to comment.