Reflection in Java

Source: Internet
Author: User

Three kinds of mechanisms for class loading 1, by GetClass
People p =new People();Class c = p.getClass();
2, People.class
Class c1=People.class;
3, Class.forName ("Tianpo.com.demo.People");
Class c2=Class.forName("tianpo.com.demo.People");
Object comparison
//比较内存地址,类加载器只会创建一次System.out.println(c==c2); //比较的是属性值System.out.println(c.equals(c2));
Reflection

In fact, the class file is structured to disassemble:

    1. Constructors (with/without parameters)
    2. Member variables (properties)
    3. Method
Reflection constructors
Class c =Class.forName("tianpo.com.demo.People");## 有参Constructor constructor=c.getConstructor(String.class,int.class);## 无参Constructor constructor1=c.getConstructor();Object obj=constructor.newInstance("dd",22);People p =(People)obj;System.out.println(p);
Violent reflexes (not recommended, damaging encapsulation, security)
//构造函数是私有的 private People(String name, int age) {    super();    this.name = name;    this.age = age;}Class c =Class.forName("tianpo.com.demo.People");Constructor constructor=c.getDeclaredConstructor(String.class,int.class);//暴力反射,取消运行时权限检查constructor.setAccessible(true);Object obj=constructor.newInstance("dd",22);System.out.println(obj);
Dynamic invocation of internal methods
Class c = Class.forName("tianpo.com.demo.People");Constructor con = c.getConstructor();Method m =c.getMethod("eat");Object obj1=con.newInstance();m.invoke(obj1);
Get all member variables
Class c = Class.forName("tianpo.com.demo.People");# 获取所有public类型的成员变量Field[] fs=c.getFields();# 获取所有privateField[] fs1=c.getDeclaredFields();for(Field field:fs){    System.out.println(field);}Field field=c.getDeclaredField("age");Constructor con = c.getConstructor();Method m =c.getMethod("eat");Object obj1=con.newInstance();//动态赋值field.set(obj1,18);System.out.println(obj1);
Reflection-based generic erase
ArrayList<String> array = new ArrayList<String>();array.add("张三");// 编译时会做类型检查//array.add(10);Class c = array.getClass();Method m = c.getMethod("add",Object.class);m.invoke(array,10);m.invoke(array,13.8);System.out.println(array);
Create a config.properties by configuring the Execute call
className = tianpo.com.demo.PeoplemethodName = eatclassName = tianpo.com.demo.StudentmethodName = StudyclassName = tianpo.com.demo.WorkermethodName = Work
Reflection Call
FileReader rd = new FileReader("config.properties");Properties pro = new Properties();pro.load(rd);rd.close();String className = pro.getProperty("className");String methodName = pro.getProperty("methodName");Class c =Class.forName(className);Method m = c.getMethod(methodName);Object obj=c.newInstance();m.invoke(obj);

Reflection in Java

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.