Reflection Learning Summary--preparing for understanding the SPRINGMVC bottom

Source: Internet
Author: User

What is reflection?

Popular Understanding-X-ray.

Java: A class is like an X-ray in front of reflection, clearly and plainly.

Application: In our IDE, we are able to "." You know that all the methods in a class are implemented by reflection.

The class full path dynamically generated classes configured in XML. Framework

The program creates objects dynamically during the run.

--As long as you know the name of the class, you can use his bytecode object to create an object of that class.

What is a class object:

Helloworld.java

by Javac Helloworld.java----->helloworld.class (bytecode file)---> java HelloWorld JVM loads helloworld.class into memory and becomes class pair Like

  

About the loading of class:

public static void Main (string[] args) {person        p = new Person ();        Class Pclassclass = P.getclass ();        System.out.println (pclassclass);        person person = new person ("AA", "+", "USA");        Class Personclass  = Person.getclass ();        System.out.println (personclass);        System.out.println (Pclassclass = = Personclass); True bytecode files are loaded into memory only once during program run    }

  

Three ways to get class: P.getclass (), Person.class, Class.forName ("Class name full path")

 Public Static voidMain (string[] args)throwsclassnotfoundexception {person P=NewPerson (); Class P1=P.getclass (); Person Person=NewPerson ("AA", "+", "USA"); Class P2=Person.getclass (); System.out.println (P1==p2); Class<Person> P3 = person.class; SYSTEM.OUT.PRINTLN (P3==p1); Class<?> P4 = Class.forName ("Com.java.demo.clazz.Person"); System.out.println (P4==p3); }

Either way, there's only one byte code

Use class

First look at the relationship of class and bytecode objects

  

About Construction methods:

Mode one: Using the Newinstance () method

Class.newinstance (), the newinstance () layer needs to call the parameterless constructor, so we need to have an argument-free constructor in our reflected class.

     Public Static void throws illegalaccessexception, Instantiationexception, classnotfoundexception {        // via byte-code object, Create an object        class<?> aclass = Class.forName ("Com.java.demo.clazz.Person");         = (person) aclass.newinstance ();    }

Way two: With constructor, this time, the method of constructing the null parameter is not necessary.

     Public Static void throws classnotfoundexception, Nosuchmethodexception, Illegalaccessexception, InvocationTargetException, instantiationexception {        Class<?> aclass = Class.forName ("Com.java.demo.clazz.Person");        Constructor<?> Constructor = Aclass.getconstructor (String.  Class, String. class, String. class );         = (person) constructor.newinstance ("AA", "+", "USA");    

Enhanced version of Mode two: When using the Getdeclaredconstructor () method, Setaccessible (true), even if his constructor is private, he can create the object (he can get everything, including public private).

     Public Static voidMain (string[] args)throwsclassnotfoundexception, Nosuchmethodexception, Illegalaccessexception, InvocationTargetException, instantiationexception {Class<?> AClass = Class.forName ("Com.java.demo.clazz.Person"); Constructor<?> constructor = Aclass.getdeclaredconstructor (String.class, String.class, String.class); Constructor.setaccessible (true);//violent visit to Illegalaccessexceptionperson instance = (person) constructor.newinstance ("AA", "+", "USA")); }

Get field (member variable)

As long as it's declared, you can get all the declared variables.

 Public Static void Main (string[] args) {        Class<Person> personclass = person.  Class;         // This can only be obtained in the public         for (Field f:fields) {            System.out.println (f);        }         // This can be obtained to all the         for (Field f:declaredfields) {            System.out.println (f);        }    }

Get Method (Methods in Class)

As long as it's declared, you can get all the way to the declaration.

 Public Static void Main (string[] args) {        Class<Person> personclass = person.  Class;         = personclass.getdeclaredmethods ();          for (method Method:declaredmethods) {            System.out.println (method);        }    }

Invocation of Method methods

     Public Static voidMain (string[] args)throwsillegalaccessexception, Instantiationexception, InvocationTargetException, nosuchmethodexception {Class<Person> Personclass = person.class; Person Person=personclass.newinstance (); Method Spark= Personclass.getdeclaredmethod ("Spark", String.class); //execution method, invoke, two parameters//the first parameter method belongs to that object, and the second object methods parameterSpark.setaccessible (true); Spark.invoke (person,"HeLLO Gril"); }

Reflection Learning Summary--preparing for understanding the SPRINGMVC bottom

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.