forked from JavaDevTeam/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-reflect-Method.java
More file actions
51 lines (39 loc) · 1.38 KB
/
java-reflect-Method.java
File metadata and controls
51 lines (39 loc) · 1.38 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---------------------
java.reflect.Method |
---------------------
---------------------
实例方法 |
---------------------
invoke(Object obj,Object value...);
* 以反射的方法执行方法对象,第一个参数表示是哪个对象的此方法,第二个参数表示方法参数,如果没有直接无视即可
<T extends Annotation> getAnnotation(Class<T> annotationClass)
* 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
Annotation[] getDeclaredAnnotations();
* 获取该方法所有的注解
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
* 判断是否有指定类型的注解标识在该方法
int getModifiers()
* 返回权限修饰的表示数值(public,private,native,final....)
Class<?> getReturnType()
* 返回 return 的Class类类型
boolean isBridge()
* 是否是泛型桥接方法
* 桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容
* 由编译器自动生成的方法
interface Parent<T> {
void foo(T t);
}
class Sub implements Parent<String> {
@Override
public void foo(String s) {
System.out.println(s);
}
// JVM编译器生成的桥接方法
// public void foo(Object s) {
// this.foo((String) s);
// }
}
Method bridgeMethod = Sub.class.getMethod("foo",Object.class);
System.out.println(bridgeMethod.isBridge()); // true
Method method = Sub.class.getMethod("foo",String.class);
System.out.println(method.isBridge()); // false