Java reflection get all private fields
Zastanawiam się, czy jest sposób, aby uzyskać wszystkie prywatne pola jakiejś klasy w Javie i ich typ.
Na przykład załóżmy, że mam klasę
class SomeClass {
private String aaa;
private SomeOtherClass bbb;
private double ccc;
}
Teraz chciałbym uzyskać wszystkie prywatne pola (aaa
, bbb
, ccc
) klasy SomeClass
(bez znajomości nazwy wszystkich pól z góry) i sprawdzenia ich typu.
6 answers
Możliwe jest uzyskanie wszystkich pól metodą getDeclaredFields()
z Class
. Następnie musisz sprawdzić modyfikator każdego pola, aby znaleźć prywatne:
List<Field> privateFields = new ArrayList<>();
Field[] allFields = SomeClass.class.getDeclaredFields();
for (Field field : allFields) {
if (Modifier.isPrivate(field.getModifiers())) {
privateFields.add(field);
}
}
Zauważ, że getDeclaredFields()
nie zwróci odziedziczonych pól.
W końcu otrzymujesz Typ pól za pomocą metody Field.getType () .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-03 09:08:57
Możesz użyć Modifier
, aby określić, czy pole jest prywatne. Upewnij się, że używasz metody getDeclaredFields
, aby upewnić się, że odzyskujesz prywatne pola z klasy, wywołanie getFields
zwróci tylko publiczne pola.
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;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-05 21:08:46
Spróbuj FieldUtils
z Apache commons-lang3:
FieldUtils.getAllFieldsList(Class<?> cls)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-25 09:22:31
Sprawdź, czy pole jest prywatne
Możesz filtrować pola za pomocą modyfikatora .isPrivate :
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
// ...
Field field = null;
// retrieve the field in some way
// ...
Modifier.isPrivate(field.getModifiers())
Na pojedynczym obiekcie Field
, który zwraca true
Jeśli pole to private
Zbierz wszystkie pola klasy
To Zbierz wszystkie pola użyj:
1) jeśli potrzebujesz tylko pól klasy bez pól wziętych z hierarchii klas, możesz po prostu użyć:
Field[] fields = SomeClass.class.getDeclaredFields();
2) Jeśli nie chcesz Odkryj na nowo koło i uzyskaj wszystkie pola hierarchii klas, na których możesz polegać Apache Commons lang Wersja 3.2+, która zapewnia 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));
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-12 09:38:35
Używanie Javy 8:
Field[] fields = String.class.getDeclaredFields();
List<Field> privateFieldList = Arrays.asList(fields).stream().filter(field -> Modifier.isPrivate(field.getModifiers())).collect(
Collectors.toList());
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-26 08:43:02
Czy masz na myśli
Field[] fields = SomeClass.class.getDeclaredFields();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-09 20:14:13