Android Injection Framework you should know everything------build your own injection framework
Source: Internet
Author: User
<span id="Label3"></p>Objective<p><p>All of Java's frames are basically reflection-based, so there's a saying, no reflection, no Frame. So the Android injection framework is also based on reflection, and the next step is simply to introduce the Android injection framework that you should KNOW.</p></p>Introduction to Annotations<p><p>Annotations (Annotation) are a relatively important part of java, but they are rarely touched by this part, so it's a simple one. Now let's simply write an annotation and explain it.<br>Right-->new->annotation through eclipse and then typing the following code.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-annotation">@Target</span>(ElementType.FIELD)<span class="hljs-annotation">@Retention</span>(RetentionPolicy.RUNTIME)<span class="hljs-keyword">public</span> @<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">TestAnnotation</span>{</span> <span class="hljs-keyword">int</span> vauls(); String test();}</code></pre></pre><p><p>You can see that the target label we defined as field is the meaning of the attribute within the class, and retention this callout is the annotation that represents the Runtime. The meaning of each note you can take a look at yourself. Then see how we use this Annotation. We arbitrarily declare an object within a class. As follows</p></p><pre><pre> @TestAnnotation (test= "hello", vauls=12) private Button button3;</pre></pre><p><p>This gives us a good statement of our Annotations. Then there is the use of annotations. It's also easy to see how to use it.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs d"> Class<?> clas = getClass(); <span class="hljs-comment">//获取属性</span> Field fields[] = clas.getDeclaredFields(); <span class="hljs-keyword">for</span> (Field field : fields) { <span class="hljs-comment">//获取注解</span> TestAnnotation testAnnotation = field.getAnnotation(TestAnnotation.<span class="hljs-keyword">class</span>); <span class="hljs-keyword">if</span><span class="hljs-literal">null</span>) { <span class="hljs-comment">//得到注解里面的值</span> String test = testAnnotation.test(); <span class="hljs-keyword">int</span> id = testAnnotation.vauls(); System.<span class="hljs-keyword">out</span>.println(test + id); } }</code></pre></pre><p><p>For this simple use, if you need to understand the annotations in depth, you can check the information of the Annotations.</p></p>Everything you need to know about the injection framework build your own injection framework<p><p>First of all, Let's talk about what we are going to do this time, inject view and inject the onclick event, first we solve the problem of injecting view.</p></p>The injection of view<p><p>Let's start by creating a new annotation and typing the following code.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.edsheng.inject;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.lang.annotation.ElementType;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.lang.annotation.Retention;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.lang.annotation.RetentionPolicy;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.lang.annotation.Target;<span class="hljs-javadoc"><span class="hljs-javadoc">/*** * Copyright (c), TNT All rights Reserved. * View annotations are annotated when declaring the view Control. *<span class="hljs-javadoctag"> @author</span> bobo *<span class="hljs-javadoctag"> @date 2015-6-9 *<span class="hljs-javadoctag"> @filename</span> Viewinject.java * */</span></span></span><span class="hljs-annotation"><span class="hljs-annotation">@Target</span></span>(elementtype.field)<span class="hljs-annotation"><span class="hljs-annotation">@Retention</span></span>(retentionpolicy.runtime)<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>@<span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">interface</span> <span class="hljs-title">viewinject</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Value ();}</code></pre><p><p>Then we are creating a new Viewinjectutile class that implements such a method Inside.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/*** * Inject control view * Simply say the flow of the injected control * 1: traverse all filed according to filed * 2: get the annotations we need * 3: get the ID from the note * 4: call through Reflection Search method * 5: finally by reflection assignment * *<span class="hljs-javadoctag"> @param</span> activity */</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Injectview</span></span>(activity Activity) {class<?> CLS = Activity.getclass (); Field field[] = Cls.getdeclaredfields ();<span class="hljs-comment"><span class="hljs-comment">//get all the filed</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(Field Field2:field) {viewinject inject = field2.getannotation (viewinject.class);<span class="hljs-comment"><span class="hljs-comment">//get Annotations</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(inject! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>id = inject.value ();<span class="hljs-comment"><span class="hljs-comment">//get ID</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{<span class="hljs-comment"><span class="hljs-comment">//findviewbyid</span></span>method = Cls.getmethod (<span class="hljs-string"><span class="hljs-string">"findviewbyid"</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>. class); Object Resview = Method.invoke (activity, id);<span class="hljs-comment"><span class="hljs-comment">//get Control</span></span>Field2.setaccessible (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>); Field2.set (activity, resview);<span class="hljs-comment"><span class="hljs-comment">//assign value to view</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(Exception E) {e.printstacktrace (); } } } }</code></pre><p><p>The comments are written very clearly I will not explain, so it is very simple to realize the injection of view, the use is very simple.</p></p>Injection of events<p><p>Let's create a new annotation to complete our event injection, typing the following code.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc">/*** * * Copyright (c) 2015, TNT All Rights Reserved. * 方法的注解类在需要回调OnlickLisenler的时候进行注解就行了 * *<span class="hljs-javadoctag"> @author</span> bobo *<span class="hljs-javadoctag"> @date</span> 2015-6-9 *<span class="hljs-javadoctag"> @filename</span> MethodInject.java * */</span><span class="hljs-annotation">@Target</span>(ElementType.METHOD)<span class="hljs-annotation">@Retention</span>(RetentionPolicy.RUNTIME)<span class="hljs-keyword">public</span> @<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MethodInject</span>{</span> <span class="hljs-keyword">int</span>[] value();}</code></pre></pre><p><p>Then implement such a method within the Viewinjectutile class.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Injection monitoring method All the frames are basically based on reflection, is not there a word? No Reflection no Frame. * Simply say this process * 1: inject method in our acitvity * 2: generate dynamic Agent * 3: through the East dynamic agent to callback the method we injected * *<span class="hljs-javadoctag"> @param</span> activity */</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Injectmethod</span></span>(activity Activity) {class<?> CLS = Activity.getclass (); Method methods[] = Cls.getmethods ();<span class="hljs-comment"><span class="hljs-comment">//get The public method of this class</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(Method Method:methods) {methodinject Meathdinject = method.getannotation (methodinject.class);<span class="hljs-comment"><span class="hljs-comment">//get Annotations on a method</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(meathdinject! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-comment"><span class="hljs-comment">//generate Dynamic proxies when annotations are available</span></span>Object proxy = (object) proxy.newproxyinstance (View.OnClickListener.class.getClassLoader (),<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>class<?>[] {View.OnClickListener.class},<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Dynahanlder (activity, method));<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>ids[] = Meathdinject.value ();<span class="hljs-comment"><span class="hljs-comment">//get The ID inside the note</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{Method Findviewbyid = Cls.getmethod (<span class="hljs-string"><span class="hljs-string">"findviewbyid"</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>. class);<span class="hljs-comment"><span class="hljs-comment">//get Method</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Id:ids) {Object view = Findviewbyid.invoke (activity, id);<span class="hljs-comment"><span class="hljs-comment">//get view based on method</span></span>Method Onclickmethod = View.getclass (). GetMethod (<span class="hljs-string"><span class="hljs-string">"setonclicklistener"</span></span>, View.OnClickListener.class); Onclickmethod.invoke (view, proxy);<span class="hljs-comment"><span class="hljs-comment">//call Setonclicklistener Method callback inside the dynamic class</span></span>} }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(Exception E) {e.printstacktrace (); } } } }</code></pre><p><p>It is important to note that the dynamic class generation and proxy, we put view.onclicklistener this interface through the proxy and reflection to the annotation to the place, we see how this dynahanlder implementation.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">dynahanlder</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Invocationhandler</span> {</span></span>Object target =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; Method method =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Dynahanlder</span></span>(Object target, METHOD) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>();<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. target = target;<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. method = method; }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * This function is a callback method for dynamic registration</span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Object<span class="hljs-title"><span class="hljs-title">Invoke</span></span>(Object proxy, method method, object[] Args)<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>Throwable {<span class="hljs-comment"><span class="hljs-comment">//here Call the method of dynamic injection</span></span> <span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Method.invoke (target, args); } }</code></pre><p><p>It is also very simple to keep the application of our method when we are called back through the proxy we also call our method by Reflection.<br>finally, an interface method is exposed to the Outside.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">//外部调用接口</span><span class="hljs-keyword">static</span><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">inject</span>(Activity activity) { injectView(activity); injectMethod(activity); }</code></pre></pre><p><p>finally, Let's see how we can use it.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span>{</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@ViewInject</span></span>(r.id.button)<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button button;<span class="hljs-annotation"><span class="hljs-annotation">@ViewInject</span></span>(r.id.button2)<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button button2;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.mainactivity); Viewinjectutile.inject (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Button.settext (<span class="hljs-string"><span class="hljs-string">"fuck"</span></span>); Button2.settext (<span class="hljs-string"><span class="hljs-string">"asdfasdf"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@MethodInject</span></span>({r.id.button, R.id.button2})<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {<span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(v.getid ()) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>R.id.button:<span class="hljs-comment"><span class="hljs-comment">//System.out.println ("asdfasdf");</span></span>Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"r.id.button"</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>). Show ();<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>R.id.button2:toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"r.id.button2"</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>). Show ();<span class="hljs-comment"><span class="hljs-comment">//System.out.println ("asdf");</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword">default</span></span>:<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>; } }</code></pre><p><p>When the click button will call back our method, injected in the beginning to help us complete the ID and control binding, This is the main essence of the injection framework, the need for a better and more powerful framework needs to be done slowly. Put the source code address: Portal</p></p> <p><p>Android Injection Framework you should know everything------build your own injection framework</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service