码迷,mamicode.com
首页 > 其他好文 > 详细

ProxyImpl 类

时间:2019-08-04 19:17:16      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:mvp   new   instance   oid   try   ref   获取   enter   etc   

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;
}
}
--------------------- 

ProxyImpl 类

标签:mvp   new   instance   oid   try   ref   获取   enter   etc   

原文地址:https://www.cnblogs.com/ly570/p/11299108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!