反射類 Method類的使用
在Java反射中,可以使用Method類獲取類,參數類型,方法注解,參數注解,方法返回值等信息,在使用Method類中,常會用到以下的方法。如下表所示。
方法名 | 作用 |
getName() |
獲取方法名 |
isVarArgs() | 如果該方法聲明為采用可變數量的參數,則返回true; 否則返回false |
getModifiers() | 獲取權限修飾符 |
getReturnType() | 獲取返回類型 |
getExceptionTypes() | 獲取所有拋出的異常類型 |
getGenericReturnType | 返回Type類型 |
getParameterTypes() | 獲取所有參數的類型 |
getParameterCount() | 獲取所有參數的個數 |
getAnnotations() | 獲取方法級別的注解 |
getDeclaringClass | 獲取方法所在的類信息 |
使用如下的示例說明Method類的使用,下面代碼段定義了兩個參數級別的注解,在MethodService類中給定了一個login()方法,三個入參,其中兩個參數使用注解進行標注。觀察main()方法的結果。
1 package com.zzz.mybatis.reflect; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8 9 @Documented 10 @Retention(RetentionPolicy.RUNTIME) 11 @Target(ElementType.PARAMETER) 12 public @interface Name { 13 //默認為空 14 String name() default "lisi"; 15 }
1 package com.zzz.mybatis.reflect; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 @Retention(RetentionPolicy.RUNTIME) 8 @Target(ElementType.PARAMETER) 9 public @interface Password { 10 //默認為空 11 String password() default ""; 12 }
1 package com.zzz.mybatis.reflect; 2 3 import java.lang.annotation.Annotation; 4 import java.lang.reflect.AnnotatedType; 5 import java.lang.reflect.Method; 6 import java.lang.reflect.Parameter; 7 8 public class MethodService { 9 @Deprecated 10 public void login(@Name(name="zhangsan") String name,@Password(password="123") String pwd,int age) throws NullPointerException,IllegalArgumentException { 11 12 } 13 14 public static void getAnnotations(Method method) { 15 String name="",pwd=""; 16 Annotation[][] ans=method.getParameterAnnotations(); 17 int anslen=ans.length; 18 for (int paramIndex = 0; paramIndex < anslen; paramIndex++) { 19 for (Annotation annotation : ans[paramIndex]) { 20 //判斷是否是Param標簽的子類,也就是說@param中是否存在value值 21 if (annotation instanceof Name) { 22 name = ((Name) annotation).name(); 23 break; 24 } 25 if (annotation instanceof Password) { 26 pwd=((Password)annotation).password(); 27 break; 28 } 29 } 30 } 31 System.out.println("name"+name+"\t"+"pwd"+pwd); 32 } 33 34 public static void getActualParameter(Method method) { 35 System.out.println("獲取參數個數"+method.getParameterCount()); 36 Class<?>[] parameterTypes= method.getParameterTypes(); 37 System.out.println("獲取所有參數類型"); 38 for(Class<?> type:parameterTypes) { 39 System.out.println(type.getName()); 40 } 41 System.out.println("獲取所有注解"); 42 AnnotatedType[] ans=method.getAnnotatedParameterTypes(); 43 for(AnnotatedType annotation:ans) { 44 System.out.println(annotation.getClass().getName()); 45 } 46 System.out.println("獲取完整參數信息"); 47 Parameter[] parameters=method.getParameters(); 48 for(Parameter parameter:parameters) { 49 System.out.println("參數修飾符:"+parameter.getModifiers()+"參數名:"+parameter.getName()+"參數類型:"+parameter.getType().getName()); 50 } 51 } 52 53 54 public static void getException(Method method) { 55 Class<?>[] exs=method.getExceptionTypes(); 56 for(Class<?> e:exs) { 57 System.out.println(e.getName()); 58 } 59 } 60 61 public static void getMethodAnotation(Method method) { 62 Annotation[] annotations=method.getAnnotations(); 63 for(Annotation annotation:annotations) { 64 System.out.println(annotation.annotationType().getName()); 65 } 66 67 } 68 69 public static void main(String[] args) throws ClassNotFoundException { 70 Class<?> c=Class.forName("com.zzz.mybatis.reflect.MethodService"); 71 Method[] methods= c.getMethods(); 72 for(Method method:methods) { 73 Class<?>[] paramTypes=method.getParameterTypes(); 74 //獲取方法名 75 if(method.getName().contains("login")) { 76 //獲取方法所在的類 com.zzz.mybatis.reflect.MethodService 77 System.out.println("方法所在的類信息:"+method.getDeclaringClass().getName()); 78 //獲取方法返回的類型 79 System.out.println("返回類型:"+method.getReturnType().getName()); 80 //跟getReturnType()類型,不過返回的是一個Type類型 81 System.out.println("返回Type類型:"+method.getGenericReturnType().getTypeName()); 82 //參數相關 83 getActualParameter(method); 84 //獲取參數級別的注解信息 85 getAnnotations(method); 86 //獲取拋出的異常信息 87 getException(method); 88 //獲取方法級別的注解信息 89 getMethodAnotation(method); 90 } 91 } 92 93 } 94 }
1 方法所在的類信息:com.zzz.mybatis.reflect.MethodService 2 返回類型:void 3 返回Type類型:void 4 獲取參數個數3 5 獲取所有參數類型 6 java.lang.String 7 java.lang.String 8 int 9 獲取所有注解 10 sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl 11 sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl 12 sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl 13 獲取完整參數信息 14 參數修飾符:0參數名:arg0參數類型:java.lang.String 15 參數修飾符:0參數名:arg1參數類型:java.lang.String 16 參數修飾符:0參數名:arg2參數類型:int 17 namezhangsan pwd123 18 java.lang.NullPointerException 19 java.lang.IllegalArgumentException 20 java.lang.Deprecated