Skip to content

Issue #13501: Kill mutation for AnnotationUtil#13415

Merged
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:ut2
Aug 4, 2023
Merged

Issue #13501: Kill mutation for AnnotationUtil#13415
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:ut2

Conversation

@Kevin222004
Copy link
Copy Markdown
Contributor

@Kevin222004 Kevin222004 commented Jul 21, 2023

Issue #13501: Kill mutation for AnnotationUtil


Mutations

<mutation unstable="false">
<sourceFile>AnnotationUtil.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.utils.AnnotationUtil</mutatedClass>
<mutatedMethod>containsAnnotation</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>if (ast == null) {</lineContent>
</mutation>


Explaination

Method 1

public static boolean containsAnnotation(final DetailAST ast) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
final DetailAST holder = getAnnotationHolder(ast);
return holder != null && holder.findFirstToken(TokenTypes.ANNOTATION) != null;
}

This method

is mutated and the usage of this method is only in the check PackageAnnotationCheck
public void visitToken(final DetailAST ast) {
final boolean containsAnnotation =
AnnotationUtil.containsAnnotation(ast);

at line no 109 as this is visit token and no other such operation is perfoming on ast it will only be the acceptaed token which is PACKAGE_DEF so
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}

this removal will not make any affect

Second reason
may be stronger then above one
lets suppose above we have not condition ast == null and by chance ast is null then while calling

final DetailAST holder = getAnnotationHolder(ast);

getAnnotationHolder already have this condition
public static DetailAST getAnnotationHolder(DetailAST ast) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
.

Method 2

public static boolean containsAnnotation(DetailAST ast, Set<String> annotations) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}

in which similar mutation at line 114

know this method is called by one method in same class at

public static boolean hasOverrideAnnotation(DetailAST ast) {
return containsAnnotation(ast, OVERRIDE_ANNOTATIONS);
}

and this method is used by 5 Checks

  1. ParameterNumberCheck :- https://checkstyle.org/checks/sizes/parameternumber.html#ParameterNumber
  2. MissingOverrideCheck :- https://checkstyle.org/checks/annotation/missingoverride.html#MissingOverride
  3. MethodNameCheck :- https://checkstyle.org/checks/naming/methodname.html#MethodName
  4. IllegalTypeCheck :- https://checkstyle.org/checks/coding/illegaltype.html
  5. IllegalThrowsCheck :- https://checkstyle.org/checks/coding/illegalthrows.html

1) ParameterNumberCheck :-
usage of method is at
https://github.com/checkstyle/checkstyle/blob/
8804126/src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/ParameterNumberCheck.java#L177-L181
line 180 know this method in ast is called only at visit token at line 164

public void visitToken(DetailAST ast) {
final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
final int count = params.getChildCount(TokenTypes.PARAMETER_DEF);
if (count > max && !shouldIgnoreNumberOfParameters(ast)) {

over here directly ast is used which is in parameter so it is also bever going to null

2) MissingOverrideCheck :-
Similar reason as above the usage is at

public void visitToken(final DetailAST ast) {
final boolean containsTag = containsInheritDocTag(ast);
if (containsTag && !JavadocTagInfo.INHERIT_DOC.isValidOn(ast)) {
log(ast, MSG_KEY_TAG_NOT_VALID_ON,
JavadocTagInfo.INHERIT_DOC.getText());
}
else {
boolean check = true;
if (javaFiveCompatibility) {
final DetailAST defOrNew = ast.getParent().getParent();
if (defOrNew.findFirstToken(TokenTypes.EXTENDS_CLAUSE) != null
|| defOrNew.findFirstToken(TokenTypes.IMPLEMENTS_CLAUSE) != null
|| defOrNew.getType() == TokenTypes.LITERAL_NEW) {
check = false;
}
}
if (check
&& containsTag
&& !AnnotationUtil.hasOverrideAnnotation(ast)) {
log(ast, MSG_KEY_ANNOTATION_MISSING_OVERRIDE);
}
}
}

the method is used at line 248 and similar ast is direclty passed from parameter.

3) MethodNameCheck :-
Similar reason for method name check

public void visitToken(DetailAST ast) {
if (!AnnotationUtil.hasOverrideAnnotation(ast)) {
// Will check the name against the format.
super.visitToken(ast);
}

4) IllegalThrowsCheck :-
Similar reason

private boolean isIgnorableMethod(DetailAST methodDef) {
return shouldIgnoreMethod(methodDef.findFirstToken(TokenTypes.IDENT).getText())
|| ignoreOverriddenMethods
&& AnnotationUtil.hasOverrideAnnotation(methodDef);
}

5) IllegalTypeCheck :-
Similar reasons

private boolean isCheckedMethod(DetailAST ast) {
final String methodName =
ast.findFirstToken(TokenTypes.IDENT).getText();
return isVerifiable(ast) && !ignoredMethodNames.contains(methodName)
&& !AnnotationUtil.hasOverrideAnnotation(ast);
}

Second reason

final DetailAST firstMatchingAnnotation = findFirstAnnotation(ast, annotationNode -> {

call
private static DetailAST findFirstAnnotation(final DetailAST ast,
Predicate<DetailAST> predicate) {
final DetailAST holder = getAnnotationHolder(ast);

and it is calling
public static DetailAST getAnnotationHolder(DetailAST ast) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}

so here ast is checked again

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@romani
https://checkstyle.semaphoreci.com/jobs/01ed458d-ea27-4b03-9c02-06e28c81f883

[ERROR] Errors: 03:03
[ERROR]   ParenPadCheckTest.testNoStackoverflowError:466->AbstractModuleTestSupport.verifyWithLimitedResources:390 » Execution java.lang.Error: Error was thrown while processing /home/semaphore/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/parenpad/InputParenPadNoStackoverflowError.java03:03
[INFO] 

Again NoStackoverFlowError

As far I don't remember we have done any changes in favour of pitest which create this type of issue

Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ok to remove such null validation.
I see no value in them, it is still exception, absolutely sae details user will get from NPE.

throw new IllegalArgumentException("annotations cannot be null"); also can be removed and all similar

Copy link
Copy Markdown
Member

@Vyom-Yadav Vyom-Yadav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Vyom-Yadav Vyom-Yadav assigned rdiachenko and unassigned Vyom-Yadav Aug 1, 2023
Copy link
Copy Markdown
Member

@rdiachenko rdiachenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rdiachenko
Copy link
Copy Markdown
Member

@Kevin222004 please resolve conflicts

@romani
Copy link
Copy Markdown
Member

romani commented Aug 2, 2023

@Kevin222004 , conflict

@Kevin222004 Kevin222004 changed the title Issue #13321: Kill mutation for AnnotationUtil Issue #13501: Kill mutation for AnnotationUtil Aug 2, 2023
@Kevin222004 Kevin222004 force-pushed the ut2 branch 2 times, most recently from 802a877 to c5aa526 Compare August 2, 2023 16:56
@romani
Copy link
Copy Markdown
Member

romani commented Aug 2, 2023

@Kevin222004 , CI is failing

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Can you please re run circle ci

@Vyom-Yadav
Copy link
Copy Markdown
Member

Can you please re run circle ci

@Kevin222004 Just add some change, push the commit, remove that change, push again, that will re trigger the CI with current changes. You can use this if no one is around.

@romani
Copy link
Copy Markdown
Member

romani commented Aug 3, 2023

CIs were restarted

@romani
Copy link
Copy Markdown
Member

romani commented Aug 4, 2023

@Kevin222004 , please resolve conflict

@Kevin222004
Copy link
Copy Markdown
Contributor Author

conflict resolved

@romani romani merged commit 1cef660 into checkstyle:master Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants