目录
⚪权限修饰符 返回值类型 方法名(参数类型1 形参名1,......)throws XxxException{}
1.getConstructors():获取当前运行时类中声明为public的构造器
2.getDeclaredConstructors():获取当前运行时类中声明的所有的构造器
一、获取当前运行时类的属性结构
1.获取属性结构
getFields():获取当前运行时类及其父类中声明为public访问权限的属性
@Test
public void test1(){
Class clazz = Person.class;
//获取属性结构
Field[] fields = clazz.getFields();
for(Field f : fields){
System.out.println(f);
}
}
getDeclaredFields():获取当前运行时类中声明的所有属性(不包含父类中声明的属性)
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
System.out.println(f);
2.权限修饰符 数据类型 变量名
@Test
public void test2(){
Class clazz = Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
//1.权限修饰符
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers) + "\t");
//2.数据类型
Class type = f.getType();
System.out.print(type.getName() + "\t");
//3.变量名
String name = f.getName();
System.out.print(name);
System.out.println();
}
}
3.获取运行时类的方法结构
getMethods():获取当前运行时类及其所有父类中声明为public权限的方法
@Test
public void test1(){
Class clazz = Person.class;
Method[] methods = clazz.getMethods();
for(Method m : methods){
System.out.println(m);
}
}
getDeclaredMethods():获取当前运行时类中声明的所有方法(不包含父类中声明的方法)
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
System.out.println(m);
}
二、获取运行时类的方法的内部结构
⚪@Xxxx (注解)
⚪权限修饰符 返回值类型 方法名(参数类型1 形参名1,......)throws XxxException{}
1.获取方法声明的注解(getAnnotations())
@Test
public void test2(){
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
Annotation[] annotations = m.getAnnotations();
for(Annotation a : annotations){
System.out.println(a);
}
}
}
2.权限修饰符(getModifiers())
@Test
public void test2(){
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
Annotation[] annotations = m.getAnnotations();
for(Annotation a : annotations){
System.out.println(a);
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
System.out.println();
}
}
3.返回值类型(getReturnType())
@Test
public void test2(){
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
Annotation[] annotations = m.getAnnotations();
for(Annotation a : annotations){
System.out.println(a);
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//3.返回值类型
System.out.print(m.getReturnType().getName() + "\t");
System.out.println();
}
}
4.方法名(getName())
//4.方法名
System.out.print(m.getName() + "\t");
5.形参列表
//4.方法名
System.out.print(m.getName());
System.out.print("(");
//5.形参列表
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].getName() + "args_" + i);
break;
}
System.out.print(parameterTypes[i].getName() + "args_" + i + ",");
}
}
System.out.print(")");
System.out.println();
6.抛出的异常
//6.抛出的异常
Class[] exceptionTypes = m.getExceptionTypes();
if(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].getName());
break;
}
System.out.print(exceptionTypes[i].getName() + ",");
}
}
三、获取运行时类的构造器结构
1.getConstructors():获取当前运行时类中声明为public的构造器
@Test
public void test1(){
Class clazz = Person.class;
Constructor[] constructors = clazz.getConstructors();
for(Constructor c : constructors){
System.out.println(c);
}
}
2.getDeclaredConstructors():获取当前运行时类中声明的所有的构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor c : declaredConstructors){
System.out.println(c);
}
四、 获取运行时类的父类及父类的泛型
1.获取运行时类的父类
@Test
public void test2(){
Class clazz = Person.class;
Class superclass = clazz.getSuperclass();
System.out.println(superclass);
}
2. 获取运行时类带泛型的父类
@Test
public void test3(){
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
}
3. 获取运行时类带泛型的父类的泛型
@Test
public void test4(){
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
//获取泛型类型
Type[] actualTypeArguments = paramType.getActualTypeArguments();
System.out.println(actualTypeArguments[0].getTypeName());
}
五、获取运行时类的接口、所在包、注释等
1.获取运行时类实现的接口
@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);
}
}
2. 获取运行时类所在的包
@Test
public void test6(){
Class clazz = Person.class;
Package aPackage = clazz.getPackage();
System.out.println(aPackage);
}
3.获取运行时类声明的注解
@Test
public void test7(){
Class clazz = Person.class;
Annotation[] annotations = clazz.getAnnotations();
for(Annotation a : annotations){
System.out.println(a);
}
}
六、涉及到的相关代码
1.Creature类
package java2;
import java.io.Serializable;
public class Creature<T> implements Serializable {
private char gender;
public double weight;
private void breath(){
System.out.println("生物会呼吸");
}
public void eat(){
System.out.println("生物需要吃东西");
}
}
2.Person类
package java2;
@MyAnnotation(value = "hi")
public class Person extends Creature<String> implements Comparable<String>,MyInterface{
private String name;
int age;
public int id;
public Person(){}
@MyAnnotation(value = "abc")
private Person(String name){
this.name = name;
}
Person(String nme,int age){
this.name = name;
this.age = age;
}
@Override
public int compareTo(String o) {
return 0;
}
@MyAnnotation
private String show(String nation){
System.out.println("我的国籍是:" + nation);
return nation;
}
public String display(String interests,int age) throws NullPointerException,ClassCastException{
return interests + age;
}
@Override
public void info() {
System.out.println("我是一个人");
}
}
3.自定义注解:MyAnnotation
package java2;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}
4.自定义接口:MyInterFace
package java2;
public interface MyInterface {
void info();
}