Java reflection, developing framework-required skills

Source: Internet
Author: User

Using the reflection technology, we map the Uniform Resource Locator (URL) above to the class equivalent

Class:news

Method:list

Parameter:2

It's almost like that.

Class news{

public string list (string catagory_id) {

...

...

}

}

We only need to parse the URL in the framework core and then call the corresponding method to download , at the same time pass the parameters in the past.

Class<?> cls = Class.forName ("Cn.netkiller.reflect.News"); Object obj= cls.newinstance (); method = Cls.getdeclaredmethod ("list", Int.class); return Method.invoke (obj, 2);


1.9. Reflection Reflex
This.getclass (). GetName ()//Current class name Thread.CurrentThread (). Getstacktrace () [1].getmethodname ()]; Current method Name
1.9.1. Get all variables
field[] fields = Objclass.getfields (), for (Field field:fields) {System.out.println (Field.getname ());}

Note: Only public variables can be removed

1.9.2. Batch assignment 1.9.3. Method Operation Download

The steps for invoking a method in Java reflection are three-step

Gets the class that is called to call the method method call (Invoke) in the class to be called
1.9.3.1. Get all the methods
Class<?> objclass = A.getclass (); Method[] methods = Objclass.getdeclaredmethods (); for (method Method:methods) {System.out.println (method);}
1.9.3.2. Set/get Method Download
package cn.netkiller.reflect;import java.lang.reflect.invocationtargetexception;import  Java.lang.reflect.method;public class member {public string name;private int  age;public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} @Overridepublic  string tostring ()  {return  "Classa [name="  + name +   ",  age="  + age +  "]";} Public member ()  {// todo auto-generated constructor stub}public static  void main (String[] args)  throws classnotfoundexception, instantiationexception,  IllegalAccessException, NoSuchMethodException, SecurityException,  Illegalargumentexception, invocationtargetexceptIon {class<?> cls = class.forname ("Cn.netkiller.reflect.Member"); object member  = cls.newinstance (); Method setmethod = cls.getdeclaredmethod ("Setage",  int.class); SetMethod.invoke (Member, &NBSP;15); Method getmethod = cls.getdeclaredmethod ("Getage"); System.out.println (Getmethod.invoke (member));}}

Package cn.netkiller.reflect;public class classa {public string name;private  int age;public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} Public classa ()  {// todo auto-generated constructor stub} @Overridepublic   String tostring ()  {return  "classa [name="  + name +  ",  age="   + age +  "]";}} PACKAGE&NBSP;CN.NETKILLER.REFLECT;PUBLIC&NBSP;CLASS&NBSP;CLASSB&NBSP;EXTENDS&NBSP;CLASSA{PUBLIC&NBSP;CLASSB ()  {// todo auto-generated constructor stub}private string address;public  string getaddress ()  {return address;} Public void setaddress (string address)  {this.address = adDress;} @Overridepublic  string tostring ()  {return  "classb [address="  + address  +  "]";}} package cn.netkiller.reflect;import java.lang.reflect.field;import  java.lang.reflect.invocationtargetexception;import java.lang.reflect.method;public class  Reflectiontest {public reflectiontest ()  {// todo auto-generated constructor  stub} download Public void testsetmethod ()  throws NoSuchMethodException,  Securityexception, illegalaccessexception, illegalargumentexception, invocationtargetexception,  instantiationexception {// classa a = new classa (); CLASSB&NBSP;B&NBSP;=&NBSP;NEW&NBSP;CLASSB (); B.setaddress ("Shenzhen"); class<classa> classa = classa.class; Classa a = classa.newinstance (); A.setname ("Neo"); A.setage (30); System.out.println (Classa.getdeclaredmethod ("Getage"). Invoke (a)); Method m&nbsP;= classa.getdeclaredmethod ("Setage",  int.class); M.setaccessible (true); //  because it is written as private   So it must be set M.invoke (B,&NBSP;26); System.out.println (A.tostring ()); System.out.println (B.tostring ()); System.out.println (B.getname ()); System.out.println (B.getage ());} Public static void main (String[] args)  throws invocationtargetexception { Reflectiontest rt = new reflectiontest (); Try {rt.testsetmethod ();}  catch  (nosuchmethodexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (securityexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (illegalaccessexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (illegalargumentexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch&nBSP; (instantiationexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}}

Set method

System.out.println (Classa.getdeclaredmethod ("Getage"). Invoke (a));

Get method

Method m = Classa.getdeclaredmethod ("Setage", Int.class); m.setaccessible (true);//Because it is written in private, it must be set to M.invoke (b, 26) ;
1.9.3.3. Static method invocation

Class cls = Class.forName ("cn.netkiller.reflect.Student"); Method Setmethod = Cls.getdeclaredmethod ("Setage", Int.class); Setmethod.invoke (Cls.newinstance (), 15);


Java reflection, developing framework-required skills

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.