class getDeclaredMethods();
该方法返回当前类中声明的方法,包括public private protected,父类的方法不包括其中。
今天在使用该方法时碰到一个例外
实现类
输出结果:
main
onApplicationEvent
onApplicationEvent
会发现找到了两个onApplicationEvent方法,但是按道理来说Application内部只有一个方法,
这个时候需要查看一下编译生成的class文件的结果,
javap -c Application.class
可以发现生成的class文件确实有两个onApplicationEvent。
原因是因为public void onApplicationEvent(org.springframework.context.ApplicationEvent);
是桥接方法是由编译器生成。
想要区分可以通过isSynthetic和isBridge来区分
该方法返回当前类中声明的方法,包括public private protected,父类的方法不包括其中。
今天在使用该方法时碰到一个例外
接口
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E event);
}
实现类
public class Application<L> implements ApplicationListener<ContextRefreshedEvent> {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
for (Method method : Application.class.getDeclaredMethods()) {
System.out.println(method.getName());
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
}
}
运行代码输出结果:
main
onApplicationEvent
onApplicationEvent
会发现找到了两个onApplicationEvent方法,但是按道理来说Application内部只有一个方法,
这个时候需要查看一下编译生成的class文件的结果,
javap -c Application.class
Compiled from "Application.java"
public class com.spring.Application<L> implements org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent> {
public com.spring.Application();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException;
Code:
0: ldc #2 // class com/spring/Application
2: invokevirtual #3 // Method java/lang/Class.getDeclaredMethods:()[Ljava/lang/reflect/Method;
5: astore_1
6: aload_1
7: arraylength
8: istore_2
9: iconst_0
10: istore_3
11: iload_3
12: iload_2
13: if_icmpge 38
16: aload_1
17: iload_3
18: aaload
19: astore 4
21: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
24: aload 4
26: invokevirtual #5 // Method java/lang/reflect/Method.getName:()Ljava/lang/String;
29: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
32: iinc 3, 1
35: goto 11
38: return
public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent);
Code:
0: return
public void onApplicationEvent(org.springframework.context.ApplicationEvent);
Code:
0: aload_0
1: aload_1
2: checkcast #7 // class org/springframework/context/event/ContextRefreshedEvent
5: invokevirtual #8 // Method onApplicationEvent:(Lorg/springframework/context/event/ContextRefreshedEvent;)V
8: return
}
可以发现生成的class文件确实有两个onApplicationEvent。
原因是因为public void onApplicationEvent(org.springframework.context.ApplicationEvent);
是桥接方法是由编译器生成。
想要区分可以通过isSynthetic和isBridge来区分