Framework Basics (annotations + reflections)

Source: Internet
Author: User

Create a layout annotation class Mycontentview.java, with the following code:

@Retention (Retentionpolicy.runtime)

@Target ({elementtype.type})

Public @interface Mycontentview {

int value ();

}


Create a control callout class Myviewinject. Java with the following code:

@Retention (Retentionpolicy.runtime)

@Target (Elementtype.field)

Public @interface Myviewinject {

int value ();

}


Create a reflection class that handles annotations Myviewutils. Java with the following code:

public class Myviewutils {

//Construction Method injection Event

public static void inject (activity activity) {

injectobject (activity, new viewfinder);//methods of actually injecting objects

}

//method of Event injection object

private static void Injectobject (Object obj, viewfinder viewfinder) {

//Inject the annotation information into the actual method, implement the layout initialization, get the entity class

Class clz = Obj.getclass ();

//Get CLZ annotation information (custom annotation information)

Mycontentview Mycontentview = (mycontentview) clz.getannotation (Mycontentview.class);

//Get the actual annotations that exist

if (mycontentview! = null) {

/** layout annotations Get the XML layout **/

//Get the value of the annotation

int value = Mycontentview.value ();

//get content injected into the established method

try {

//Set the Setcontentview parameter (Getdeclaremethod cannot get the public method?)

//Setcontentview method is the way to get the XML layout

method = Clz.getmethod ("Setcontentview", new class[] {integer.type});

method.setaccessible (TRUE);//This setting means ignoring permissions and getting directly

method.invoke (obj, value);//The Setcontentview method that gets the parameter is added to the specified class

} catch (Nosuchmethodexception e) {

e.printstacktrace ();

} catch (InvocationTargetException e) {

e.printstacktrace ();

} catch (Illegalaccessexception e) {

e.printstacktrace ();

}

/** control annotations Gets the XML layout control **/

//Get information about the control and inject the content, get all the members of the field[] on the Myviewinject,

To have declared, you can access the private properties

//visible, the creation of the control must be defined as a member variable before annotations can be used

field[] fields = Clz.getdeclaredfields ();

//has member variables, and the number is not 0

if (Fields! = NULL && fields.length! = 0) {

for (int i = 0; i < fields.length; i++) {

Field field = Fields[i];

//Get information about annotations (on member variables)

Myviewinject inject = field.getannotation (Myviewinject.class);

if (inject! = null) {

//Get the actual content

int fieldvalue = Inject.value ();

//custom method get viewfinder, call Findviewbyid method, help current obj find control

View view = Viewfinder.findviewbyid (Fieldvalue);

if (view! = null) {

try {

field.setaccessible (TRUE);//Ignore access rights

field.set (obj, view);//Assign him to the field member variable

} catch (Illegalaccessexception e) {

e.printstacktrace ();

}

}

}

}

}

}

}


//Custom, to find the control

public static class viewfinder {

private activity activity;

private View view;

Public viewfinder (activity activity) {

this.activity = activity;

}

Public viewfinder (view view) {

this.view = view;

}

//ID fill in and return the actual view

public View findviewbyid (int id) {

View view = Activity.findviewbyid (ID);

return view;

}

}

}


Finally, test, test the class Testviewutilsactivity.java, the code is as follows:

@MyContentView (R.layout.activity_test_view_utils)

public class Testviewutilsactivity extends Activity {

@MyViewInject (R.id.textview_text)

Private TextView TextView;

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Myviewutils.inject (this);

Textview.settext ("test data, Annotations + reflection mode");

}

}


The layout file contains only one text box, which is no longer on the code.




Framework Basics (annotations + reflections)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.