Skip to content

SONARJAVA-5493 Implement check for uses of ClassFile.build instead of transformClass#5222

Merged
romainbrenguier merged 13 commits intomasterfrom
romain/S7478
Jul 2, 2025
Merged

SONARJAVA-5493 Implement check for uses of ClassFile.build instead of transformClass#5222
romainbrenguier merged 13 commits intomasterfrom
romain/S7478

Conversation

@romainbrenguier
Copy link
Copy Markdown
Contributor

@romainbrenguier romainbrenguier commented Jun 30, 2025

@romainbrenguier romainbrenguier force-pushed the romain/S7478 branch 8 times, most recently from d45319e to 6db7667 Compare July 2, 2025 07:29
@romainbrenguier romainbrenguier marked this pull request as ready for review July 2, 2025 07:47
return new TreeMatcher<>(predicate.or(other.predicate));
}

public void setPredicate(Predicate<T> newPredicate) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I prefer if we keep the predicate immutable. What do you think about it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Totally agree, but it was not trivial. I think I got a decent solution with the recursive factory.

}

public static TreeMatcher<Tree> statementAt(int index, TreeMatcher<StatementTree> statementMatcher) {
return new TreeMatcher<>(tree -> ((tree instanceof BlockTree block && statementMatcher.check(block.body().get(index)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be good to add a test with out of bound index

public static void transformClassFile(Path path) throws IOException {
ClassFile classFile = ClassFile.of();
ClassModel classModel = classFile.parse(path);
byte[] newBytes = classFile.build( // Noncompliant
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think testing the message at least one time would be good

@sonarqube-next
Copy link
Copy Markdown

sonarqube-next bot commented Jul 2, 2025

@romainbrenguier romainbrenguier merged commit b7553c5 into master Jul 2, 2025
14 of 15 checks passed
@romainbrenguier romainbrenguier deleted the romain/S7478 branch July 2, 2025 14:13
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.

2 participants