Java-Method類常用方法詳解
阿新 • • 發佈:2018-12-22
這次主要整理下Java中Method類的常用方法
一、Method類的定義
- Method類位於 java.lang.reflect 包中,主要用於在程式執行狀態中,動態地獲取方法資訊
二、Method類的常用方法
1、getAnnotatedReturnType()
- 返回一個AnnotatedType物件,該物件表示使用一個型別來指定由該可執行檔案表示的方法/建構函式的返回型別
public class MethodTest {
public String test() {
return null;
}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
AnnotatedType methodAnnotatedReturnType = method.getAnnotatedReturnType();
// class java.lang.String
System.out.println(methodAnnotatedReturnType.getType());
}
}
2、getAnnotatedExceptionTypes()
- 返回一個AnnotatedType物件陣列,這些物件表示使用型別來指定由該可執行檔案表示的方法/建構函式宣告的異常
public class MethodTest {
public void test() throws NullPointerException, ClassNotFoundException {}
public static void main(String[] args) throws Exception {
Method method = MethodTest. class.getDeclaredMethod("test");
AnnotatedType[] annotatedExceptionTypes = method.getAnnotatedExceptionTypes();
for (AnnotatedType annotatedExceptionType : annotatedExceptionTypes) {
// class java.lang.NullPointerException
// class java.lang.ClassNotFoundException
System.out.println(annotatedExceptionType.getType());
}
}
}
3、getAnnotatedReceiverType()
- 返回一個AnnotatedType物件,該物件表示使用一個型別來指定該可執行物件表示的方法/建構函式的接收者型別
public class MethodTest {
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
AnnotatedType annotatedReceiverType = method.getAnnotatedReceiverType();
// class lang.reflect.MethodTest
System.out.println(annotatedReceiverType.getType());
}
}
4、getAnnotation(Class<T> annotationClass)
- 如果該方法物件存在指定型別的註解,則返回該註解,否則返回null
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotation() 方法與 getDeclaredAnnotation() 方法作用相同
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
String key();
String value();
}
public class MethodTest {
@MethodAnnotation(key = "key", value = "value")
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
MethodAnnotation annotation = method.getAnnotation(MethodAnnotation.class);
// @lang.reflect.MethodAnnotation(value=value, key=key)
System.out.println(annotation);
}
}
5、getDeclaredAnnotation(Class<T> annotationClass)
- 如果該方法物件存在指定型別的註解,則返回該註解,否則返回null
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotation() 方法與 getDeclaredAnnotation() 方法作用相同
6、getAnnotationsByType(Class<T> annotationClass)
- 如果該方法物件存在指定型別的註解,則返回該註解陣列,否則返回null
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotationsByType() 方法與 getDeclaredAnnotationsByType() 方法作用相同
- getAnnotationsByType() 方法與 getAnnotation() 方法的區別在於 getAnnotationsByType() 方法會檢查修飾該方法物件的註解是否為可重複型別註解,如果是則會返回該引數型別的一個或多個註解
- @Repeatable 用於宣告註解為可重複型別註解,當宣告為可重複型別註解後,如果方法註解仍為一個,則 getAnnotation() 方法會正常返回,如果方法註解為多個,則 getAnnotation()方法會返回null
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(RepeatableAnnotation.class)
public @interface MethodAnnotation {
String key();
String value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface RepeatableAnnotation {
MethodAnnotation[] value();
}
public class MethodTest {
@MethodAnnotation(key = "key1", value = "value1")
@MethodAnnotation(key = "key2", value = "value2")
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
MethodAnnotation[] annotationsByType = method.getAnnotationsByType(MethodAnnotation.class);
// [@lang.reflect.MethodAnnotation(value=value1, key=key1), @lang.reflect.MethodAnnotation(value=value2, key=key2)]
System.out.println(Arrays.toString(annotationsByType));
}
}
7、getDeclaredAnnotationsByType(Class<T> annotationClass)
- 如果該方法物件存在指定型別的註解,則返回該註解陣列,否則返回null
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotationsByType() 方法與 getDeclaredAnnotationsByType() 方法作用相同
8、getAnnotations()
- 返回該方法物件上的所有註解,如果沒有註解,則返回空陣列
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotations() 方法與 getDeclaredAnnotations() 方法作用相同
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
String key();
String value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String key();
String value();
}
public class MethodTest {
@MethodAnnotation(key = "key1", value = "value1")
@TestAnnotation(key = "key2", value = "value2")
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
// @lang.reflect.MethodAnnotation(value=value1, key=key1)
// @lang.reflect.Parameter.TestAnnotation(key=key2, value=value2)
System.out.println(annotation);
}
}
}
9、getDeclaredAnnotations()
- 返回該方法物件上的所有註解,如果沒有註解,則返回空陣列
- 只有類級別的註解會被繼承得到,對於其他物件而言,getAnnotations() 方法與 getDeclaredAnnotations() 方法作用相同
10、getModifiers()
- 返回修飾該方法物件修飾符的整數形式,使用 Modifier 類對其進行解碼
public class MethodTest {
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
// public
System.out.println(Modifier.toString(method.getModifiers()));
}
}
11、getName()
- 返回方法物件名稱
public class MethodTest {
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
// test
System.out.println(method.getName());
}
}
12、isAnnotationPresent(Class<? extends Annotation> annotationClass)
- 如果該方法物件上有指定型別的註解,則返回true,否則為false
public class MethodTest {
@MethodAnnotation(key = "key", value = "value")
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
// true
System.out.println(method.isAnnotationPresent(MethodAnnotation.class));
}
}
13、isVarArgs()
- 如果該方法物件的引數中存在 可變參,則返回true,否則為false
public class MethodTest {
public void test(String ... args) {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test", String[].class);
// true
System.out.println(method.isVarArgs());
}
}
14、getDeclaringClass ()
- 返回該方法物件表示的方法所在類的Class物件
public class MethodTest {
public void test() {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
Class<?> declaringClass = method.getDeclaringClass();
// class lang.reflect.MethodTest
System.out.println(declaringClass);
}
}
15、getAnnotatedParameterTypes()
- 返回一個AnnotatedType物件陣列,這些物件表示使用型別來指定由該可執行檔案表示的方法/建構函式的形式引數型別
public class MethodTest {
public void test(String name, Integer age) {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test", String.class, Integer.class);
AnnotatedType[] annotatedParameterTypes = method.getAnnotatedParameterTypes();
for (AnnotatedType annotatedParameterType : annotatedParameterTypes) {
// class java.lang.String
// class java.lang.Integer
System.out.println(annotatedParameterType.getType());
}
}
}
16、getParameterAnnotations()
- 返回一組註解陣列,這些註解以宣告順序修飾該方法物件的引數
public class MethodTest {
public void test(@ParameterAnnotation(key = "key1", value = "value1") String name,
@ParameterAnnotation(key = "key2", value = "value2") Integer age) {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test", String.class, Integer.class);
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
// [[@lang.reflect.ParameterAnnotation(key=key1, value=value1)], [@lang.reflect.ParameterAnnotation(key=key2, value=value2)]]
System.out.println(Arrays.deepToString(parameterAnnotations));
}
}
17、getParameterCount()
- 返回該方法物件的引數個數 (無論是顯式宣告的還是隱式宣告的或不宣告的)
public class MethodTest {
public void test(String name, Integer age) {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test", String.class, Integer.class);
// 2
System.out.println(method.getParameterCount());
}
}
18、getParameters()
- 返回一個引數物件陣列,該陣列表示該方法物件的所有引數
public class MethodTest {
public void test(String name, Integer age) {}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test", String.class, Integer.class);
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
// java.lang.String name
// java.lang.Integer age
System.out.println(parameter);
}
}
}
19、getDefaultValue()
- 返會該註解方法物件表示的成員預設值
- 如果成員屬於基本資料型別,則返回對應的包裝類例項
- 如果沒有預設值或者該方法例項不表示註解方法,則返回null
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
String key() default "default key";
String value() default "default value";
}
public class MethodTest {
public static void main(String[] args) throws Exception {
Method key = MethodAnnotation.class.getMethod("key");
Method value = MethodAnnotation.class.getMethod("value");
Object defaultValue1 = key.getDefaultValue();
Object defaultValue2 = value.getDefaultValue();
// default key
System.out.println(defaultValue1);
// default value
System.out.println(defaultValue2);
}
}
20、getParameterTypes()
- 返回一個Class物件陣列,該陣列以宣告順序表示該方法物件的引數型別,會擦除泛型
public class MethodTest<T> {
public void test(T t) {}
public void test(LinkedList<Integer> list) {}
public static void main(String[] args) throws Exception {
Method objectMethod = MethodTest.class.getMethod("test", Object.class);
Method listMethod = MethodTest.class.getMethod("test", LinkedList.class);
Class<?>[] objectParameterTypes = objectMethod.getParameterTypes();
// [class java.lang.Object]
System.out.println(Arrays.toString(objectParameterTypes));
Class<?>[] listParameterTypes = listMethod.getParameterTypes();
// [class java.util.LinkedList]
System.out.println(Arrays.toString(listParameterTypes));
}
}
21、getReturnType()
- 返回一個Class物件,該Class物件表示該方法物件的返回型別,會擦除泛型
public class MethodTest<T> {
public T test(T t) {
return t;
}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test", Object.class);
Class<?> returnType = method.getReturnType();
// class java.lang.Object
System.out.println(returnType);
}
}
22、getGenericReturnType()
- 返回一個Type物件,該Type物件表示該方法物件的返回型別,會保留泛型
public class MethodTest<T> {
public T test(T t) {
return t;
}
public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test", Object.class);
Type genericReturnType = method.getGenericReturnType();
// T
System.out.println