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