The reflection of the Java Foundation

Source: Internet
Author: User
How do I create a class object? There are two ways of doing this:
Class.forName ("class name");
Class a = A.class;
The difference between the two is that forname () will help you initialize the static variables, while Class A = A.class to initialize the static variable yourself, the reflection, the dynamic proxy, and through reflection, you can implement the method of acquiring class, that is, the object's method; using a Newinstace ; can be changed from class to object; Nstanceof can determine whether a class belongs to another class and is used for the judgment of downward transformation;
 
A class object allows you to get a constructor, a member variable, a member method, and access to a member.

Gets the constructor method:
Class.getconstructors (), gets the public constructor method,
Class.getdeclaredconstructors (), gets all the constructor methods ;
Class.getconstructor (null); Gets the public, parameterless construction method;
These methods return a constructor con; A class object of the constructor, which can be passed con.newinstance ("parameter") To create the object,

get member Variable:
Class.getfields (); Get all public fields;
Class.getdeclaredfields () ; Gets all fields;
Class.getfield ("name"); Gets a public-specific field (name);
Class.getdeclaredfield ("Phonenum"); Gets a private field phonenum; The
returns field, sets our member variables in set mode, F.set (obj, "Andy Lau"), and of these, our obj is an object produced by our class;

Gets the member method:
Class.getmethods (); Gets all public methods;
Class.getdeclaredmethods (); Gets all the methods;
Class.getmethod ("Method name", String.class ); Obtain a single specific method; The String.class refers to our method parameter; The
returns a way object; So what we're going to do, is a field, first of all to produce an object; M.invoke (obj,20) We used the method's Execute function invoke (), obj is the object, 20 is the argument, the
can even reflect our main () method directly, Class.getmethod ("main", Stirng[].class), through reflection, We can implement the original base mountain, read the properties to achieve reflection, to create new classes, and execute methods, with reflection can avoid the generic check: we can even call a container's class file, and then call his method, to avoid the problem of generic check;

In addition, we can also implement dynamic proxy mode through reflection:

Interface subject{public    void DoSomething ();} Class Realsubject implements subject{public    void DoSomething () {        System.out.println ("Call DoSomething ()");}        } Class Proxyhandler implements invocationhandler{    private Object tar;    public object bind (object tar) {       This.tar = tar;       Return Proxy.newproxyinstance (Tar.getclass (). getClassLoader (),                                      Tar.getclass (). Getinterfaces (), this                                      );     } Public     object Invoke (Object proxy, Method method, object[] args) throws throwable{         object result = null;        result = Method.invoke (Tar,args);        return result;}    } public class test{public    static void Main (string[] args[]) {        Proxyhandler proxy = new Proxyhandler ();        Subject sub = (Subject) proxy.bind (New Realsubject ());        Sub.dosomething ();    }}

The use of a proxyhandler, the implementation of the Invocationhandler interface, by passing in a real object, to obtain a proxy object, the required parameters are: Tar.getclass (). getClassLoader () The real object's ClassLoader, Tar.getclass (). Getinterfaces () The interface of the real object, and This,invocationhandler itself, through the mechanism of reflection, to obtain a proxy object, while in the proxy object's method execution function , to write our real object in the way of execution. In order to invoke the method of our real object first in the subsequent method call, remember to turn the object down into the real object when invoking the bind of the proxy object.

Related Article

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.