好玩的注解和反射

来源:互联网 发布:阿里云服务器代理ip 编辑:程序博客网 时间:2024/06/10 06:00

原文地址:http://blog.csdn.net/BingHongChaZuoAn/article/details/52045292
不知道为啥,老觉得注解和反射挺好玩的,以前会写,但是不是玩,没深度,没意思,这次好好没玩一哈。
看下面代码:

注解MM,这是一个方法注解

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MM {    String name() default "cooci";    int age() default 24;    int order() default 0;}

注解Boy,这是一个变量注解

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface Boy {   String name () default "";}

注解主要类

public class TestMM {    @Boy(name = "西门庆")    private String boy1;    @Boy(name = "武大郎")    private String boy2;    @MM(name = "潘金莲",order = 1)    private void moMM(String boyName, String mmName) {        Log.v("invoke", "--------" + boyName + "摸了" + mmName + "一下---------");    }    @MM(name = "潘金莲",order = 2)    private void mmSay(String boyName, String mmName) {        if (boyName.equals("西门庆")) {            Log.v("invoke", "----"+mmName+"说----流氓!-----");        }else if(boyName.equals("武大郎")){            Log.v("invoke", "----"+mmName+"说----滚!-----");        }    }    public String getBoy1() {        return boy1;    }    public String getBoy2() {        return boy2;    }}

注解辅助类

 class MethodHold {        private Method method;        private MM mm;        public MethodHold() {        }        public MethodHold(Method method, MM mm) {            this.method = method;            this.mm = mm;        }        public Method getMethod() {            return method;        }        public void setMethod(Method method) {            this.method = method;        }        public MM getMm() {            return mm;        }        public void setMm(MM mm) {            this.mm = mm;        }    }

测试类

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {//                testStudentInvoke();//                testAppleInvoke();                testMM();            }        });    }    private void testMM() {        Class<?> cls = TestMM.class;        try {            TestMM testMM = (TestMM) cls.newInstance();            Field fields[] = cls.getDeclaredFields();            for (Field field : fields) {                if (field.isAnnotationPresent(Boy.class)) {                    Boy boy = field.getAnnotation(Boy.class);                    field.setAccessible(true);                    field.set(testMM, boy.name());                }            }            Log.v("invoke", "--------boy1-------------->" + testMM.getBoy1());            Log.v("invoke", "--------boy2-------------->" + testMM.getBoy2());            Method methods[] = cls.getDeclaredMethods();            ArrayList<MethodHold> arrayList = new ArrayList<>();            for (Method method : methods) {                if (method.isAnnotationPresent(MM.class)) {                    MM mm = method.getAnnotation(MM.class);                    method.setAccessible(true);                    arrayList.add(new MethodHold(method, mm));                }            }            arrayList = sortMethodByOrder(arrayList);            for (MethodHold methodHold : arrayList) {                methodHold.getMethod().invoke(testMM, testMM.getBoy1(), methodHold.getMm().name());            }            for (MethodHold methodHold : arrayList) {                methodHold.getMethod().invoke(testMM, testMM.getBoy2(), methodHold.getMm().name());            }        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InstantiationException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }    }    /**     *标准冒泡排序     */    private ArrayList<MethodHold> sortMethodByOrder(ArrayList<MethodHold> arrayList) {        for (int i = 0; i < arrayList.size() - 1; i++) {            boolean flag = true;            for (int j = 0; j < arrayList.size() - i - 1; j++) {                MethodHold preMethodHold = arrayList.get(j);                MethodHold aftermethodHold = arrayList.get(j + 1);                if (preMethodHold.getMm().order() > aftermethodHold.getMm().order()) {                    arrayList.remove(j);                    arrayList.add(j + 1, preMethodHold);                    flag = false;                }            }            if (flag) {                break;            }        }        return arrayList;    }    }

就此搞定,看打印的结果:

invoke: --------boy1-------------->西门庆invoke: --------boy2-------------->武大郎invoke: --------西门庆摸了潘金莲一下---------invoke: ----潘金莲说----流氓!-----invoke: --------武大郎摸了潘金莲一下---------invoke: ----潘金莲说----滚!-----

好了 ,好玩吧 哈哈,你也试试

0 0
原创粉丝点击