一、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());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  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());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  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());
}
}
1
2
3
4
5
6
7
8
9
10
11
  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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  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));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  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()));
}
}
1
2
3
4
5
6
7
8
9
10
  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());
}
}
1
2
3
4
5
6
7
8
9
10
  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));
}
}
1
2
3
4
5
6
7
8
9
10
11
  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());
}
}
1
2
3
4
5
6
7
8
9
10
  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);
}
}
1
2
3
4
5
6
7
8
9
10
11
  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());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  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));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
  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());
}
}
1
2
3
4
5
6
7
8
9
10
  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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  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));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  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(genericReturnType);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  23、getExceptionTypes()

返回一个Class对象数组,该数组表示由该方法对象抛出的异常对象,会擦除泛型
public class MethodTest<T> {

public <T extends Exception> void test() throws T, NullPointerException {}

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
Class<?>[] exceptionTypes = method.getExceptionTypes();
// [class java.lang.Exception, class java.lang.NullPointerException]
System.out.println(Arrays.toString(exceptionTypes));
}
}
1
2
3
4
5
6
7
8
9
10
11
  24、getGenericExceptionTypes()

返回一个Type对象数组,该数组表示由该方法对象抛出的异常类型,会保留泛型
public class MethodTest<T> {

public <T extends Exception> void test() throws T, NullPointerException {}

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
Type[] genericExceptionTypes = method.getGenericExceptionTypes();
// [T, class java.lang.NullPointerException]
System.out.println(Arrays.toString(genericExceptionTypes));
}
}
1
2
3
4
5
6
7
8
9
10
11
  25、getTypeParameters()

返回一个TypeVariable对象数组,该数组表示该方法对象声明列表上的类型变量数组
public class MethodTest<T, V> {

public <T, V> void test() {}

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
TypeVariable<Method>[] typeParameters = method.getTypeParameters();
// [T, V]
System.out.println(Arrays.toString(typeParameters));
}
}
1
2
3
4
5
6
7
8
9
10
11
  26、toString()

返回该方法对象的字符串表示形式,会擦除泛型
public class MethodTest<T, V> {

public <T, V> void test() {}

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
// public void lang.reflect.MethodTest.test()
System.out.println(method.toString());
}
}
1
2
3
4
5
6
7
8
9
10
  27、toGenericString()

返回该方法对象的字符串表示形式,会保留泛型
public class MethodTest<T, V> {

public <T, V> void test() {}

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
// public <T,V> void lang.reflect.MethodTest.test()
System.out.println(method.toGenericString());
}
}
1
2
3
4
5
6
7
8
9
10
  28、isAccessible()

获取该方法对象的可访问标志
public class MethodTest {

private void test() {}
}

public class Test {

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
// false
System.out.println(method.isAccessible());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  29、setAccessible(boolean flag)

设置该方法对象的可访问标志
在其他类里调用该方法对象时,如果该方法为私有方法,需要设置访问标志为true,否则会报异常
public class MethodTest {

private void test() {}
}

public class Test {

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getDeclaredMethod("test");
method.setAccessible(true);
// test
System.out.println(method.getName());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  30、isDefault()

判断该方法对象是否为默认方法,如果是则返回true,否则为false
public interface Interface {

default void test() {
System.out.println("这是一个默认方法");
}
}

public class MethodTest implements Interface {

public static void main(String[] args) throws Exception {
Method method = MethodTest.class.getMethod("test");
// true
System.out.println(method.isDefault());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  31、isSynthetic()

判断该方法对象是否为合成方法,如果是则返回true,否则为false
在内部类InnerClass中,name是一个私有属性,而我们在外部类MethodTest中,直接引用了这个属性,因此编译器通过生成一个合成方法,用于绕开private私有属性的限制
public class MethodTest {

private class InnerClass {
private String name = "小明";
}

public static void main(final String[] arguments) {
InnerClass innerClass = new MethodTest().new InnerClass();
System.out.println("name: " + innerClass.name);

Method[] declaredMethods = innerClass.getClass().getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
// 【static java.lang.String lang.reflect.MethodTest$InnerClass.access$100(lang.reflect.MethodTest$InnerClass)】 isSynthetic(): true
System.out.println("【" + declaredMethod + "】" + " isSynthetic(): " + declaredMethod.isSynthetic());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
有关synthetic的相关内容,小伙伴可以看下这里
Java 中冷门的 synthetic 关键字原理解读
  32、isBridge()

判断该方法对象是否桥接方法,如果是则返回true,否则为false
桥接方法: 是 JDK1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法
public interface Interface<T> {

T test(T t);
}

public class MethodTest implements Interface<String> {

@Override
public String test(String str) {
return str;
}

public static void main(final String[] arguments) {
Method[] declaredMethods = MethodTest.class.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
//【public static void lang.reflect.MethodTest.main(java.lang.String[])】 isBridge(): false
//【public java.lang.String lang.reflect.MethodTest.test(java.lang.String)】 isBridge(): false
//【public java.lang.Object lang.reflect.MethodTest.test(java.lang.Object)】 isBridge(): true
System.out.println("【" + declaredMethod + "】" + " isBridge(): " + declaredMethod.isBridge());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Java-Method类常用方法详解的更多相关文章

  1. java Object类源代码详解 及native (转自 http://blog.csdn.net/sjw890821sjw/article/details/8058843)

    package java.lang; public class Object { /* 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用.*/ private static na ...

  2. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  3. Java-Modifier类常用方法详解

    一.Modifier类的定义 Modifier类 (修饰符工具类) 位于 java.lang.reflect 包中,用于判断和获取某个类.变量或方法的修饰符Modifier类将各个修饰符表示为相对应的 ...

  4. Java 嵌套类基础详解

    目录 1. 什么是嵌套类? 2. 为什么要使用嵌套类? 3. 嵌套类的类型 4. 静态嵌套类 5. 非静态嵌套类 5.1 成员内部类 5.2 局部内部类 5.3 匿名内部类 6. 嵌套接口 1. 什么 ...

  5. Java常用类StringBuffer详解

    内容多为最近学习的自我总结,可能有些地方写的不严谨,甚至会有错误的地方,仅供参考,如发现错误敬请指出,谢谢! 灰色字体为补充扩展内容,多为帮助自己理解. StringBuffer概述: 线程安全的可变 ...

  6. Java常用类object详解

    1.Object概述: 类Object是类层次结构的根类.每个类都使用Object作为超类.所有对象(包括数组)都实现这个类的方法. 2.构造方法详细信息: Object只有一个无参构造方法,因为ob ...

  7. StringUtils工具类常用方法详解

    StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...

  8. Java的类的详解

    首先呢,我承认上一次我理解的有误. 1.构造方法的作用:是初始化一个对象,而不是成员变量,它和get和set方法都有给成员变量赋值的功能. 2.下来说一下JVM调用main方法的过程: a.静态变量赋 ...

  9. 11-02 Java Object类使用详解

     Object 作为超类 Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类. Object类的构造方法有一个,并且是无参构造,这其实就是理解当时我们说过,子类构造方法默认访 ...

随机推荐

  1. 通过JS生成由字母与数字组合的随机字符串

    在项目中可能需要随机生成字母数字组成的字符,如生成3-32位长度的字母数字组合的随机字符串(位数不固定)或者生成43位随机字符串(位数固定) 使用Math.random()与toString()方法的 ...

  2. git 入门教程之里程碑式标签

    "春风得意马蹄疾,一日看尽长安花",对于项目也是如此,最值得期待的恐怕就要数新版本发布的时刻了吧?每当发布新版本时要么是版本号命名(比如v0.0.1)或者代号命名(比如Chelse ...

  3. 产品经理说| AIOps 让告警变得更智能 (下)

    AIOps 人工智能和IT运营支撑 Ops 之间的故事,愈演愈烈,已经成为当今运维圈的热门话题,我打算从2篇文档分享我们在 AIOps 上一些探索和实践.(上篇)主要介绍了为什么事件(告警)处理需要 ...

  4. 老K漫谈区块链的共识(1)——免信任的共识机制

    老k,柏链道捷CTO.清华阿尔山区块链研究中心高级工程师,超过17年的系统软件开发经验,在操作系统.编译器.虚拟机和符号执行方面都有实战经验.主持开发多个开眼项目,目前主要从事区块链底层系统开发工作. ...

  5. 【转载】Python中的垃圾回收机制

    GC作为现代编程语言的自动内存管理机制,专注于两件事:1. 找到内存中无用的垃圾资源 2. 清除这些垃圾并把内存让出来给其他对象使用.GC彻底把程序员从资源管理的重担中解放出来,让他们有更多的时间放在 ...

  6. JS一些实用的方法

    1.首次为变量赋值时务必使用var关键字 变量没有声明而直接赋值得话,默认会作为一个新的全局变量,要尽量避免使用全局变量. 2.使用===取代== ==和!=操作符会在需要的情况下自动转换数据类型.但 ...

  7. Linux 小知识翻译 - 「LDAP」

    这次聊聊「LDAP」. LDAP是「Lightweight Directory Access Protocol」的所有,从名字上可以看出是协议的一种. LDAP是访问数据库(层次型数据库)的组件.管理 ...

  8. vonic单页面应用

    Vonic—基于Vue.js和ionic样式的移动端UI框架 先放上源码和demo地址: 标签演示:  https://wangdahoo.github.io/vonic/docs/       源码 ...

  9. Fetch请求后台的数据

    <style> #btn{ width: 50px; height: 50px; background-color: red; } #output{ width: 100px; heigh ...

  10. 【转】Vue组件一-父组件传值给子组件

    Vue组件一-父组件传值给子组件 开始 Vue组件是学习Vue框架最比较难的部分,而这部分难点我认为可以分为三个部分学习,即 组件的传值 - 父组件向子组件中传值 事件回馈 - 子组件向父组件发送消息 ...