作者:残阳丶
一丶Class中常用方法详解
1)getFields()
只能获取到运行时类中及其父类中声明为public的属性
2)getDeclaredFields()
获取运行时类本身声明的所有属性
3)getMethods()
获取运行时类中及其父类中声明为public的方法
4)getDeclaredMethods()
获取运行时类本身声明的所有方法
5)getSuperclass()
获取运行时类的父类
6)getGenericSuperclass()
获取运行时类带泛型的父类
7)获取运行时类的父类的泛型如下:
Class clazz = Person.class;
Type type = clazz.getGenericSuperclass();//获取带泛型的父类
ParameterizedType param = (ParameterizedType)type;//强转为子类
Type[] ars = param.getActualTypeArguments();//获得父类的泛型,可能有多个,例如map<k,v>
System.out.println(((Class)ars[0]).getName()); //Class是Type接口的实现类,所以可以强转,输出父类的泛型
8)getInterfaces()
获取运行时类实现的接口
9)getPackage()
获取运行时类所在的包
10)getAnnotations()
获取运行时类的注解
二丶Field中常用方法详解
1)getModifiers()
获得属性的权限修饰符,返回对应的整数形式
2)getType()
获取属性的类型,返回的是Class对象
3)getName()
获取属性名
三丶Method中常用方法详解
1)getAnnotations
()
获取方法上的所有注解
2)getName()
获取方法名
3)getModifiers()
获得方法的权限修饰符,返回对应的整数形式
4)getReturnType()
获取方法的返回值类型,返回的是Class对象
5)getParameterTypes()
获取方法的形参类型,返回的是Class[] 对象
6)getExceptionTypes()
获取方法的抛出异常类型,返回的是Class[] 对象
-------------------
注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationsTest {
public String name();
}
父类:
public abstract class Person {
public String personName;
int personAge;
public void play(){
}
void learn(){
}
}
子类:
@AnnotationsTest(name = "helloPerson")
public class HelloPerson extends Person {
public String helloPersonName;
int helloPersonAge;
public String helloPersonWeight;
private String helloPersonHeight;
protected String sex;
public HelloPerson() {
}
public HelloPerson(String helloPersonName, int helloPersonAge) {
this.helloPersonName = helloPersonName;
this.helloPersonAge = helloPersonAge;
}
public void sayHello(String msg){
System.out.println(msg);
}
private void sayGoodBye(){
System.out.println("goodBye");
}
public static void staticTest(){
System.out.println("static method...");
}
public String getHelloPersonName() {
return helloPersonName;
}
public int getHelloPersonAge() {
return helloPersonAge;
}
}
测试类:
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class NioTest3 {
public static void main(String[] args) throws ClassNotFoundException {
String className = "com.reflect.HelloPerson";
Class clazz = Class.forName(className);
Field [] fields = clazz.getFields();
System.out.println("----Fields(只能获取到运行时类中及其父类中声明为public的属性):----");
for(Field field:fields){
System.out.println(field.getName());
}
System.out.println("----declaredFields(获取运行时类本身声明的所有属性):----");
Field [] declaFields = clazz.getDeclaredFields();
for(Field field:declaFields){
System.out.println("fieldName:"+field.getName());
System.out.println("fieldModifiers:"+field.getModifiers());
System.out.println("fieldType:"+field.getType());
}
System.out.println("----Methods(获取运行时类中及其父类中声明为public的方法):----");
Method [] methods = clazz.getMethods();
for(Method method:methods){
System.out.println("methodName:"+method.getName());
System.out.println("methodModifiers:"+method.getModifiers());
System.out.println("methodReturnType:"+method.getReturnType());
System.out.println("methodParameterTypes:"+method.getParameterTypes());
}
System.out.println("----declaredMethods(本类所有方法):----");
Method [] declaMethods = clazz.getDeclaredMethods();
for(Method method:declaMethods){
System.out.println(method.getName());
}
System.out.println("----getSuperClass:----");
System.out.println(clazz.getSuperclass().getName());
System.out.println("----getGenericSuperClass(获取运行时类带泛型的父类):----");
System.out.println(clazz.getGenericSuperclass().getClass().getName());
System.out.println("----getInterfaces(获取运行时类实现的接口):----");
Class []clazzs = clazz.getInterfaces();
for(Class c:clazzs){
System.out.println(c.getName());
}
System.out.println("----getPackage:----");
Package p = clazz.getPackage();
System.out.println(p.getName());
System.out.println("----getAnnotations:----");
Annotation []as = clazz.getAnnotations();
for(Annotation a:as){
System.out.println(a.toString());
System.out.println(a.getClass().getName());
}
}
}
运行结果:
----Fields(只能获取到运行时类中及其父类中声明为public的属性):----
helloPersonName
helloPersonWeight
personName
----declaredFields(获取运行时类本身声明的所有属性):----
fieldName:helloPersonName
fieldModifiers:1
fieldType:class java.lang.String
fieldName:helloPersonAge
fieldModifiers:0
fieldType:int
fieldName:helloPersonWeight
fieldModifiers:1
fieldType:class java.lang.String
fieldName:helloPersonHeight
fieldModifiers:2
fieldType:class java.lang.String
fieldName:sex
fieldModifiers:4
fieldType:class java.lang.String
----Methods(获取运行时类中及其父类中声明为public的方法):----
methodName:sayHello
methodModifiers:1
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@5c647e05
methodName:staticTest
methodModifiers:9
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@33909752
methodName:getHelloPersonName
methodModifiers:1
methodReturnType:class java.lang.String
methodParameterTypes:[Ljava.lang.Class;@55f96302
methodName:getHelloPersonAge
methodModifiers:1
methodReturnType:int
methodParameterTypes:[Ljava.lang.Class;@3d4eac69
methodName:play
methodModifiers:1
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@42a57993
methodName:wait
methodModifiers:17
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@75b84c92
methodName:wait
methodModifiers:17
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@6bc7c054
methodName:wait
methodModifiers:273
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@232204a1
methodName:equals
methodModifiers:1
methodReturnType:boolean
methodParameterTypes:[Ljava.lang.Class;@4aa298b7
methodName:toString
methodModifiers:1
methodReturnType:class java.lang.String
methodParameterTypes:[Ljava.lang.Class;@7d4991ad
methodName:hashCode
methodModifiers:257
methodReturnType:int
methodParameterTypes:[Ljava.lang.Class;@28d93b30
methodName:getClass
methodModifiers:273
methodReturnType:class java.lang.Class
methodParameterTypes:[Ljava.lang.Class;@1b6d3586
methodName:notify
methodModifiers:273
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@4554617c
methodName:notifyAll
methodModifiers:273
methodReturnType:void
methodParameterTypes:[Ljava.lang.Class;@74a14482
----declaredMethods(本类所有方法):----
sayGoodBye
sayHello
staticTest
getHelloPersonName
getHelloPersonAge
----getSuperClass:----
com.reflect.Person
----getGenericSuperClass(获取运行时类带泛型的父类):----
java.lang.Class
----getInterfaces(获取运行时类实现的接口):----
----getPackage:----
com.reflect
----getAnnotations:----
@com.reflect.AnnotationsTest(name=helloPerson)
com.sun.proxy.$Proxy1