I'd like to find a hack to infer the actual generic instance of another instance's var in runtime, without:
- Changing my method signature (adding class..., the obvious way)
-
Having to instanceOf all posiible subtypes in a hardcoded way
MyInterface<? extends Number> myInterface = whateverReturnsWildcardDoubleInterface(); Class<?> type = inferInstanceType(myInterface); assert type == Double.class; /** This is the method that represents the code I am looking for with the conrete signature**/ public <T extends Number> Class<T> inferInstanceType(MyInterface<T> myInterface){ return T.class; //Concrete T (can or cannot be the very Number) }
Ideally, it should return Double when T is particular subtype Integer,Double.. and Number when T is Number
I checked reflection, several "TypeResolver"/"GenericResolver" libs (as the one in Spring or others in Github), but I cannot fin a way to hack it.
Aucun commentaire:
Enregistrer un commentaire