Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all fields (even private and inherited) from class

I am making university project.
I need to get all fields from class. Even private and inherited. I tried to get all declared fields and then cast to super class and repeat. Fragment of my code:

private void listAllFields(Object obj) {     List<Field> fieldList = new ArrayList<Field>();     while (obj != null) {         fieldList.addAll(Arrays.asList(obj.getClass().getDeclaredFields()));         obj = obj.getClass().getSuperclass().cast(obj);     }     // rest of code 

But it does not work. tmpObj after casting is still the same class (not superclass).
I will appreciate any help how to fix casting problem, or how to retrieve these fields in different way.

Problem is not to gain access to fields, but to get names of fields!
I manages it that way:

private void listAllFields(Object obj) {     List<Field> fieldList = new ArrayList<Field>();     Class tmpClass = obj.getClass();     while (tmpClass != null) {         fieldList.addAll(Arrays.asList(tmpClass .getDeclaredFields()));         tmpClass = tmpClass .getSuperclass();     }     // rest of code 
like image 718
Michał Herman Avatar asked Apr 30 '13 08:04

Michał Herman


People also ask

How do you get all fields in a class?

The list of all declared fields can be obtained using the java. lang. Class. getDeclaredFields() method as it returns an array of field objects.

How to get inherited fields in java?

The only way we have to get only inherited fields is to use the getDeclaredFields() method, as we just did, and filter its results using the Field::getModifiers method. This one returns an int representing the modifiers of the current field. Each possible modifier is assigned a power of two between 2^0 and 2^7.

Can fields be inherited?

The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass.

What is the reflection in Java?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


1 Answers

obj = obj.getClass().getSuperclass().cast(obj); 

This line does not do what you expect it to do. Casting an Object does not actually change it, it just tells the compiler to treat it as something else.

E.g. you can cast a List to a Collection, but it will still remain a List.

However, looping up through the super classes to access fields works fine without casting:

Class<?> current = yourClass; while(current.getSuperclass()!=null){ // we don't want to process Object.class     // do something with current's fields     current = current.getSuperclass(); } 

BTW, if you have access to the Spring Framework, there is a handy method for looping through the fields of a class and all super classes:
ReflectionUtils.doWithFields(baseClass, FieldCallback)
(also see this previous answer of mine: Access to private inherited fields via reflection in Java)

like image 116
Sean Patrick Floyd Avatar answered Oct 16 '22 15:10

Sean Patrick Floyd