Skip to content

优化反射创建

Drowning Coder edited this page Nov 14, 2018 · 2 revisions

本项目的创建原理基于反射构造器创建对应的ViewHolder,项目本身会对反射的构造器进行缓存,利用全局的LruCache进行缓存

缓存构造器

//SlotsMap.java
public class SlotsMap {
    private static final int CONSTRUCTOR_CACHE = 10;
    public static volatile SlotsMap instance;
    private IComponentRule componentRule;
    private static final LruCache<Class<?>, Constructor> constructorMap =
            new LruCache<Class<?>,Constructor>(CONSTRUCTOR_CACHE);
    //private IPresenterRule presenterRule;

    public static SlotsMap getInstance() {
        if (instance == null) {
            synchronized (SlotsMap.class) {
                if (instance == null) {
                    instance = new SlotsMap();
                }
            }
        }
        return instance;
    }

    private SlotsMap() {
        initRule();
    }

    private void initRule() {
       ...
    }

    public Constructor obtainConstructor(Class<?> clazz) {
        return  constructorMap.get(clazz);
    }

    public void cacheConstructor(Class<?> clazz, Constructor constructor) {
        constructorMap.put(clazz, constructor);
    }

    public IComponentRule obtainRule() {
        return componentRule;
    }
}


//DFComponentFactory.java
@Override
    public Component reflectCreate(Class<?> clazz) {
        Component component = null;
        try {
            //获取缓存的构造器
            Constructor c = SlotsMap.getInstance().obtainConstructor(clazz);
            if (c == null) {
                c = clazz.getConstructor(Context.class, View.class);
                SlotsMap.getInstance().cacheConstructor(clazz, c);
            }
            component = (Component) c.newInstance(context, rootView);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return component;
    }

自定义创建

如果对于反射的性能你有担心,项目本身提供了自定义创建的过程

@ComponentType(
            value = PersonId.INNER,
            view = TextView.class,
            //注解不使用反射
            autoCreate = false
    )
    public static class InnerVH extends RecyclerView.ViewHolder implements IComponentBind<PersonModel> {
        private TextView tv;
        public InnerVH(View itemView) {
            super(itemView);
            ...
        }

        @Override
        public void onBind(int pos, PersonModel item) {
            tv.setText("这是内部类的注解");
        }

        @Override
        public void onUnBind() {

        }
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        SlotContext slotContext =
                new ToolKitBuilder<PersonModel>(this)
                        .setData(mData)
                        .setComponentFactory(new CustomFactory() {
                            @Override
                            protected IComponentBind createViewHolder(Context context, ViewGroup
                                    parent, int type) {
                                if (type == PersonId.INNER) {
                                    LogUtil.Log("自定义创建");
                                    return new InnerVH(new TextView(PersonModelActivity.this));
                                }
                                return super.createViewHolder(context, parent, type);
                            }
                        }).build();
        slotContext.bind(mRcy);
    }
    1. 注解ComponentType中autoCreate=false
    1. 调用setComponentFactory自定义创建过程

具体可以看PersonModelActivity

Clone this wiki locally