-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Generics" discover function #92
Comments
I may be missing something, but I suspect the example as written is not solvable due to type erasure. If you had a collection that contained some elements, you could introspect them and guess what the generic type might have been, by trying to guess a least-generic common (super)type for the collection elements? |
I think this might be done because generic type arguments are retained in subclasses. I'll have a try at it! |
Hi @CodeFarmer, actually i have take a look on this issue and yes erasure as usual create this type of problem. Anyway, i think to use a simple solution that seems working fine. Simply getting the first element X1 of a non empty Collection and get the type of Object X1 and the problem seems resolved (but still to be implemented apart a simple POC). |
@antimoroma your solution is a start (if you are willing to ignore the empty list case in the first example, which @fborriello might not want). However, you will need to look at all the objects in the collection, as they may not all be the same type (say ArrayList[Employee] containing objects that are Manager and Engineer classes), and find the most-specific superclass that expresses all of the elements. |
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/
then we could recover the actual type parameter In conclusion, the only possible way seems that of looking up the element's type as you suggested above. |
@CodeFarmer for empty list i'm not the owner so we refer to @fborriello as you said, but the feature is needed to copy data from list, so (in this particular case i think) we can not take in charge this List or Map if it's empty could be and easy solution, we genereate an empty list on output. Good point about the observation about subclass in List (may be we need to investigate more about this i think in the mean time) , and i agree absolutely with @polarene last message |
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. |
Yes @fborriello, that could be the only way to go. Keep in mind that it will be simple if you have an empty collection or map, or if the elements are homogeneous ( This can get quite complicated, I know, but in the end maybe we could decide we don't want to go through all this hassle and just make Bull raise an exception if it finds a bounded type parameter. |
Yes, i think it could be useful to fix some prerequisite for this implementation (or for a first implementation):
|
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.
and
Additional context
The method should be added into the utility class:
ReflectionUtil
The text was updated successfully, but these errors were encountered: