The reflection of the Java Foundation

Source: Internet
Author: User
Tags reflection

How do I create a class object?    There are two ways: 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 Newi Nstace; can be changed from class to object; instanceof 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. Get constructed method:Class.getconstructors (); Gets the public construction method; class.getdeclaredconstructors (); Gets all the constructor methods; Class.getconstructor (null); get public, Non-parametric construction methods; These methods return a constructor con, the class object of the constructor, and the object can be created by means of con.newinstance ("parameter"); Get member Variable:Class.getfields (); Gets all the public fields; Class.getdeclaredfields (); Gets all fields; Class.getfield ("name"); Gets a public-specific field (name); class. Getdeclaredfield ("Phonenum"); Gets a private field phonenum, returns field, sets our member variable in set mode, F.set (obj, "Andy Lau"); Our this obj is one of our objects produced by class; Get member Method:Class.getmethods (); Get all public methods; Class.getdeclaredmethods (); Get all methods; Class.getmethod ("Method name", String.class); Get a single specific method The back String.class refers to our method parameter; And what we're going to do is, like a field, produce an object first; M.invoke (obj,20); We used the method's Execute function invoke (), Obj is an object, 20 is a parameter, and even a direct reflection of our main () method, Class.getmethod ("main", Stirng[].class), through reflection, we can implement in the original base mountain, read the properties to achieve reflection, To create a new class, and to execute a method; a generic check can be avoided by reflection: we can even invoke the class file of a container and then call his method to escape the generic check, and we can also implement the dynamic proxy mode through reflection:
Interfacesubject{ Public voiddosomething ();}classRealsubjectImplementssubject{ Public voiddosomething () {System.out.println ("Call DoSomething ()"); }}classProxyhandlerImplementsinvocationhandler{PrivateObject tar;  Publicobject bind (Object tar) { This. tar =tar; returnproxy.newproxyinstance (Tar.getclass (). getClassLoader (), Tar.getclass (). Getinterf Aces (), This); }      PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable{Object Result=NULL; Result=Method.invoke (Tar,args); returnresult; }} Public classtest{ Public Static voidMain (string[] args[]) {Proxyhandler proxy=NewProxyhandler (); Subject Sub= (Subject) proxy.bind (Newrealsubject ());    Sub.dosomething (); }}

One uses a proxyhandler, implements the self-invocationhandler interface, by passing in a real object, obtains a proxy object, needs the parameter to have: 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 in the subsequent method call, remember to turn the object down to the real object when invoking the bind of the proxy object;

Get constructors, member variables, and member methods based on reflection for more detailed code please refer to the blog: 71799078

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.