package com.reflection;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Properties;
public class ReflectionTest {
@Test
public void reflectionStudy() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
//调用非私有的
//1.获得person类对象
Class personClass = Person.class;
//2.获得其构造器
Constructor constructor = personClass.getConstructor(String.class, int.class);
Person p1 = constructor.newInstance("张三", 12);
System.out.println(p1.toString());
//3.获得其属性
Field name = personClass.getDeclaredField("age");
//修改属性的方法 set
name.set(p1,14);
System.out.println(p1.toString());
//4.调用方法
Method showNation = personClass.getDeclaredMethod("showNation", String.class);
Object showNationInvoke = showNation.invoke(p1, "中国");
//调用私有的
//5.私有的构造器
Constructor declaredConstructor = personClass.getDeclaredConstructor(String.class);
//接下来是访问private的核心方法
declaredConstructor.setAccessible(true);
Person p2 = declaredConstructor.newInstance("王五");
System.out.println(p2.toString());
//6.私有的属性
Field name1 = personClass.getDeclaredField("name");
name1.setAccessible(true);
name1.set(p2,"赵六");
System.out.println(p2.toString());
//7.私有的方法
Method showMe = personClass.getDeclaredMethod("showMe", String.class);
showMe.setAccessible(true);
Object act = showMe.invoke(p2, "跳跃");
System.out.println(act);
}
@Test
public void reflectionStudy1() throws ClassNotFoundException {
//1.采用该类.class方法
Class personClass = Person.class;
//2.采用该类对象.getclass()方法
Person person = new Person();
Class extends Person> personClass1 = person.getClass();
//3.采用Class.forName(地址)方法 这个方法最常用,最能够体现出java动态性特点来
Class> forName = Class.forName("com.reflection.Person");
//4.采用类加载器
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
Class> loadClass = classLoader.loadClass("com.reflection.Person");
//这四种方法都是一个地址
System.out.println(personClass==personClass1);
System.out.println(personClass==forName);
System.out.println(personClass==loadClass);
}
@Test
public void reflectionStudy2() throws IOException {
//第一种加载方式,此时默认加载src文件夹下,当处理web项目时,一般多用此种方法
// Properties properties = new Properties();
// ClassLoader classLoader = ReflectionTest.class.getClassLoader();
// InputStream resourceAsStream = classLoader.getResourceAsStream("data1.properties");
// properties.load(resourceAsStream);
// Object user = properties.get("user");
// Object password = properties.get("password");
// System.out.println(user+"t"+password);
//第二种加载方式,此时默认加载当前module下
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("data2.properties");
FileInputStream fileInputStream1 = new FileInputStream("src\data1.properties");
properties.load(fileInputStream1);
Object user = properties.get("user");
Object password = properties.get("password");
System.out.println(user+"t"+password);
}
}
package com.reflection;
public class Person {
private String name;
public int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
public Person() {
}
private Person(String name) {
this.name=name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void showNation(String nation) {
System.out.println("我是" + nation);
}
private String showMe(String activity) {
System.out.println("我可以" + activity);
return activity;
}
}