lundi 11 mai 2015

how to get anonymous class fields by using reflection

here is my example classes, i want get y and z fields by using reflection

package test;

import java.lang.reflect.Field;

public class Reflection {

    static ClassLoader classLoader = null;

    public static void main(String[] args) {
        classLoader = Reflection.class.getClassLoader();
        Reflection r = new Reflection();
        r.getAnnClas();
    }

    private void getAnnClas() {
        Class<?> forName = null;
        try {
            forName = classLoader.loadClass("test.Wrirter");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Field[] declaredFields = forName.getDeclaredFields();
        for (Field field : declaredFields) {
            //here i canot find annoumouse calss filed Z
            System.out.println(field.getName());
        }
    }
}

package test;

import java.io.File;
import java.io.FileReader;

public class Wrirter {

    public Cb c;

    public Wrirter() {

    }

    public void ann() {
        c = new Cb(10, 20) {
            int y = 10;
            int z = 20;

            @Override
            public void doSom() {
                super.doSom();
                int cbf = getIntC() + getIntB();
            }
        };

    }
}

class Cb {

    public int c;
    public int b;

    public Cb(int c, int b) {
        this.c = c;
        this.b = b;
    }

    public void doSom() {

    }

    public int getIntC() {
        return c;
    }

    public int getIntB() {
        return b;
    }

    public int setIntC() {
        return c;
    }

    public int setIntB() {
        return b;
    }
}





Aucun commentaire:

Enregistrer un commentaire