好玩的注解和反射
2016-07-27 14:36
316 查看
原文地址:http://blog.csdn.net/BingHongChaZuoAn/article/details/52045292
不知道为啥,老觉得注解和反射挺好玩的,以前会写,但是不是玩,没深度,没意思,这次好好没玩一哈。
看下面代码:
注解MM,这是一个方法注解
注解Boy,这是一个变量注解
注解主要类
注解辅助类
测试类
就此搞定,看打印的结果:
好了 ,好玩吧 哈哈,你也试试
不知道为啥,老觉得注解和反射挺好玩的,以前会写,但是不是玩,没深度,没意思,这次好好没玩一哈。
看下面代码:
注解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: ----潘金莲说----滚!-----
好了 ,好玩吧 哈哈,你也试试
相关文章推荐
- js 加密
- Design Support library简介
- css position属性
- Android_NewBird之Asynctask的基本使用(1)
- android studio shape使用
- 将HTML5 Canvas的内容保存为图片借助toDataURL实现
- HTTP_CLIENT_IP、HTTP_X_FORWARDED_FOR、REMOTE_ADDR
- mysql字段长度是什么意思
- Ubuntu 14.04 下通过 XKB 修改键盘映射, 实现自定义按键
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
- mac 常用终端命令与使用
- Bag-of-words model
- 小议安全威胁情报
- linux中编译安装log4cpp
- PyGobject(二十三)布局容器之AspectFrame
- Spring Framework Artifacts
- 正则表达式
- Windows 安装ELK
- hdu-1533 Going Home(最小费用最大流)
- PCB层介绍