How to get a returned List of Objects object type from a Java method? -


i have getter method in class returns list of objects. looks this:

public list <cars> getcars() {  // code here  } 

the class contains other getters one. in class, want getter methods contained in first class , display name of methods , returned data types.

i'm able name of above method (getcars) , it's returned data type (list). however, can't seem "cars" type of object list contains. best can "objecttype". there way of getting "cars" displayed? i've read type erasure , how generics removed in bytecode it's used benefit of java compiler. problem related type erasure?

is possible word "cars" displayed? when read type erasure there seems way generics list examples i've seen string , integer , not objects.

get generic type of java.util.list

thanks

you can hold of (generic) information method using standard java reflection:

class<?> yourclass = class.forname("a.b.c.classthathasthemethod"); method getcarsmethod = yourclass.getmethod("getcars"); type returntype = getcarsmethod.getgenericreturntype(); 

now there's not particularly elegant way handle returntype variable (that know of). plain class, or of subinterfaces (e.g. parameterizedtype, in case). in past when i've done this, i've had use instanceof , casting handle cases. example:

if (returntype instanceof class<?>) {     class<?> returnclass = (class<?>)returntype;     // class } else if (returntype instanceof parameterizedtype) {     // case in example     parameterizedtype pt = (parameterizedtype)returntype;     type rawtype = pt.getrawtype();     type[] genericargs = pt.getactualtypearguments();      // here `rawtype` class "java.util.list",     // , `genericargs` 1 element array containing     // class "cars".  overall, pt equivalent list<cars>     // you'd expect.     // in order work out, need     // call method recursively,     // convert `type` `class`... } else if (...) // handle wildcardtype, genericarraytype, typevariable completeness 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -