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.

"Generics" discover function

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
fborriellocommented, Sep 24, 2019

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

1reaction
polarenecommented, Sep 24, 2019

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:

class Names implements List<String> {...}

Names n = new Names();
Class<?> cls = getGenericType(n.getClass());

then we could recover the actual type parameter String. But when the function is handled a List<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 type E, as declared in the source of AbstractList.

In conclusion, the only possible way seems that of looking up the element’s type as you suggested above.

Read more comments on GitHub >

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

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