Class -- 10 -- Method类常用方法解析
原文链接:Class – 10 – Method类常用方法解析
相关文章:
Class – 01 – System类常用方法解析
Class – 02 – Arrays类常用方法解析
Class – 03 – Random类常用方法详解析
Class – 04 – Date类常用方法解析
Class – 05 – TimeUnit类常用方法解析
Class – 06 – TimeZone类常用方法详解析
Class – 07 – Modifier类常用方法解析
Class – 08 – Parameter类常用方法解析
Class – 09 – Field类常用方法解析
Class – 10 – Method类常用方法解析
Class – 11 – Math类常用方法解析
Class – 12 – Locale类常用方法解析
Class – 13 – ThreadPoolExecutor类常用方法解析
这次主要整理下 Java 中 Method 类的常用方法
一、Method 类的定义
- Method 类位于 java.lang.reflect 包中,主要用于在程序运行状态中,动态地获取方法信息
二、Method 类常用方法
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 annotatedReturnType = method.getAnnotatedReturnType();// class java.lang.StringSystem.out.println(annotatedReturnType.getType());} }
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.ClassNotFoundExceptionSystem.out.println(annotatedExceptionType.getType());}} }
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.MethodTestSystem.out.println(annotatedReceiverType.getType());} }
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);} }
getDeclaredAnnotation(Class<T> annotationClass)
如果该方法对象存在指定类型的注解,则返回该注解,否则返回 null
只有类级别的注解会被继承得到,对于其他对象而言,getAnnotation() 方法与 getDeclaredAnnotation() 方法作用相同
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");// nullSystem.out.println(method.getAnnotation(MethodAnnotation.class));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));} }
getDeclaredAnnotationsByType(Class<T> annotationClass)
如果该方法对象存在指定类型的注解,则返回该注解数组,否则返回 null
只有类级别的注解会被继承得到,对于其他对象而言,getAnnotationsByType() 方法与 getDeclaredAnnotationsByType() 方法作用相同
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);}} }
getDeclaredAnnotations()
返回该方法对象上的所有注解,如果没有注解,则返回空数组
只有类级别的注解会被继承得到,对于其他对象而言,getAnnotations() 方法与 getDeclaredAnnotations() 方法作用相同
getModifiers()
返回修饰该方法对象修饰符的整数形式,使用 Modifier 类对其进行解码
public class MethodTest {public void test() {}public static void main(String[] args) throws Exception {Method method = MethodTest.class.getDeclaredMethod("test");// publicSystem.out.println(Modifier.toString(method.getModifiers()));} }
getName()
返回方法对象名称
public class MethodTest {public void test() {}public static void main(String[] args) throws Exception {Method method = MethodTest.class.getDeclaredMethod("test");// testSystem.out.println(method.getName());} }
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");// trueSystem.out.println(method.isAnnotationPresent(MethodAnnotation.class));} }
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);// trueSystem.out.println(method.isVarArgs());} }
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.MethodTestSystem.out.println(declaringClass);} }
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.IntegerSystem.out.println(annotatedParameterType.getType());}} }
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));} }
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);// 2System.out.println(method.getParameterCount());} }
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 ageSystem.out.println(parameter);}} }
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 keySystem.out.println(defaultValue1);// default valueSystem.out.println(defaultValue2);} }
getParameterTypes()
返回一个 Class 对象数组,该数组以声明顺序表示该方法对象的参数对象 (擦除泛型)
public class MethodTest<T> {public void test(T t, LinkedList<Integer> list) {}public static void main(String[] args) throws Exception {Method method = MethodTest.class.getMethod("test", Object.class, LinkedList.class);Class<?>[] parameterTypes = method.getParameterTypes();// [class java.lang.Object, class java.util.LinkedList]System.out.println(Arrays.toString(parameterTypes));} }
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.ObjectSystem.out.println(returnType);} }
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();// TSystem.out.println(genericReturnType);} }
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));} }
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));} }
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));} }
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());} }
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());} }
isAccessible()
获取该方法对象的可访问标志
public class MethodTest {private void test() {} }public class Test {public static void main(String[] args) throws Exception {Method method = MethodTest.class.getDeclaredMethod("test");// falseSystem.out.println(method.isAccessible());} }
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);// testSystem.out.println(method.getName());} }
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");// trueSystem.out.println(method.isDefault());} }
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();// name: 小明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(): trueSystem.out.println("【" + declaredMethod + "】" + " isSynthetic(): " + declaredMethod.isSynthetic());}} }
有关 synthetic 的相关内容,小伙伴可以看下这里
- Java 中冷门的 synthetic 关键字原理解读
isBridge()
判断该方法对象是否桥接方法,如果是则返回 true,否则为 false
桥接方法: 是 JDK1.5 引入泛型后,为了使 Java 的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法
public interface Interface<T> {T test(T t); }public class MethodTest implements Interface<String> {@Overridepublic 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(): trueSystem.out.println("【" + declaredMethod + "】" + " isBridge(): " + declaredMethod.isBridge());}} }
有关 bridge 的相关内容,小伙伴可以看下这里
- java中什么是bridge method(桥接方法)
Class -- 10 -- Method类常用方法解析相关推荐
- Class -- 08 -- Parameter类常用方法解析
原文链接:Class – 08 – Parameter类常用方法解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class ...
- Class -- 09 -- Field类常用方法解析
原文链接:Class – 09 – Field类常用方法解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class – 0 ...
- Class -- 03 -- Random类常用方法详解析
原文链接:Class – 03 – Random类常用方法详解析 相关文章: Class – 01 – System类常用方法解析 Class – 02 – Arrays类常用方法解析 Class – ...
- Method类的使用
概述 每个方法都由修饰符.返回值.参数.注解和抛出的异常组成.而java.lang.reflect.Method类提供了获取上述内容的API. 需要注意的是,反射一个类的方法时不会考虑父类的方法,只会 ...
- java method field_java_解析Java中的Field类和Method类,Field类
Field类中定义了一些方 - phpStudy...
解析Java中的Field类和Method类 Field类Field类中定义了一些方法,可以用来查询字段的类型以及设置或读取字段的值.将这些方法与继承而来的member方法结合在一起.就可以使我们能够 ...
- method java_解析Java中的Field类和Method类
Field类Field类中定义了一些方法,可以用来查询字段的类型以及设置或读取字段的值.将这些方法与继承而来的member方法结合在一起.就可以使我们能够找出有关字段声明的全部信息,并且能够操纵某个特 ...
- C# 语言规范_版本5.0 (第10章 类)
1. 类 类是一种数据结构,它可以包含数据成员(常量和字段).函数成员(方法.属性.事件.索引器.运算符.实例构造函数.静态构造函数和析构函数)以及嵌套类型.类类型支持继承,继承是一种机制,它使派生类 ...
- es6删除数组某一项_javascript基础系列:数组常用方法解析
javascript基础系列:数组常用方法解析 今天是比较特殊的日子,我们编程人员共同的节日,1024,祝每个编程人员节日快乐! 数组是javascript必不可少的一项,今天让我们来总结一下数组操作 ...
- javascript基础系列:数组常用方法解析
javascript基础系列:数组常用方法解析 今天是比较特殊的日子,我们编程人员共同的节日,1024,祝每个编程人员节日快乐! 数组是javascript必不可少的一项,今天让我们来总结一下数组操作 ...
最新文章
- 20145227鄢曼君《网络对抗》逆向及Bof基础
- ARM开发步步深入之NandFlash 4KB突围
- Python之进程+线程+协程(进程间通信、进程同步、进程池、回调函数)
- Android setOnPageChangeListener 过时解决
- mac使用之必备神器
- 威纶触摸屏EB8000编程软件V4.65.14 官方最新版
- Windows快速更改IP脚本
- 一文读懂华为智能网联汽车产业链布局
- n6 tenda 固件_腾达 Tenda N6 刷 TTDW 说明
- 外贸网站 | 在NameCheap或NameSilo购买网站域名
- 删除的文件怎么恢复?
- C语言 Sn=a+aa+aaa+……之值,其中a是一个数字,n表示a的位数,n由键盘输入。
- 岁末精选:2005国外经典语录
- 服务器接上显示器操作,服务器接上显示器
- linux装软件需要root用户,Linux下非root用户安装软件的一般流程:
- AcWing120防线 经典题二分+前缀和+等差数列
- R语言实战笔记--第十五章 处理缺失数据
- 【ROS学习】- tf学习 - tf中重要函数解析 (陆续更新....)
- 猎头职场:真正城府深的人都不会做这些
- 2020年蓝桥杯暑假第1次练习赛(C++组)
热门文章
- 什么是“SCSI”硬盘
- python类的使用的生物学应用_python类的使用的生物学应用_Python 类的使用
- hdu5172 GTY‘s gay friends(hash判断排列)
- EMP微前端-Vue和React项目互相调用
- Attend and Rectify: a Gated Attention Mechanism for Fine-Grained Recovery
- 国内外遥感卫星整理汇总
- 十年,又回到原点,也许是个新的起点
- 来 给朕手写一个OOM异常的栗子
- Ubuntu18.04开机自动挂载Nas硬盘
- 测试开发工作者日记:2020.6.28