Dynamic proxy exception Com.sun.proxy. $Proxy 0 cannot is cast to

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.