Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Field Information Using Java Reflection

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
0
0
US
I need to get a list of field names and their types within a class and do formatted output.

Often the type is a generic such as the following:

Code:
java.util.List<myType>

In this case, I would want to print a line that looks like:

Code:
java.util.List<myType> myFieldName

Questions:

1) How do I determine whether the type is a generic?
2) How do I determine and what the parameterized type is? By "parameterized type," I mean the "myType" in the example above.
3) Are the angle brackets available in any list of modifiers for the field?

I'm using the following loop to get my information:
Code:
Field[] f = myClass.getDeclaredFields();
for ( Field aField : f)
{
  String fldName = aField.getName();
  Class fldTypeCls = aField.getType();
  String fldType = fldTypeCls.getSimpleName();
}

Let me know if my post does not make sense -- when it came time to write this, I had a hard time finding the words!

Thanks,
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top