AccessibleObject: setAccessible(boolean flag)AccessibleObject: setAccessible(boo

import java.lang.reflect.Field;

/**
 * Created by IntelliJ IDEA.
 * User: zhangchuanlong
 * Date: 12-4-7
 * Time: 下午9:49
 * To change this template use File | Settings | File Templates.
 */
public class Feflection {
    public static void initIntFields(final Object obj) {
        try {
            Field[] fields = obj.getClass()
                    .getDeclaredFields();
            for (int idx = 0; idx < fields.length; idx++) {
                if (fields[idx].getType() == int.class) {
                    fields[idx].setAccessible(true);
                    fields[idx].setInt(obj, 0);
                }
            }
        } catch (final IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
    }


    public static  void main(String[] args)
    {
        SomeNumbers value = new SomeNumbers();
        System.out.println("Before: " + value);
        initIntFields(value);
        System.out.println("After: " + value);

    }
}

class SomeNumbers
{
    /** A demo double. */
    public double a = 21.25d;

    /** A demo float. */
    public float b = 54.5f;

    /** A Demo int */
    public int c = 5665;

    /** Another demo int. */
    public int d = 2043;

    /** Another demo int. */
    protected int e = 3121;

    /** Another demo int. */
    private int f = 1019;

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return new String("[a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", e=" + e
                + ", f=" + f + "]");
    }

}

 

你可能感兴趣的:(boolean)