Return extension classes with wildcard type arguments
See original GitHub issueCurrently a family of PluginManager#getExtensionClasses(Class<T> extensionType, …) return List<Class<T>>. That, however, makes mocking quite hard:
when(pluginManager.getExtensionClasses(Service.class, pluginId))
.thenReturn(ImmutableList.of(TestService1.class));
// Won't compile, because List<Class<TestService1>> is not assignable
// to List<Class<Service>>
when(pluginManager.getExtensionClasses(Service.class, pluginId))
.thenReturn(ImmutableList.of(TestService1.class, TestService2.class));
// Won't compile, because List<Class<? extends Service>> is not assignable
// to List<Class<Service>>
when(pluginManager.getExtensionClasses(Service.class, pluginId))
.thenReturn(ImmutableList.of((Class<Service>) TestService1.class);
// Won't compile, because Class<TestService1>> cannot be
// cast to Class<Service>.
I suggest returning List<Class<? extends T>> to make the interface more flexible.
Workaround 1
Add a method to perform conversion:
@SuppressWarnings("unchecked")
@SafeVarargs
private static <T> List<Class<T>> extensions(Class<? extends T>... extensions) {
return Arrays.stream(extensions)
.map(extensionClass -> (Class<T>) extensionClass)
.collect(toList());
}
Workaround 2
(Not really)
thenReturn(ImmutableList.of(
((Class<Service>) ((Class<? extends Service>) TestService1.class))
));
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Avoiding Returning Wildcard Types - java - Stack Overflow
Here's a type-safe way to store multiple instances of a given type in a map. The key is that you need to provide...
Read more >Bounded Type Parameters - The Java™ Tutorials
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in...
Read more >Wildcard Arguments in Types - Scala 3
We would like to use the underscore syntax _ to stand for an anonymous type parameter, aligning it with its meaning in value...
Read more >Wildcard Arguments in Types
We would like to use the underscore syntax _ to stand for an anonymous type parameter, aligning it with its meaning in value...
Read more >Java Generics - Wilcard Instantiations of Parameterized Types
The wildcard instantiations of parameterized types can be used as argument and return types of methods, as type arguments to other ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi, I think I’ll try in the coming week.-- Best wishes,Dmitry Timofeev21.03.2019, 15:23, “Decebal Suiu” notifications@github.com:@dmitry-timofeev Can you submit a PR for pf4j_3 branch? Thanks!
—You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or mute the thread.
That means that you use PF4J. Good 👍 😄.