Java Reflection mechanism

Source: Internet
Author: User

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.

Java's reflection mechanism (Reflection), a very prominent dynamic correlation mechanism, is used in Java to refer to the classes that we can load, detect, and use completely unknown during compilation at runtime. In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods.

Let's start with an example to explain the use of reflection.

Tvos.tv.TManager.getInstance (). Getfactorymanager (). Gettvosversion ()

Tmanager and Mfmanager are classes in the package tvos.tv.

GetMethod (......) The first parameter of this method is the method name of the method that needs to be obtained, and the parameter that follows is the parameter of the obtained method, and the number of GetMethod parameters is related to the method to be obtained.

Mtmanager.getmethod ("getinstance", context.class); Gets the method Getintance, the second parameter is the parameter of the method Getintance, That is, the Mtmanager object itself (the use of the method in the class is Getintance (this))

Mtmanager.getmethod ("Getfactorymanager"); Gets method Getfactorymanager, no second argument (method is defined as Getfactorymanager (void), parameter is void)

Invoke (......) This method is similar to GetMethod, where the first parameter is the name of the object, and the subsequent arguments are related to the method that needs to be called

Object Obj_tmanager = Getinstance.invoke (Mtmanager,mcontext), getinstance by Mtmanager call method, Obj_tmanager, is the same as normal invocation (normal call mtmnanager.getinstance (this))

Object Obj_fmanager = Getfactorymanager.invoke (Obj_tmanager); Obj_tmanager call Getfactorymanager Get Objects Obj_FManager (The normal call is Mtmnanager.getinstance (this). Getfactorymanager ())

String sver = Gettvsdkversion.invoke (Obj_fmanager). toString (); Through Obj_ Fmanager calls Gettvsdkversion (the Gettvsdkversion method has no arguments) to get the object.

In fact, reflection calls just a few steps:

1, through the class.forname to get to the class, parameter is the full name of the class

2, through the GetMethod to obtain the method name in the class

3. Invoke method name via Invoke

public int gettvosversion () {
int ver = 0;
try {
LOG.D (TAG, "Search New Interface");
class<?> Mtmanager = Class.forName ("Tvos.tv.TManager");
LOG.D (TAG, "Mtmanager" +mtmanager);
class<?> Mfmanager = Class.forName ("Tvos.tv.TFactoryManager");
LOG.D (TAG, "Mtmanager" +mfmanager);
Method getinstance =mtmanager.getmethod ("getinstance", Context.class);
LOG.D (TAG, "getinstance" +getinstance);
Method Getfactorymanager = Mtmanager.getmethod ("Getfactorymanager");
LOG.D (TAG, "Getfactorymanager" +getfactorymanager);
Method gettvosversion = Mfmanager.getmethod ("gettvosversion");
LOG.D (TAG, "gettvosversion" +gettvosversion);
Object Obj_tmanager = Getinstance.invoke (Mtmanager,mcontext);
LOG.D (TAG, "Obj_tmanager" +obj_tmanager);
Object Obj_fmanager = Getfactorymanager.invoke (Obj_tmanager);
LOG.D (TAG, "Obj_tmanager" +obj_fmanager);
String sver = Gettvosversion.invoke (Obj_fmanager). toString ();
LOG.D (TAG, "string Tvosversion" +sver);
ver = (Integer) gettvosversion.invoke (Obj_fmanager);
LOG.D (TAG, "gettvosversion" +gettvosversion);
}
catch (Nosuchmethodexception E1) {
TODO auto-generated Catch block
LOG.I (TAG, "E1");
E1.printstacktrace ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
LOG.I (TAG, "E2");
E.printstacktrace ();
} catch (IllegalArgumentException e) {
TODO auto-generated Catch block
LOG.I (TAG, "E3");
E.printstacktrace ();
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
LOG.I (TAG, "E4");
E.printstacktrace ();
} catch (InvocationTargetException e) {
TODO auto-generated Catch block
LOG.I (TAG, "E5");
E.printstacktrace ();
}
LOG.I (TAG, "return tvosversion" +ver);
Return ver;
}

Java Reflection mechanism

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.