文章目录
- 1. 反射机制概述
- 1.1 反射机制提供的功能
- 1.2 反射相关API
- 1.3 反射的应用举例
- 2. Class类
- 2.1 Class类概述
- 2.2 Class类的常用方法
- 2.3 获取Class类的实例(四种方法)
- 2.4 哪些类型可以有Class对象
- 3.类的加载 和 ClassLoader
- 3.1 类的加载过程
- 3.3.1 概述
- 3.3.2 ClassLoader相关方法
- 3.3.3 ClassLoader 相关方法具体代码演示
- 4.创建运行时类的对象
- 4.1 newInstance方法
- 4.2 体会反射的动态性
- 5. 获取运行时类的完整结构
- 5.1 方法概述
- 5.2 获取属性结构
- 5.3 获取运行时类的方法结构
- 5.4 获取构造器结构
- 5.5 获取运行时类的父类 & 泛型 相关
- 5.5.1 获取运行时类的父类
- 5.5.2 获取运行时类的带泛型的父类
- 5.5.3 获取运行时类的带泛型的父类的泛型
- 5.6 获取运行时类声明的注解
- 5.7 获取运行时类所在的包
- 5.8 获取运行时类实现的接口相关&父类实现的接口
- 6. 调用运行时类中指定的结构:属性、方法、构造器
- 6.1 如何操作运行时类中的指定的方法
- 6.2 如何操作运行时类中的指定的属性
- 6.3 调用运行时类中的指定的构造器 (使用较少,大部分用 .newInstance调用空参构造器)
1. 反射机制概述
1.1 反射机制提供的功能
1.2 反射相关API
1.3 反射的应用举例
public class ReflectionTest {
@Test
public void test1(){
Person p1 = new Person("tom",12);
p1.age = 10;
System.out.println(p1);
p1.show();
}
@Test
public void test2() throws Exception {
Class clazz = Person.class;
Constructor cons = clazz.getConstructor(String.class, int.class);
Object obj = cons.newInstance("tom", 12);
Person p = (Person)obj;
System.out.println(obj.toString());
Field age = clazz.getDeclaredField("age");
age.set(p,10);
System.out.println(p.toString());
Method show = clazz.getDeclaredMethod("show");
show.invoke(p);
System.out.println("********************");
Constructor cons1 = clazz.getDeclaredConstructor(String.class);
cons1.setAccessible(true);
Object jerry = cons1.newInstance("jerry");
Person p1 = (Person)jerry;
System.out.println(p1);
Field name = clazz.getDeclaredField("name");
name.setAccessible(true);
name.set(p1,"hanmeimei");
System.out.println(p1);
Method showNation = clazz.getDeclaredMethod("showNation", String.class);
showNation.setAccessible(true);
String nation = (String)showNation.invoke(p1, "中国");
System.out.println(nation);
}
2. Class类
2.1 Class类概述
2.2 Class类的常用方法
2.3 获取Class类的实例(四种方法)
代码演示:
@Test
public void test3() throws ClassNotFoundException {
Class clazz1 = Person.class;
System.out.println(clazz1);
Person p1 = new Person();
Class clazz2 = p1.getClass();
System.out.println(clazz2);
Class clazz3 = Class.forName("studyjava.day11_reflect.java.Person");
System.out.println(clazz3);
System.out.println(clazz1 == clazz2);
System.out.println(clazz1 == clazz3);
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
Class clazz4 = classLoader.loadClass("studyjava.day11_reflect.java.Person");
System.out.println(clazz1 == clazz4);
}
}
2.4 哪些类型可以有Class对象
@Test
public void test4(){
Class c1 = Object.class;
Class c2 = Comparable.class;
Class c3 = String[].class;
Class c4 = int[][].class;
Class c5 = ElementType.class;
Class c6 = Override.class;
Class c7 = int.class;
Class c8 = void.class;
Class c9 = Class.class;
int[] a = new int[10];
int[] b = new int[100];
Class c10 = a.getClass();
Class c11 = b.getClass();
System.out.println(c10 == c11);
}
3.类的加载 和 ClassLoader
3.1 类的加载过程
## 3.2 类的初始化
## 3.3 类加载器 ClassLoader
3.3.1 概述
3.3.2 ClassLoader相关方法
3.3.3 ClassLoader 相关方法具体代码演示
public class ClassLoaderTest {
@Test
public void test1(){
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
System.out.println(classLoader);
ClassLoader classLoader1 = classLoader.getParent();
System.out.println(classLoader1);
ClassLoader classLoader2 = classLoader1.getParent();
System.out.println(classLoader2);
ClassLoader classLoader3 = String.class.getClassLoader();
System.out.println(classLoader3);
}
@Test
public void test2() throws Exception {
Properties pros = new Properties();
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("jdbc1.properties");
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
System.out.println("user = " + user + ",password = " + password);
}
}
4.创建运行时类的对象
4.1 newInstance方法
@Test
public void test1() throws IllegalAccessException, InstantiationException {
Class clazz = Person.class;
Person obj = (Person) clazz.newInstance();
System.out.println(obj);
}
}
4.2 体会反射的动态性
@Test
public void test2() {
for (int i = 0; i < 100; i++) {
int num = new Random().nextInt(3);
String classPath = "";
switch (num) {
case 0:
classPath = "java.util.Date";
break;
case 1:
classPath = "java.lang.Object";
break;
case 2:
classPath = "studyjava.day11_reflect.java.Person";
break;
}
try {
Object obj = getInstance(classPath);
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Object getInstance(String classPath) throws Exception {
Class clazz = Class.forName(classPath);
return clazz.newInstance();
}
}
5. 获取运行时类的完整结构
5.1 方法概述
5.2 获取属性结构
public class FieldTest {
@Test
public void test1(){
Class clazz = Person.class;
Field[] fields = clazz.getFields();
for (Field f : fields){
System.out.println(f);
}
System.out.println();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields){
System.out.println(f);
}
}
@Test
public void test2(){
Class clazz = Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields){
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers) + "\t");
Class type = f.getType();
System.out.print(type.getTypeName() + "\t");
String fName = f.getName();
System.out.print(fName + "\t");
System.out.println();
}
}
}
5.3 获取运行时类的方法结构
public class MethodTest {
@Test
public void test1(){
Class clazz = Person.class;
Method[] methods = clazz.getMethods();
for (Method m : methods){
System.out.println(m);
}
System.out.println();
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods){
System.out.println(m);
}
}
@Test
public void test2() {
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
Annotation[] annos = m.getAnnotations();
for (Annotation a : annos) {
System.out.println(a);
}
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
System.out.print(m.getReturnType().getTypeName() + "\t");
System.out.print(m.getName());
System.out.print("(");
Class[] parameterTypes = m.getParameterTypes();
if (!(parameterTypes == null && parameterTypes.length == 0)){
for (int i = 0; i < parameterTypes.length; i++ ){
if (i == parameterTypes.length - 1){
System.out.print(parameterTypes[i].getTypeName() + " args_" + i);
break;
}
System.out.print(parameterTypes[i].getTypeName() + " args_" + i + ",");
}
}
System.out.print(")");
Class[] exceptionTypes = m.getExceptionTypes();
if (!(exceptionTypes == null || exceptionTypes.length == 0)){
System.out.print("throws ");
for (int i = 0; i < exceptionTypes.length; i++){
if (i == exceptionTypes.length - 1){
System.out.print(exceptionTypes[i].getTypeName());
break;
}
System.out.print(exceptionTypes[i].getTypeName() + ",");
}
}
System.out.println();
}
}
}
5.4 获取构造器结构
@Test
public void test1() {
Class clazz = Person.class;
Constructor[] constructors = clazz.getConstructors();
for (Constructor c : constructors) {
System.out.println(c);
}
System.out.println();
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor c : declaredConstructors) {
System.out.println(c);
}
}
5.5 获取运行时类的父类 & 泛型 相关
5.5.1 获取运行时类的父类
@Test
public void test2() {
Class clazz = Person.class;
Class superclass = clazz.getSuperclass();
System.out.println(superclass);
}
5.5.2 获取运行时类的带泛型的父类
@Test
public void test3() {
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
}
5.5.3 获取运行时类的带泛型的父类的泛型
@Test
public void test4() {
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = paramType.getActualTypeArguments();
System.out.println(((Class)actualTypeArguments[0]).getName());
}
}
5.6 获取运行时类声明的注解
@Test
public void test7(){
Class clazz = Person.class;
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annos : annotations){
System.out.println(annos);
}
}
5.7 获取运行时类所在的包
@Test
public void test6(){
Class clazz = Person.class;
Package pack = clazz.getPackage();
System.out.println(pack);
}
5.8 获取运行时类实现的接口相关&父类实现的接口
@Test
public void test5(){
Class clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for (Class c : interfaces){
System.out.println(c);
}
System.out.println();
Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
for (Class c : interfaces1){
System.out.println(c);
}
}
}
6. 调用运行时类中指定的结构:属性、方法、构造器
6.1 如何操作运行时类中的指定的方法
@Test
public void testMethod() throws Exception {
Class clazz = Person.class;
Person p = (Person) clazz.newInstance();
Method show = clazz.getDeclaredMethod("show", String.class);
show.setAccessible(true);
Object returnValue = show.invoke(p, "CHN");
System.out.println(returnValue);
System.out.println("*****************************");
Method showDesc = clazz.getDeclaredMethod("showDesc");
showDesc.setAccessible(true);
Object returnVal = showDesc.invoke(Person.class);
System.out.println(returnVal);
}
6.2 如何操作运行时类中的指定的属性
@Test
public void testField1() throws Exception {
Class clazz = Person.class;
Person p = (Person) clazz.newInstance();
Field name = clazz.getDeclaredField("name");
name.setAccessible(true);
name.set(p,"tom");
System.out.println(name.getName());
}
6.3 调用运行时类中的指定的构造器 (使用较少,大部分用 .newInstance调用空参构造器)
@Test
public void testConstructor() throws Exception {
Class clazz = Person.class;
Constructor constructor = clazz.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
Person per = (Person) constructor.newInstance("tom");
System.out.println(per);
}