第一组::java.lang.Class类
- getName:获取全类名
- getSimpleName:获取简单类名
- getFields:获取所有public修饰的属性,包含本类以及父类的
- getDeclaredFields:获取本类中所有属性
- getMethods:获取所有public修饰的方法,包含本类以及父类的
- getDeclaredMethods:获取本类中所有方法
- getConstructors:获取本类所有public修饰的构造器
- getDecilaredConstructors:获取本类中所有构造器
- getPackage:以Package形式返回包信息
- getSuperClass:以Class形式返回父类信息
- getlnterfaces:以Class形式返回接口信息
- getAnnotations:以Annotation[ ] 形式返回注解信息
第二组: java.lang.reflect.Field类
- getModifiers:以int形式返回修饰符。[ 说明:默认修饰符是0,public 是1,private是2,protected是4,static是8,final是16 ]。如果有两个就相加,如:public(1) + static (8) = 9
- getType:以Class形式返回类型
- getName:返回属性名
第三组: java.lang.reflect.Method类
- getModifiers:以int形式返回修饰符。[说明:默认修饰符是0,public是1,private是2, protected是4,static是8,final是16]
- getReturnType:以Class形式获取返回类型
- getName:返回方法名
- getParameterTypes:以Class[ ]返回参数类型数组
第四组: java.lang.reflect.Constructor类
- getModifiers:以int形式返回修饰符
- getName:返回构造器名(全类名)
- getParameterTypes:以Class[ ]返回参数类型数组
应用实例
public class ReflectionUtils {
public static void main(String[] args) throws ClassNotFoundException {
}
@Test
public void api01() throws ClassNotFoundException {
//得到Class 对象
Class<?> personClass = Class.forName("com.kuang.reflection.Class_.Person");
//Class<Person> personClass = Person.class;
// 1. getName:获取全类名
System.out.println(personClass.getName()); //com.kuang.reflection.Class_.Person
// 2. getSimpleName:获取简单类名
System.out.println(personClass.getSimpleName()); //Person
// 3. getFields:获取所有public修饰的属性,包含本类以及父类的
Field[] fields = personClass.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
// 4. getDeclaredFields:获取本类中所有属性
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
// 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
Method[] methods = personClass.getMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
// 6. getDeclaredMethods:获取本类中所有方法
Method[] declaredMethods = personClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod.getName());
}
// 7. getConstructors: 获取本类所有public修饰的构造器
Constructor<?>[] constructors = personClass.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor.getName());
}
// 8. getDeclaredConstructors:获取本类中所有构造器
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor.getName());
}
// 9. getPackage:以Package形式返回包信息
System.out.println(personClass.getPackage()); //package com.kuang.reflection.Class_
// 10.getSuperClass:以Class形式返回父类信息
Class<?> superclass = personClass.getSuperclass();
System.out.println(superclass);
// 11.getInterfaces:以Class[]形式返回接口信息
Class<?>[] interfaces = personClass.getInterfaces();
for (Class<?> anInterface : interfaces) {
System.out.println(anInterface);
}
// 12.getAnnotations:以Annotation[]形式返回注解信息
Annotation[] annotations = personClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
@Test
public void api02() throws ClassNotFoundException {
//得到Class 对象
Class<?> personClass = Class.forName("com.kuang.reflection.Class_.Person");
//Class<Person> personClass = Person.class;
//[说明:默认修饰符是0,public是1,private是2,protected是4,static是8,final是16]
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println("本类中所有属性= "+declaredField.getName() + " 该属性的修饰符值= "
+ declaredField.getModifiers() + " 该属性的类型= " + declaredField.getType());
}
// 6. getDeclaredMethods:获取本类中所有方法
Method[] declaredMethods = personClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println("本类中所有方法= "+declaredMethod.getName()+
" 该方法的访问修饰符值= " + declaredMethod.getModifiers() +
" 该方法返回类型= "+ declaredMethod.getReturnType());
//输出当前这个方法的形参数组情况
Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println("该方法的形参类型= "+parameterTypes);
}
}
// 8. getDeclaredConstructors:获取本类中所有构造器
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println("本类中所有构造器= "+declaredConstructor.getName());
Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println("该构造器的形参类型= " + parameterType);
}
}
}
}
@Deprecated
class Person extends A implements IA,IB{
//属性
public String name;
protected int age;
private double sal;
String job;
//构造器
public Person(){ }
public Person(String name){ }
private Person(String name,int age){ }
//方法
public void m1() { }
protected void m2() { }
void m3() { }
private void m4(){ }
public void m5(String name) { }
}
class A{
public String hobby;
public void hi(){ }
public A(){}
}
interface IA{}
interface IB{}