Constructor详解

Constructor 提供获取构造方法信息,并可生成实例。
一个构造方法的属性有:
方法上使用的注解、方法的修饰符、方法上定义的泛型参数、方法名称、方法参数(泛型、注解)、方法抛出的异常。

比如下面这个方法:

  1. @MyAnnotation  
  2. public <T>  ConstructorTest(@MyAnnotation List<T> list, T... params) throws RuntimeException,  
  3.         Exception {  
  4.       
  5. }  
注解:@MyAnnotation
修饰符:public
泛型参数:T
方法名:ConstructorTest
方法参数(泛型、注解):List<T> list,T...params
抛出的异常:RuntimeException,Exception


以上就是一个完整构造方法的所有信息,当然构造函数的主要作用还是用来生成对象实例的。

下面我们来看看构造函数信息

  1. package reflect;  
  2.   
  3. import java.lang.annotation.Annotation;  
  4. import java.lang.reflect.Constructor;  
  5. import java.lang.reflect.GenericArrayType;  
  6. import java.lang.reflect.Modifier;  
  7. import java.lang.reflect.ParameterizedType;  
  8. import java.lang.reflect.Type;  
  9. import java.lang.reflect.TypeVariable;  
  10. import java.lang.reflect.WildcardType;  
  11. import java.util.ArrayList;  
  12. import java.util.Arrays;  
  13. import java.util.List;  
  14.   
  15. /** 
  16.  * @author zhangquanit 
  17.  */  
  18. public class ConstructorTest<T> {  
  19.   
  20.     private List<T> mList;  
  21.     private T[] mParams;  
  22.   
  23.     public ConstructorTest(){  
  24.           
  25.     }  
  26.     @MyAnnotation  
  27.     public ConstructorTest(@MyAnnotation List<T> list, T... params)  
  28.             throws RuntimeException, Exception {  
  29.         this.mList = list;  
  30.         this.mParams = params;  
  31.   
  32.     }  
  33.   
  34.     public static void main(String[] args) throws Exception {  
  35.         // 获取构造函数  
  36.         Constructor<ConstructorTest> constructor = ConstructorTest.class  
  37.                 .getDeclaredConstructor(List.class, Object[].class);  
  38.         if (!constructor.isAccessible()) {  
  39.             constructor.setAccessible(true);  
  40.         }  
  41.         // 构造函数信息  
  42.         constructorInfo(constructor);  
  43.   
  44.         // 创建实例  
  45.         List<String> arrayList = new ArrayList<String>();  
  46.         arrayList.add("1");  
  47.         arrayList.add("2");  
  48.         ConstructorTest instance = constructor.newInstance(arrayList,  
  49.                 new String[] { "2""2" });  
  50.         List<String> list=instance.mList; //[1,2]  
  51.         String[] mParams=(String[]) instance.mParams;//[2,2]  
  52.           
  53.         //默认构造方法创建实例  
  54.         Constructor<ConstructorTest> defaultConstructor= ConstructorTest.class.getDeclaredConstructor();  
  55.         ConstructorTest instance2=defaultConstructor.newInstance();  
  56.           
  57.         //构造函数所在的类  
  58.         Class<ConstructorTest> declaringClass = constructor.getDeclaringClass();  
  59.         //如果此构造方法是一个复合构造方法,则返回 true  
  60.         boolean synthetic = constructor.isSynthetic(); //false  
  61.         //构造方法参数中是否包含可变参数  
  62.         boolean varArgs = constructor.isVarArgs(); //true  
  63.     }  
  64.   
  65.     private static void constructorInfo(Constructor constructor) {  
  66.         // 1、注解  
  67.         boolean annotationPresent = constructor  
  68.                 .isAnnotationPresent(MyAnnotation.class);  
  69.         if (annotationPresent) {  
  70.             MyAnnotation myAnnotation = constructor  
  71.                     .getDeclaredAnnotation(MyAnnotation.class);  
  72.         }  
  73.         Annotation[] declaredAnnotations = constructor.getDeclaredAnnotations();  
  74.   
  75.         // 2.修饰符  
  76.         int modifiers = constructor.getModifiers();  
  77.         String modify = Modifier.toString(modifiers);// public transient  
  78.   
  79.         // 3、定义在构造方法上的泛型  
  80.         TypeVariable[] typeParameters = constructor.getTypeParameters();  
  81.         System.out.println(Arrays.toString(typeParameters));  
  82.   
  83.         // 4、构造方法名  
  84.         String name = constructor.getName();// reflect.ConstructorTest  
  85.   
  86.         // 5、构造方法参数  
  87.         int parameterCount = constructor.getParameterCount();  
  88.         // 方法参数类型  
  89.         Class<?>[] parameterTypes = constructor.getParameterTypes();  
  90.         // 打印 [interface java.util.List, class [Ljava.lang.Object;]  
  91.         Type[] genericParameterTypes = constructor.getGenericParameterTypes();  
  92.         // 打印 [java.util.List<T>, T[]]  
  93.         for (Type type : genericParameterTypes) {  
  94.             if (type instanceof ParameterizedType) { // 参数类型  
  95.                 System.out.println("ParameterizedType类型:" + type);  
  96.                 ParameterizedType parameterizedType = (ParameterizedType) type;  
  97.                 Type[] actualTypeArguments = parameterizedType  
  98.                         .getActualTypeArguments();  
  99.                 System.out.println("实际参数为:"  
  100.                         + Arrays.toString(actualTypeArguments));  
  101.                 for (Type actualType : actualTypeArguments) {  
  102.                     if (actualType instanceof WildcardType) {  
  103.                         WildcardType wildcardType = (WildcardType) actualTypeArguments[0];  
  104.                         System.out.println("实际参数为WildcardType类型:"  
  105.                                 + wildcardType.getUpperBounds());  
  106.                     } else if (actualType instanceof Class) {  
  107.                         System.out.println("实际参数为Class类型:" + actualType);  
  108.                     }  
  109.                 }  
  110.   
  111.             } else if (type instanceof GenericArrayType) { // 泛型数组类型 T[]  
  112.                 GenericArrayType genericArrayType = (GenericArrayType) type;  
  113.                 System.out.println("GenericArrayType类型:"  
  114.                         + genericArrayType.getGenericComponentType());// T  
  115.             } else if (type instanceof TypeVariable) { // 泛型变量  
  116.                 System.out.println("TypeVariable类型:" + type);  
  117.             } else if (type instanceof Class) { //  
  118.                 System.out.println("Class类型:" + type);  
  119.             }  
  120.         }  
  121.         /* 
  122.          * 方法有2个参数,第一个参数list为ParameterizedType,实际参数为T, 
  123.          * 第二个参数为GenericArrayType泛型数组类型T[],数组元素类型为T 
  124.          */  
  125.   
  126.         // 方法参数——注解 第一个参数使用了注解  
  127.         Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();  
  128.         Annotation myAnnotation = parameterAnnotations[0][0];  
  129.         // 打印 @reflect.MyAnnotation(intValue=0)  
  130.           
  131.         // 6、方法抛出的异常  
  132.         Class<?>[] exceptionTypes = constructor.getExceptionTypes();  
  133.         // 打印 [class java.lang.RuntimeException, class java.lang.Exception]  
  134.         Type[] genericExceptionTypes = constructor.getGenericExceptionTypes();  
  135.         // 打印 [class java.lang.RuntimeException, class java.lang.Exception]  
  136.     }  
  137. }  
以上就是Constructor的所有信息,实际开发中,Constructor最重要的作用还是用来构造实例。
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值