Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a View Annotation binding Library by using reflection and dynamic proxy in Java

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how Java uses reflection and dynamic proxy to implement a View annotation binding library". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Use reflection combined with dynamic proxy to implement a View annotation binding library, which supports View and event binding, simple code, easy to use and strong expansibility.

Supported Featur

@ ContentView binds layout instead of setContentView ()

@ BindView binds View instead of findViewById ()

@ OnClick bind Click event instead of setOnClickListener ()

@ OnLongClick binding length by event instead of setOnLongClickListener ()

The code annotation class @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) public @ interface ContentView {int value ();} @ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) public @ interface BindView {int value ();} @ Target (ElementType.ANNOTATION_TYPE) @ Retention (RetentionPolicy.RUNTIME) public @ interface OnEvent {/ / subscription method String setCommonListener (); / / event source object Class commonListener () } @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ OnEvent (setCommonListener = "setOnClickListener", commonListener = View.OnClickListener.class) public @ interface OnClick {int value ();} @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ OnEvent (setCommonListener = "setOnLongClickListener", commonListener = View.OnLongClickListener.class) public @ interface OnLongClick {int value ();} implementation class public class MsInjector {public static void inject (Object object) {injectContentView (object) InjectView (object); injectEvent (object);} private static void injectContentView (Object object) {Class clazz = object.getClass (); / / get the ContentView annotation ContentView contentView = clazz.getAnnotation (ContentView.class); if (contentView = = null) {return;} / / get the value of the annotation, that is, layoutResID int layoutResID = contentView.value () Try {/ / reflects the setContentView method and calls Method method = clazz.getMethod ("setContentView", int.class); method.invoke (object, layoutResID);} catch (Exception e) {e.printStackTrace ();}} private static void injectView (Object object) {Class clazz = object.getClass () / / get all the fields and traverse Field [] fields = clazz.getDeclaredFields (); for (Field field: fields) {field.setAccessible (true); / / get the BindView comments on the field BindView bindView = field.getAnnotation (BindView.class); if (bindView = = null) {continue } / / get viewId int viewId = bindView.value (); try {/ / call findViewById through reflection to get view instance object Method method = clazz.getMethod ("findViewById", int.class); Object view = method.invoke (object, viewId) / / assign a value to the corresponding field field.set (object, view) of the annotation;} catch (Exception e) {e.printStackTrace ();} private static void injectEvent (Object object) {Class clazz = object.getClass () / / get all the methods in the current page year and traverse Method [] declaredMethods = clazz.getDeclaredMethods (); for (Method declaredMethod: declaredMethods) {declaredMethod.setAccessible (true); / / get all comments on the method and traverse Annotation [] annotations = declaredMethod.getDeclaredAnnotations () For (Annotation annotation: annotations) {/ / gets the annotation itself Class commonListener = onEvent.commonListener (); try {/ / since it is not clear which annotation is obtained above, you need to use reflection to get viewId Method valueMethod = annotationType.getDeclaredMethod ("value") ValueMethod.setAccessible (true); int viewId = (int) valueMethod.invoke (annotation); / / obtain the corresponding view Method findViewByIdMethod = clazz.getMethod ("findViewById", int.class) through reflection findViewById; Object view = findViewByIdMethod.invoke (object, viewId) / / obtain the corresponding setCommonListener method Method viewMethod = view.getClass () .getMethod (setCommonListener, commonListener) in view through reflection. / / use dynamic proxy listening callback Object proxy = Proxy.newProxyInstance (clazz.getClassLoader (), new Class [] {commonListener}) New InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ / finally executes the annotated method return declaredMethod.invoke (object, null) }}); / / call the setCommonListener method viewMethod.invoke (view, proxy) of view;} catch (Exception e) {e.printStackTrace () } use @ ContentView (R.layout.activity_main) public class MainActivity extends AppCompatActivity {@ BindView (R.id.button1) private Button button1; @ BindView (R.id.button2) Button button2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); MsInjector.inject (this) } @ OnClick (R.id.button1) public void clickButton1 () {Toast.makeText (this, "click button1", Toast.LENGTH_SHORT). Show ();} @ OnClick (R.id.button2) public void clickButton2 () {Toast.makeText (this, "click button2", Toast.LENGTH_SHORT). Show () } @ OnLongClick (R.id.button1) public boolean longClickButton1 () {Toast.makeText (this, "long click button1", Toast.LENGTH_SHORT) .show (); return false;} @ OnLongClick (R.id.button2) public boolean longClickButton2 () {Toast.makeText (this, "long click button2", Toast.LENGTH_SHORT) .show (); return false This is the end of "how Java implements a View annotation binding library using reflection and dynamic proxies". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report