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 Aug 4, 2022
1 parent 6f6986a commit be9410b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 68 deletions.
68 changes: 7 additions & 61 deletions .ci/pitest-suppressions/pitest-utils-suppressions.xml
Expand Up @@ -633,73 +633,19 @@
<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>getCheckstyleModules</mutatedMethod>
<mutatedMethod>addNewModuleIdentification</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator</mutator>
<description>replaced call to java/util/stream/Stream::filter with receiver</description>
<lineContent>.filter(ModuleReflectionUtil::isCheckstyleModule)</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>&amp;&amp; (isCheckstyleTreeWalkerCheck(clazz)</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>|| isAuditListener(clazz)</lineContent>
<description>replaced call to java/util/function/Predicate::or with receiver</description>
<lineContent>identificationPredicate = newPredicate.or(identificationPredicate);</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>|| isFileFilterModule(clazz)</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>|| isFileSetModule(clazz)</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>|| isFilterModule(clazz)</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
<description>removed conditional - replaced equality check with true</description>
<lineContent>|| isRootModule(clazz));</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>ModuleReflectionUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil</mutatedClass>
<mutatedMethod>isCheckstyleModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>|| isTreeWalkerFilterModule(clazz)</lineContent>
<mutatedMethod>getCheckstyleModules</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator</mutator>
<description>replaced call to java/util/stream/Stream::filter with receiver</description>
<lineContent>.filter(ModuleReflectionUtil::isCheckstyleModule)</lineContent>
</mutation>

<mutation unstable="false">
Expand Down
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 @@ -381,4 +404,12 @@ protected void finishLocalSetup() {

}

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

}

}

0 comments on commit be9410b

Please sign in to comment.