反射-注解

MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    Class<Preson> cls;
    Preson preson;

    @BindView(R.id.main_b1) //自定义的BindView
    Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InjectViewParser.bind(this);
        findViews();
        initData();
    }

    private void initData() {

        preson = new Preson();
        cls = (Class<Preson>)preson.getClass();
    }

    private void findViews() {
         //mButton1 = findViewById(R.id.main_b1);
         mButton2 = findViewById(R.id.main_b2);
         mButton3 = findViewById(R.id.main_b3);
         mButton4 = findViewById(R.id.main_b4);

         mButton.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mButton4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.main_b1:

                Method[] method = cls.getDeclaredMethods();
                System.out.println("getDeclaredMethods():获取所有的权限修饰符修饰的Method");
                for (Method m : method) {
                    System.out.println("Method Name = " + m.getName());
                }
                System.out.println();
                /*try {
                    Preson preson = (Preson)Class.forName("fanruiqi.bwie.com.reflect.Preson")
                            .newInstance();

                    System.out.println(preson.toString());
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }*/
                break;

            case R.id.main_b2:
                Field[] field = cls.getDeclaredFields();
                System.out.println("getFields():获取所有权限修饰符修饰的字段");
                for (Field f : field) {
                    System.out.println("Field Name = " + f.getName());
                }
                System.out.println();
                break;

            case R.id.main_b3:
                try {
                    Preson preson = (Preson)Class.forName("fanruiqi.bwie.com.reflect.Preson")
                            .newInstance();

                    System.out.println(preson.getName());
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.main_b4:

                try {

                    Field name = cls.getDeclaredField("name");
                    name.setAccessible(true);
                    name.set(preson,"mi");
                    System.out.println(name);

                    // 获取方法名为showName,参数为String类型的方法
                    Method method1 = cls.getDeclaredMethod("setName", String.class);
                    // 若调用私有方法,必须抑制java对权限的检查
                    method1.setAccessible(true);
                    // 使用invoke调用方法,并且获取方法的返回值,需要传入一个方法所在类的对象,new Object[]
                    // {"Kai"}是需要传入的参数,与上面的String.class相对应
                    String string = (String) method1.invoke(preson,
                            new Object[] {"nii"});
                    System.out.println(string);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }

                /*try {
                    Preson preson = (Preson)Class.forName("fanruiqi.bwie.com.reflect.Preson")
                            .newInstance();

                    System.out.println(preson.getName());
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }*/
                break;
        }
    }
}
BindView

@Retention(RUNTIME)
@Target(ElementType.FIELD)
public @interface BindView {
   @IdRes int value();
}
InjectViewParser.java
public class InjectViewParser {
    public static void bind(Object object) {
        try {
            parse(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parse(Object object) throws Exception {
        final Class<?> clazz = object.getClass();
        View view = null;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(BindView.class)) {
                BindView injectView = field.getAnnotation(BindView.class);
                int id = injectView.value();
                if (id < 0) {
                    throw new Exception("error");
                } else {
                    field.setAccessible(true);
                    if (object instanceof View) {
                        view = ((View) object).findViewById(id);
                    } else if (object instanceof Activity) {
                        view = ((Activity) object).findViewById(id);
                    }
                    field.set(object, view);
                }
            }
        }
    }
}
Preson.java
public class Preson {

    private int age=20;
    private String name="ni";

    public Preson(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Preson() {

    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String setName(String name) {
        this.name = name;
        return name;
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值