Add ServiceUsage utility and OSGiServiceInjector test-rule #726
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is is intended to incubate/try out two utilities to simplify the correct usage of OSGi services:
unget
after it has been used. It is currently located inM2EUtils
and can be obtained via theuseService(Class)
respectivelyuseService(Class, Function)
.The implementation currently based on the E4's
ContextInjectionFactory
but it likely has to be implemented 'manually' by using a ServiceTracker per field that is closed after the evaluation because theEclipseContextOSGi
ungets the service-reference immediately. Using custom annotations like@InjectService
would also make it more clear what is happening and would allow parameters if necessary (to specify the element type of a collection?).The same functionality could be implemented for JUnit-5 using a corresponding
Extension
.I already applied them to a few locations within the m2e-core code-base and in tesla/m2e-core-tests#155 but there are more possible.
I think similar functionality could also be useful for many Eclipse-Platform projects because Services are there often not used correctly.
Finally it could maybe even be considered for inclusion into OSGi itself respectively osgi-test (although
useService()
requires Java-9+).@laeubi what do you think?