When I was learning dynamic agents, I did some exercises, and the results reported Exception in thread "main" Java.lang.ClassCastException:com.sun.proxy. $Proxy 0 cannot to be cast to exception;
In close contrast with the examples written by other people on the Internet, we found the original
After the change came back to run successfully.
Deliberately checked on the internet, found that their knowledge is still relatively weak, but also manually tested some of their own conjecture, as follows
Work.class = = new Work (). GetClass () is equal, so the same can be done: Work.class.getClassLoader () = = new Work (). GetClass (). getClassLoader () also equal
However, there is a hole in my picture written Work Work = new Hello ();
Here System.out.println (Work.getClass.getName (), the output is "Hello";
The type of the instance that was previously thought to be the parent class to the subclass or "strong turn" will change, but the test found that class does not change, so I guess the type is unchanged.
I think the new Work () GetClass () is the class object of Work, but it is the class object of the subclass hello.
back to the topic : This time the main reason for the exception is not the first parameter of the method problem, but the second argument!
The main function of Class.getinterfaces () is to get all interfaces of the interface implemented by a class, return the value class<?>[], and I ... Haha unexpectedly is to get interface interface, so the error, interested friends can traverse the output of all interface class.
In addition, I also found a friend on the internet to find the same exception, but his exception is: The proxy class does not implement the interface, but the implementation of a base class. Everybody should pay attention to OH.