Enumerate Java Fields

Sch*_*eel 3 java

I have a Java class with ~90 fields. I want to be able to do things to every field (generate an XML element for instance) without writing the same 5 lines of code with slight substitutions 90 times. In Objective C I think you can get to instance variables in ways similar to accessing Dictionary elements (ObjectForKey). Is there anything similar in Java such that I can get an array of the fields then do something to each of them?

Nik*_*bak 5

是的,它叫做Reflection API.

特别是,MyClass.class.getDeclaredFields()将返回此类声明的完整字段列表(有关详细信息,请参阅API)