Java reflection get all private fields
Solution 1
It is possible to obtain all fields with the method getDeclaredFields()
of Class
. Then you have to check the modifier of each fields to find the private ones:
List<Field> privateFields = new ArrayList<>();
Field[] allFields = SomeClass.class.getDeclaredFields();
for (Field field : allFields) {
if (Modifier.isPrivate(field.getModifiers())) {
privateFields.add(field);
}
}
Note that getDeclaredFields()
will not return inherited fields.
Eventually, you get the type of the fields with the method Field.getType().
Solution 2
You can use Modifier
to determine if a field is private. Be sure to use the getDeclaredFields
method to ensure that you retrieve private fields from the class, calling getFields
will only return the public fields.
public class SomeClass {
private String aaa;
private Date date;
private double ccc;
public int notPrivate;
public static void main(String[] args) {
List<Field> fields = getPrivateFields(SomeClass.class);
for(Field field: fields){
System.out.println(field.getName());
}
}
public static List<Field> getPrivateFields(Class<?> theClass){
List<Field> privateFields = new ArrayList<Field>();
Field[] fields = theClass.getDeclaredFields();
for(Field field:fields){
if(Modifier.isPrivate(field.getModifiers())){
privateFields.add(field);
}
}
return privateFields;
}
}
Solution 3
Try FieldUtils
from apache commons-lang3:
FieldUtils.getAllFieldsList(Class<?> cls)
Solution 4
Using Java 8 :
Field[] fields = String.class.getDeclaredFields();
List<Field> privateFieldList = Arrays.asList(fields).stream().filter(field -> Modifier.isPrivate(field.getModifiers())).collect(
Collectors.toList());
Solution 5
Check if a Field is private
You could filter the Fields using Modifier.isPrivate:
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
// ...
Field field = null;
// retrieve the field in some way
// ...
Modifier.isPrivate(field.getModifiers())
on a single Field
object which returns true
if the field is private
Collect all Fields of a class
To collect the all the Fields use:
1) If you need only the fields of the the class without the fields taken from the class hierarchy you could simply use:
Field[] fields = SomeClass.class.getDeclaredFields();
2) If you don't want to reinvent the wheel and get all the fields of a class hierarchy you could rely upon Apache Commons Lang version 3.2+ which provides FieldUtils.getAllFieldsList
:
import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractSequentialList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Assert;
import org.junit.Test;
public class FieldUtilsTest {
@Test
public void testGetAllFieldsList() {
// Get all fields in this class and all of its parents
final List<Field> allFields = FieldUtils.getAllFieldsList(LinkedList.class);
// Get the fields form each individual class in the type's hierarchy
final List<Field> allFieldsClass = Arrays.asList(LinkedList.class.getFields());
final List<Field> allFieldsParent = Arrays.asList(AbstractSequentialList.class.getFields());
final List<Field> allFieldsParentsParent = Arrays.asList(AbstractList.class.getFields());
final List<Field> allFieldsParentsParentsParent = Arrays.asList(AbstractCollection.class.getFields());
// Test that `getAllFieldsList` did truly get all of the fields of the the class and all its parents
Assert.assertTrue(allFields.containsAll(allFieldsClass));
Assert.assertTrue(allFields.containsAll(allFieldsParent));
Assert.assertTrue(allFields.containsAll(allFieldsParentsParent));
Assert.assertTrue(allFields.containsAll(allFieldsParentsParentsParent));
}
}
Related videos on Youtube
user2152361
Updated on July 05, 2022Comments
-
user2152361 almost 2 years
I wonder is there a way to get all private fields of some class in java and their type.
For example lets suppose I have a class
class SomeClass { private String aaa; private SomeOtherClass bbb; private double ccc; }
Now I would like to get all private fields (
aaa
,bbb
,ccc
) of classSomeClass
(Without knowing name of all fields upfront) and check their type.-
Angad almost 9 years@atk Nobody's forcing you to answer; but for many of us landing on this question from Google, the top answer is a far more succinct explanation than reading through long posts on reflection. Even the long-tail of questions like this contribute to what SO is today.
-
Martin Schröder over 8 years
-
-
vallentin over 10 years@AfzaalAhmadZeeshan why did you change my edit "clazz" to "class", when we clearly need to use the variable "clazz" and not the keyword "class"
-
Afzaal Ahmad Zeeshan over 10 yearsI am sorry for that mistake, I thought this was a mistake as there was no variable assosiated with the document or the code anywhere! Pardon me for that..
-
vallentin over 10 years@AfzaalAhmadZeeshan there was a variable, it is in the arguments of the method. Though, do you care to revoke your change, since I can't change it.
-
Martin Schröder over 8 yearsThis excludes any inherited fields.
-
payne over 3 yearsI think the fact that I'm working against a compiled class makes it quite inconvenient because the returned fields have names such as
m0
,m1
, etc., and are of typejava.lang.reflect.Method
. This could be due to me trying to use reflection on a Spring Bean casted to an interface?