运行时IOC框架设计

1 基础知识

  1. 注解和元注解
  2. 自定义注解
  3. 动态代理(访问控制) 生成java文件转换byte【】,虚拟机解析处理
  4. 静态代理条件:1 同一接口 2 代理持有代理对象引用
  5. 观察者模式(触发联动)
  6. 反射知识

2 android布局绑定

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ContentView{
    int value();
}


private static void injectLayout(Activity activity) {
    Class<? extends Activity> classd = activity.getClass();
    ContentView contentView = classd.getAnnotation(ContentView.class);
    int res = contentView.value();
    activity.setContentView(res);
}

3 android控件绑定

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) //注解用在属性
public  @interface ViewInject {
    int value();
}

private static void injectView(Activity activity) {
    Class<? extends Activity> classd = activity.getClass();
    Field[] anima = classd.getDeclaredFields();
    for (Field field : anima) {
        ViewInject view = field.getAnnotation(ViewInject.class);
        if (view == null) {
            continue;
        }
        View view1 = activity.findViewById(view.value());
        try {
            field.setAccessible(true);
            field.set(activity, view1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4 android事件绑定

事件三要素–注解的注解,指定三要素 1 设置监听方法 2 监听类型 3 回调的方法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public  @interface EventBase {
    String listenerSetter();
    Class<?> listenerType();
    String methednCallBack();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //注解用在属性
@EventBase(listenerSetter = "setOnClickListener",
 methednCallBack = "onClick", 
listenerType = View.OnClickListener.class)
public @interface OnClick {
    int[] value();
}

– 注入使用

private static void injectEvents(Activity activity) {
    Class<? extends Activity> classd = activity.getClass();
    Method[] methods = classd.getDeclaredMethods();
    for (Method methed : methods) {
        Annotation[] annotations = methed.getAnnotations();
        for (Annotation annotation : annotations) {
            Class<?> annotationType = annotation.annotationType();
            EventBase eventbase = annotationType.getAnnotation(EventBase.class);
            if (eventbase == null) {
                continue;
            }
            String listenerSetter = eventbase.listenerSetter();
            Class<?> listenerType = eventbase.listenerType();
            String callback = eventbase.methednCallBack();

            //代理方法映射表
            Map<String, Method> methodMap = new HashMap<>();
            methodMap.put(callback, methed);

            try {
                Method vauleMethed = annotationType.getDeclaredMethod("value");
                int[] viewId = (int[]) vauleMethed.invoke(annotation);
                for (int id : viewId) {
                    View view = activity.findViewById(id);
                    if (view == null) {
                        continue;
                    }
                    //得到set方法
                    Method method = view.getClass().getMethod(listenerSetter, listenerType);
                    //执行回调方法
                    EventInvocationHandler handler = new EventInvocationHandler(methodMap, activity);
                    Object proxy = Proxy.newProxyInstance(listenerType.getClassLoader(), new Class[]{listenerType}, handler);
                    method.invoke(view, proxy);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

– map动态代理

public class EventInvocationHandler implements InvocationHandler {
	
	//methed对应onclick
	private Map<String, Method> methedmap;
	private Object srcObj;
		
	public EventInvocationHandler(Map<String, Method> methedmap, Object activity) {
	this.methedmap = methedmap;
	this.srcObj = activity;
	}
		
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	Method mtd = methedmap.get(method.getName());
	if (mtd != null) {
	}
	return method.invoke(proxy, args);
	}
}

-

Table of Contents