Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 542520

Summary: [JUnit 5] Warning The method xxx from the type X is never used locally is shown when using MethodSource
Product: [Eclipse Project] JDT Reporter: Pierre-Yves Bigourdan <pyvesdev>
Component: CoreAssignee: Pierre-Yves Bigourdan <pyvesdev>
Status: VERIFIED FIXED QA Contact: Stephan Herrmann <stephan.herrmann>
Severity: normal    
Priority: P3 CC: daniel_megert, jarthana, noopur_gupta, stephan.herrmann
Version: 4.10   
Target Milestone: 4.11 M3   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/134155
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=abf93b41f080158cf2a0f2630bf4a409a995f65b
https://bugs.eclipse.org/bugs/show_bug.cgi?id=546084
https://bugs.eclipse.org/bugs/show_bug.cgi?id=558873
Whiteboard:

Description Pierre-Yves Bigourdan CLA 2018-12-07 09:20:04 EST
Consider the following parameterized JUnit 5 test:

class MethodSourceTest {
    @ParameterizedTest
    @MethodSource("getIntegers")
    void testPositiveIntegers(Integer integer) {
        assertTrue(integer >= 0);
    }

    private static List<Integer> getIntegers() {
        return List.of(0, 5, 1);
    }
}

Eclipse will display the warning "The method getIntegers() from the type MethodSourceTest is never used locally" and will suggest to delete it as part of its quick fix options, even though getIntegers is actually used as a method source for the test. Given the increasing popularity of the latest version of the JUnit framework, would this be something we would like to deal with?
Comment 1 Stephan Herrmann CLA 2018-12-07 17:39:57 EST
Third-party annotations and reflection easily kill any static analysis ;-/

But JUnit is indeed close to home, so I'm not against this proposal.

As we speak about JUnit 5 annotations, are there more that define a reflective access to code?

May we assume that the mentioned methods cannot participate in any overloading?
Comment 2 Pierre-Yves Bigourdan CLA 2018-12-08 12:20:45 EST
As far as I know, there aren't any other JUnit 5 annotations that define a reflective access to code.

I'm unclear what you mean by your second question.

I've never digged into the source code for any of these static analysers. However, I'm happy to look into this in the coming weeks if no one else picks it up.
Comment 3 Stephan Herrmann CLA 2018-12-08 14:32:31 EST
(In reply to Pierre-Yves B. from comment #2)
> I'm unclear what you mean by your second question.

If you have @MethodSource("getIntegers") can we assume that there can legally be only one method of that name in the current class? Or do we have to select the no-args variant among same-named candidates? Or does the annotation apply to all methods (overloads) of that name?

> I've never digged into the source code for any of these static analysers.
> However, I'm happy to look into this in the coming weeks if no one else
> picks it up.

If you do so, the following may serve as starting points:

ProblemReporter.unusedPrivateMethod(AbstractMethodDeclaration)

ExtraCompilerModifiers.AccLocallyUsed

For an example how we handle well-known annotations, you can confer:
- TypeConstants.COM_GOOGLE_INJECT_INJECT
- TypeIds.T_ComGoogleInjectInject
and search your way from there :)

For setting up a JDT workspace I recommend using Oomph.
Comment 4 Pierre-Yves Bigourdan CLA 2018-12-15 07:26:00 EST
(In reply to Stephan Herrmann from comment #3)
> If you have @MethodSource("getIntegers") can we assume that there can
> legally be only one method of that name in the current class? Or do we have
> to select the no-args variant among same-named candidates? Or does the
> annotation apply to all methods (overloads) of that name?

Method source can only be used with methods without any parameters (otherwise throws exception "factory method must not declare formal parameters"), so we should keep on warning as we did before for overloaded methods.

Two interesting variations to my example above:
- when no name parameter is specified for the @MethodSource annotation, the same name as the test method is used as the method source (@MethodSource == @MethodSource("testPositiveIntegers")). This shouldn't require a lot of extra work to cover though.
- fully qualified names are supported, for instance @MethodSource("some_package.MethodSourceTest#getIntegers"). If someone decides to put method sources and test methods themselves in different classes, JUnit 5 will still cope happily even if the method sources are private in that different class. This means that ideally we would have to solve a problem such as "for any private static no-argument method in my entire project, is there any parameterized JUnit 5 test in my entire test suite that references it via a fully qualified MethodSource annotation?". I'm inclined to ignore this case and only deal with method sources that are in the same class, as this will probably be by far the most common case.

> For setting up a JDT workspace I recommend using Oomph.

I've already contributed quite a few patches to JDT-UI, my setup seems to work fine with JDT-Core as well. :)
Comment 5 Eclipse Genie CLA 2018-12-17 17:55:32 EST
New Gerrit change created: https://git.eclipse.org/r/134155
Comment 6 Stephan Herrmann CLA 2018-12-18 18:27:10 EST
(In reply to Pierre-Yves B. from comment #4)
> I've already contributed quite a few patches to JDT-UI, my setup seems to
> work fine with JDT-Core as well. :)

Please pardon my ignorance :)

Change is ready for merging once we have Jenkin's +1 after rebase.
Comment 7 Dani Megert CLA 2019-01-10 09:04:19 EST
M1 is done.
Comment 8 Pierre-Yves Bigourdan CLA 2019-01-11 15:59:08 EST
Shall we try to get this merged for next milestone? :)
Comment 10 Stephan Herrmann CLA 2019-01-13 15:29:47 EST
(In reply to Eclipse Genie from comment #9)
> Gerrit change https://git.eclipse.org/r/134155 was merged to [master].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/
> ?id=abf93b41f080158cf2a0f2630bf4a409a995f65b

Released for 4.11 M3

Thanks, Pierre-Yves!
Comment 11 Pierre-Yves Bigourdan CLA 2019-01-13 15:37:24 EST
Awesome! Thanks for your help and the review, Stephan!
Comment 12 Jay Arthanareeswaran CLA 2019-02-20 00:28:13 EST
Verified for 4.11 M3 using build I20190219-1800
Comment 13 Noopur Gupta CLA 2020-02-07 03:50:52 EST
See bug 558873 comment #12 which seems to be caused after this change in tests.
Comment 14 Stephan Herrmann CLA 2020-02-07 18:15:38 EST
(In reply to Noopur Gupta from comment #13)
> See bug 558873 comment #12 which seems to be caused after this change in
> tests.

See bug 558873 comment #19 :)