Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnusedMethod] exempt @MethodSource with qualified method names #2411

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -254,14 +254,20 @@ private void handleMethodSource(MethodTree tree) {
.findAny()
// get the annotation value array as a set of Names
.flatMap(a -> getAnnotationValue(a, "value"))
.map(y -> asStrings(y).map(state::getName).collect(toImmutableSet()))
// remove all potentially unused methods whose simple name is referenced by the
// @MethodSource
.map(y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet()))
// remove all potentially unused methods referenced by the @MethodSource
.ifPresent(
referencedNames ->
unusedMethods
.entrySet()
.removeIf(e -> referencedNames.contains(e.getKey().getSimpleName())));
.removeIf(e -> {
Symbol unusedSym = e.getKey();
String simpleName = unusedSym.getSimpleName().toString();
return referencedNames.contains(simpleName)
|| referencedNames.contains(unusedSym.owner.getQualifiedName() + "#" + simpleName);
}
)
);
}
}

Expand Down
Expand Up @@ -223,4 +223,56 @@ public void methodSource() {
"}")
.doTest();
}

@Test
public void qualifiedMethodSource() {
helper
.addSourceLines(
"MethodSource.java",
"package org.junit.jupiter.params.provider;",
"public @interface MethodSource {",
" String[] value();",
"}")
.addSourceLines(
"Test.java",
"import java.util.stream.Stream;",
"import org.junit.jupiter.params.provider.MethodSource;",
"class Test {",
" @MethodSource(\"Test#parameters\")",
" void test() {}",
"",
"",
" private static Stream<String> parameters() {",
" return Stream.of();",
" }",
"}")
.doTest();
}

@Test
public void nestedQualifiedMethodSource() {
helper
.addSourceLines(
"MethodSource.java",
"package org.junit.jupiter.params.provider;",
"public @interface MethodSource {",
" String[] value();",
"}")
.addSourceLines(
"Test.java",
"import java.util.stream.Stream;",
"import org.junit.jupiter.params.provider.MethodSource;",
"class Test {",
" // @Nested",
" public class NestedTest {",
" @MethodSource(\"Test#parameters\")",
" void test() {}",
" }",
"",
" private static Stream<String> parameters() {",
" return Stream.of();",
" }",
"}")
.doTest();
}
}