import java.lang.reflect.*;
import java.util.Date;
public class RefelectTest {
/**
* 通过反射机制获取类的基本信息(包名,修饰符,类名,基类,实现的接口)
*/
public static void getClassBaseInfo() {
try {
// 通过String指定要获取那个类的Class,注意,必须是完整的类名,也就是包名+类名格式
String className = "com.powernode.refelect.test1.Student";
// 获取指定类名的Class
Class claz = Class.forName(className);
// 取得包名的信息
System.out.println("包名:" + claz.getPackage().getName());
// 取得修饰符的信息
System.out.println("修饰符:" + Modifier.toString(claz.getModifiers()));
// 取得类名的信息(完整的类名)
System.out.println("类名:" + claz.getName());
// 取得类名的信息(简单的类名)
System.out.println("类名:" + claz.getSimpleName());
// 取得基类的信息
System.out.println("基类:" + claz.getSuperclass().getName());
// 取得接口的信息
Class[] interfaceClazs = claz.getInterfaces();
if (interfaceClazs.length > 0) {
System.out.print("实现的接口:");
for (Class interfaceClaz : interfaceClazs) {
System.out.print(interfaceClaz.getName() + ",");
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 通过反射机制获取类的方法的信息(修饰符,返回类型,方法名称,参数列表,抛出的异常)
*/
public static void getMethodInfo() {
try {
// 通过String指定要获取那个类的Class,注意,必须是完整的类名,也就是包名+类名格式
String className = "com.powernode.refelect.test1.Student";
// 获取指定类名的Class
Class claz = Class.forName(className);
// 获取方法的信息,claz.getMethods()获取类中的所有的公开的方法的信息(包括继承下来的公开的方法)
// Method[] methods = claz.getMethods();
// claz.getDeclaredMethods()获取类中声明的所有的方法的信息(包括公开的,保护的,缺省的,私有的,但是没有继承的)
Method[] methods = claz.getDeclaredMethods();
// 遍历Method的数组
for (Method method : methods) {
// 从方法上面取得方法的各个信息
System.out
.print(Modifier.toString(method.getModifiers()) + " ");
// 取得方法的返回类型信息
System.out.print(method.getReturnType().getName() + " ");
// 取得方法名称信息
System.out.print(method.getName() + " ");
// 获取方法的参数的信息
System.out.print("(");
Class[] paramClazs = method.getParameterTypes();
for (Class paramClaz : paramClazs) {
System.out.print(paramClaz.getName() + ",");
}
System.out.print(")");
// 取得异常的信息
Class[] exceptionClazs = method.getExceptionTypes();
if (exceptionClazs.length > 0) {
System.out.print(" throws ");
for (Class exceptionClaz : exceptionClazs) {
System.out.print(exceptionClaz.getName() + ",");
}
}
System.out.println();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 通过反射机制获取构造方法的信息(修饰符,类名,参数列表,抛出的异常)
*/
public static void getConstructorInfo() {
try {
// 通过String指定要获取那个类的Class,注意,必须是完整的类名,也就是包名+类名格式
String className = "com.powernode.refelect.test1.Student";
// 获取指定类名的Class
Class claz = Class.forName(className);
// 获取构造方法的信息
// claz.getConstructors()获取类中公开的构造方法的信息
// Constructor[] cons =claz.getConstructors();
// claz.getConstructors()获取类中声明的的构造方法的信息(包括公开的,保护的,私有的,缺省的)
Constructor[] cons = claz.getDeclaredConstructors();
if (cons.length > 0) {
// 遍历每一个构造方法,分别取得构造方法的信息
for (Constructor con : cons) {
// 取得修饰符的信息
System.out.print(Modifier.toString(con.getModifiers())
+ " ");
// 构造方法名称(类名)
System.out.print(con.getName() + " ");
// 取得参数的信息
System.out.print("(");
Class[] paramClazs = con.getParameterTypes();
if (paramClazs.length > 0) {
for (Class paramClaz : paramClazs) {
// System.out.print(paramClaz.getName() + "-" +
// paramClaz.getSimpleName() + "," );
System.out.print(paramClaz.getSimpleName() + ",");
}
}
System.out.print(") ");
// 取得异常的信息
Class[] exceptionClazs = con.getExceptionTypes();
if (exceptionClazs.length > 0) {
System.out.print(" throws ");
for (Class exceptionClaz : exceptionClazs) {
System.out.print(exceptionClaz.getName() + ",");
}
}
System.out.println();
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过反射机制创建对象
*
* @return
*/
public static Object createObject() {
Object obj = null;
try {
// 通过String指定要获取那个类的Class,注意,必须是完整的类名,也就是包名+类名格式
String className = "com.powernode.refelect.test1.Student";
// 获取指定类名的Class
Class claz = Class.forName(className);
// 通过参数列表,获取指定的构造方法
Class[] paramClazs = { String.class, String.class, Sex.class,
int.class };
Constructor con = claz.getDeclaredConstructor(paramClazs);
// 设置con是可以访问的
con.setAccessible(true);
obj = con.newInstance("张三", "1987-12-8", Sex.男, 178);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
/**
* 通过反射机制调用类中的方法
*/
public static void invokeMethod() {
// 获取Student类的Class
Class claz = Student.class;
try {
// Method m1 = claz.getDeclaredMethod("toString1", Integer.class);
Method m1 = claz.getDeclaredMethod("toString1", Integer.TYPE);
m1.setAccessible(true);
// 调用方法,创建对象
Object obj = createObject();// "张三", "1987-12-8", Sex.男, 178
Object res = m1.invoke(obj, 100);
System.out.println(res);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void changeField() {
try {
Object obj = createObject();
Class claz = obj.getClass();
// 取得类中的属性,也就是Field
Field nameField = claz.getDeclaredField("name");
Field sexField = claz.getDeclaredField("sex");
Field heightField = claz.getDeclaredField("height");
Field birthdayField = claz.getDeclaredField("birthday");
nameField.setAccessible(true);
sexField.setAccessible(true);
heightField.setAccessible(true);
birthdayField.setAccessible(true);
// 修改属性的值
heightField.setInt(obj, 180);
nameField.set(obj,"张4");
birthdayField.set(obj, new Date());
sexField.set(obj, Sex.女);
// 取得对象上面的属性
int height = heightField.getInt(obj);
String name = (String)nameField.get(obj);
Sex sex = (Sex)sexField.get(obj);
Date birthday = (Date)birthdayField.get(obj);
System.out.println(height + "," + name + "," + sex + "," + birthday);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// getClassBaseInfo();
// getMethodInfo();
// getConstructorInfo();
// Object obj = createObject();
// System.out.println(obj);
//invokeMethod();
changeField();
}
}
反射操作
最新推荐文章于 2022-08-31 10:51:59 发布