question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return extension classes with wildcard type arguments

See original GitHub issue

Currently 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:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
dmitry-timofeevcommented, Mar 23, 2019

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.

1reaction
decebalscommented, Mar 13, 2019

We use the first workaround in the project.

That means that you use PF4J. Good 👍 😄.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found