get Private Fields from a class - Android java.lang.reflect

Android examples for java.lang.reflect:Field Private

Description

get Private Fields from a class

Demo Code


//package com.java2s;
import android.support.annotation.NonNull;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Field> getPrivateFields(@NonNull Class clazz) {
        List<Field> fields = new ArrayList<>();
        for (Field field : clazz.getDeclaredFields()) {
            if (Modifier.isPrivate(field.getModifiers())) {
                fields.add(field);//from   w w  w  .j  av a  2 s . c  o m
            }
        }
        return fields;
    }
}

Related Tutorials