Java的java.lang.reflect.Constructor应用

本文详细介绍了Java反射中Constructor类的使用,包括创建Constructor对象、使用Constructor创建实例、获取构造函数注解,以及Constructor的相关方法和示例,适用于JDK1.8和SpringBoot2.6.3环境。
摘要由CSDN通过智能技术生成

记录:479

场景:java.lang.reflect.Constructor的使用,包括但不限于创建Constructor,使用Constructor创建对象实例,获取类的构造函数上的注解,以及其它常用方式使用。在Java反射机制中发挥重要作用。

版本:JDK 1.8,Spring Boot 2.6.3。

1.基础

1.1Constructor

在JDK的java.lang.reflect.Constructor中对Constructor的官方说明如下:

Constructor provides information about, and access to, a single constructor for a class.

Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor's formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.

1.2Java 反射机制

Java 反射机制是在运行状态中,对于任意一个类,都能够获得这个类的所有属性和方法,对于任意一个对象都能够调用它的任意一个属性和方法。这种在运行时动态的获取信息以及动态调用对象的方法的功能称为Java 的反射机制。

2.创建Constructor对象

请注意

(1)Java的Constructor是一个final修饰的类,不可被继承。

(2)Constructor没有public修饰的构造函数,不能使用new关键字创建对象。

(3)使用Class对象获取Constructor对象。

//创建Class对象
Class<?> clz01 = ThreadPoolExecutor.class;
//1.使用getConstructor获取类的构造函数
Constructor<?> con01 = clz01.getConstructor(int.class, int.class, long.class, TimeUnit.class, BlockingQueue.class);
//2.使用getConstructors获取类的全部构造函数
Constructor<?>[] cons02 = clz01.getConstructors();
//3.使用getDeclaredConstructor获取类的构造函数
Constructor<?> con03 = clz01.getDeclaredConstructor(int.class, int.class, long.class, TimeUnit.class, BlockingQueue.class);
//4.使用getDeclaredConstructors获取类的全部构造函数
Constructor<?>[] cons04 = clz01.getDeclaredConstructors();
//5.使用getEnclosingConstructor获取类的构造函数(针对局部类和匿名类)
Class<?> clz02 = new FujianProvince().city.getClass();
Constructor<?> cons05 = clz02.getEnclosingConstructor();

3.使用Constructor创建类的实例对象

Class<?> clz = ThreadPoolExecutor.class;
Constructor<?> con = clz.getConstructor(int.class, int.class, long.class, TimeUnit.class, BlockingQueue.class);
ThreadPoolExecutor pool = (ThreadPoolExecutor) con.newInstance(10, 10, 10, TimeUnit.MILLISECONDS, new LinkedBlockingDeque(10));

4.使用Constructor获取类构造函数上的注解

Constructor<?> conWork = WorkArea.class.getConstructor();
Annotation ann01 = conWork.getAnnotation(ProvinceAno.class);
Annotation[] ann02 = conWork.getAnnotations();
Annotation ann03 = conWork.getDeclaredAnnotation(ProvinceAno.class);
Annotation[] ann04 = conWork.getDeclaredAnnotations();
AnnotatedType ann05 = conWork.getAnnotatedReceiverType();
AnnotatedType ann06 = conWork.getAnnotatedReturnType();
AnnotatedType[] ann07 = conWork.getAnnotatedExceptionTypes();
AnnotatedType[] ann08 = conWork.getAnnotatedParameterTypes();

5.其它常用方法

//1.获取构造函数的Class对象
Class<?> clz03 = con01.getDeclaringClass();
//2.获取构造函数名称
String name01 = con01.getName();
//3.获取构造函数的modifiers
int mod01 = con01.getModifiers();
//4.获取参数类型
TypeVariable<? extends Constructor<?>>[] type01 = con01.getTypeParameters();
//5.返回形参的类型的Class对象数组
Class<?>[] clz04 = con01.getParameterTypes();
//6.获取形参数量
int num01 = con01.getParameterCount();
//7.返回形参的类型的Type对象数组
Type[] type02 = con01.getGenericParameterTypes();
//8.返回Class对象数组,表示函数执行时抛出的异常类型
Class<?>[] clz05 = con01.getExceptionTypes();
//9.返回Type对象数组,表示函数执行时抛出的异常类型
Type[] type03 = con01.getGenericExceptionTypes();
//10.构造函数的形式参数是可变参数返回true,否则返回false.
boolean bl01 = con01.isVarArgs();
//11.构造函数的是合成构造返回true,否则返回false.
boolean bl02 = con01.isSynthetic();

6.辅助类

6.1ProvinceAno

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
public @interface ProvinceAno {
    //省编码
    String code();
    //省名称
    String value() default "";
}

6.2WorkArea

public class WorkArea {
    @ProvinceAno(code = "35",value = "福建")
    public WorkArea(){
        System.out.println("省级行政区");
    }
}

以上,感谢。

2024年2月28日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值