Java reflection get all private fields

后端 未结 6 783
北恋
北恋 2020-12-07 22:29

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 {
            


        
6条回答
  •  悲&欢浪女
    2020-12-07 23:06

    Using Java 8 :

    Field[] fields = String.class.getDeclaredFields();
    List privateFieldList = Arrays.asList(fields).stream().filter(field -> Modifier.isPrivate(field.getModifiers())).collect(
            Collectors.toList());
    

提交回复
热议问题