"Generics" discover function
See original GitHub issueDescription
A really useful function, this project should offer, is the possibility to figure out the “Generic” type of a Collection or a Map. This function is also required to complete the implementation of a new Map Transformer (for more details please refer to issue: #88)
Desired solution
The ideal solution would be the implementation of a new method: getGenericType
that takes in input any kind of Java Collection or Map and returns its Generic type(s).
e.g.
List<String> v = new ArrayList<String>();
Class<?> genericClazz = getGenericType(v.getClass()); // will return: String
and
Map<String, Integer> v = new HashMap<String, Integer>();
Pair<Class<?>, Class<?>> typeMap = getGenericType(v.getClass()); // will return a Pair Class<?> keyClass = typeMap.getLeft(); // will return: String
Class<?> elemClass = typeMap.getRight(); // will return: Integer
Additional context
The method should be added into the utility class: ReflectionUtil
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Generics: How They Work and Why They Are Important - Oracle
This method returns the count of a specified coffee type for a given purchase. In order to perform this task effectively, the method...
Read more >Tutorial: Getting started with generics
This tutorial introduces the basics of generics in Go. With generics, you can declare and use functions or types that are written to...
Read more >The Basics of Java Generics - Baeldung
We write generic methods with a single method declaration, and we can call them with arguments of different types. The compiler will ensure...
Read more >Discover Generics on Swift by Sundell
Learn how to fully utilize Swift's powerful generics system to write reusable types, functions, extensions, and protocols.
Read more >Generics in Go Explained with Code Examples - freeCodeCamp
Generics allow our functions or data structures to take in several types that ... Let's explore more and see how far generics can...
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 FreeTop 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
Top GitHub Comments
I do agree with you all. This function is actually required for the Map Transformation module to figure out the key and the element type to understand how to get them transformed. The solution, you proposed, to discover the type from the first element of a collection/map could work. In case the collection/map would be empty we will return an empty List/Map. I’ll try with that solution and then get back to you. Thanks
Hi guys, after some tries, also involving @antimoroma in the end, I realized that this particular use case is unsolvable just using reflection. The trick I knew was described in this post: https://xebia.com/blog/acessing-generic-types-at-runtime-in-java/ but it works only for subtypes closing on actual type parameters. For example, if we had:
then we could recover the actual type parameter
String
. But when the function is handled aList<String>
, even if the instance has a generic superclass, the type parameter is not closed over at compile time (eg:new ArrayList<String>()
) so the type information is unknown and the method will return a typeE
, as declared in the source ofAbstractList
.In conclusion, the only possible way seems that of looking up the element’s type as you suggested above.