Basic Java reflection knowledge

Source: Internet
Author: User
Package COM. hsj. domain; public class person {private string username; private string password; Public Person (string username, string password) {super (); this. username = username; this. password = password;} public person () {super ();} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void set Password (string password) {This. Password = password;} public static void main (string [] ARGs) {If (ARGs! = NULL) {for (string S: ARGs) {system. out. println (s) ;}} system. out. println ("Reflection main function test");} public void test1 (char [] chars) {system. out. println ("test1 char []");} public void Test2 (integer [] ints) {system. out. println ("test2... ");} public void test3 (string S1, string S2) {system. out. println ("test3") ;}@ overridepublic string tostring () {return this. username + ":" + this. password;}/* @ overridepublic int hashcode () {// Do auto-generated method stubreturn super. hashcode ();} */} package COM. hsj; import Java. lang. reflect. field; import Java. lang. reflect. method; import Org. JUnit. test; import COM. hsj. domain. person;/***** @ author administrator * Requirement Description, there is a common bean object person class * 1. Use reflection to call the null parameter constructor clazz of the person class. newinstance (); * if you want to call a constructor with parameters, use constructor c = clazz. getconstructor (string. class, String. class); * Then use person P2 = (person) C. newi Nstance ("heshengjun", "123456 "); instantiation * 2. the username field is reflected and assigned to the 'electric fan '* 3. The password is assigned to the 'dianfengshanc' * 4. The reflection of the main function running the person class * **/public class personreftest {@ testpublic void test () {try {/*** get the bytecode object through the full name of the class */class clazz = Class. forname ("com. hsj. domain. person "); // call the null parameter constructor of the class to instantiate person = (person) clazz. newinstance (); // The username field is reflected. Because it is a private field, all calls to clazz. getdeclaredfield ("username"); Method Field userna Me = clazz. getdeclaredfield ("username"); // sets the accessible permissions. For private attributes, set username. setaccessible (true); // assign username to this method of an object. set (person, "fan"); // get the value of this object string name = (string) username. get (person); system. out. println (name); // method for obtaining the bytecode setpassword = clazz. getmethod ("setpassword", String. class); // you can determine the access permissions of this method. If it is not accessible and set to true, you can access if (! Setpassword. isaccessible () {setpassword. setaccessible (true);} setpassword. invoke (person, "dianfengshan"); system. out. println (person. getPassword (); // reflection on the main method. Note that for variable parameter types, the array type is used. For arrays of basic types, int []. class, char []. method main = clazz. getmethod ("Main", string []. class); If (! Main. isaccessible () {main. setaccessible (true);}/*** when passing parameters here, pay attention to the transmission of array parameters, generally, the array is split and passed as multiple parameters. * There are two ways to avoid this. * 1. Forced conversion to an object type object, tell the compiler not to disassemble ** 2. Use an object [] array for packaging */main. invoke (person, (object) New String [] {"Zhang San"}); main. invoke (person, new object [] {New String [] {"Zhang Xueyou"});} catch (exception e) {e. printstacktrace ();}}}

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.