From e8bfc0d01cebf1bb89c5dc9fa400f6e2e40c5be2 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Tue, 23 Jul 2024 20:05:11 +0200 Subject: [PATCH] [java] UnusedPrivateMethodRule - fixups from PR review --- .../UnusedPrivateMethodRule.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java index 399456c39d..1552bdfa44 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java @@ -57,24 +57,26 @@ public class UnusedPrivateMethodRule extends AbstractIgnoredAnnotationRule { // method calls/method refs that may refer to a method in the // first set, ie, not every call in the file. Set methodsUsedByAnnotations = - file.descendants(ASTAnnotation.class) - .crossFindBoundaries() - .toStream() - .flatMap(a -> Stream.concat( - a.getFlatValues().toStream() - .map(ASTMemberValue::getConstValue) - .filter(value -> value instanceof String) - .map(value -> (String) value) - .filter(StringUtils::isNotEmpty), - NodeStream.of(a) - .filter(it -> TypeTestUtil.isA("org.junit.jupiter.params.provider.MethodSource", it) - && it.getFlatValue("value").isEmpty()) - .parents().parents() - .filterIs(ASTMethodDeclaration.class) - .toStream() - .map(ASTMethodDeclaration::getName)) - ) - .collect(Collectors.toSet()); + file.descendants(ASTAnnotation.class) + .crossFindBoundaries() + .toStream() + .flatMap(a -> Stream.concat( + a.getFlatValues().toStream() + .map(ASTMemberValue::getConstValue) + .filter(String.class::isInstance) + .map(String.class::cast) + .filter(StringUtils::isNotEmpty), + NodeStream.of(a) + .filter(it -> TypeTestUtil.isA("org.junit.jupiter.params.provider.MethodSource", it) + && it.getFlatValue("value").isEmpty()) + .ancestors(ASTMethodDeclaration.class) + .firstOpt() + .map(ASTMethodDeclaration::getName) + .map(Stream::of) + .orElse(Stream.empty()) + ) + ) + .collect(Collectors.toSet()); Map> consideredNames = file.descendants(ASTMethodDeclaration.class)