• ProxyImpl 类


    package com.test.mvp.mvpdemo.mvp.v7.proxy;

    import com.test.mvp.mvpdemo.mvp.v7.basemvp.BasePresenter;
    import com.test.mvp.mvpdemo.mvp.v7.basemvp.IBaseView;
    import com.test.mvp.mvpdemo.mvp.v7.inject.InjectPresenter;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;

    public class ProxyImpl implements IProxy {

    private IBaseView mView;
    private List<BasePresenter> mInjectPresenters;

    public ProxyImpl(IBaseView view) {
    this.mView = view;
    mInjectPresenters = new ArrayList<>();
    }

    @SuppressWarnings({"unchecked", "TryWithIdenticalCatches"})
    @Override
    public void bindPresenter() {
    //获得已经申明的变量,包括私有的
    Field[] fields = mView.getClass().getDeclaredFields();
    for (Field field : fields) {
    //获取变量上面的注解类型
    InjectPresenter injectPresenter = field.getAnnotation(InjectPresenter.class);
    if (injectPresenter != null) {
    try {
    Class<? extends BasePresenter> type = (Class<? extends BasePresenter>) field.getType();
    BasePresenter mInjectPresenter = type.newInstance();
    mInjectPresenter.attach(mView);
    field.setAccessible(true);
    field.set(mView, mInjectPresenter);
    mInjectPresenters.add(mInjectPresenter);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InstantiationException e) {
    e.printStackTrace();
    } catch (ClassCastException e) {
    e.printStackTrace();
    throw new RuntimeException("SubClass must extends Class:BasePresenter");
    }
    }
    }
    }

    @Override
    public void unbindPresenter() {
    /**
    * 解绑,避免内存泄漏
    */
    for (BasePresenter presenter : mInjectPresenters) {
    presenter.detach();
    }
    mInjectPresenters.clear();
    mInjectPresenters = null;
    }
    }
    --------------------- 

  • 相关阅读:
    AMQP 介绍
    JMS 介绍
    SpringCloud 入门实战(12)--Zipkin(2)--安装使用
    SpringCloud 入门实战(11)--Zipkin(1)--简介
    Kafka 入门实战(3)--SpringBoot 整合 Kafka
    跨域访问方法介绍(9)--nginx 反向代理
    跨域访问方法介绍(8)--使用 WebSocket
    个人网站建好啦!
    Spring Boot配置Slf4j + log4j2
    SpringBoot 解决跨域,3种解决方案
  • 原文地址:https://www.cnblogs.com/ly570/p/11299108.html
Copyright © 2020-2023  润新知