什么是 com.sun.proxy.$Proxy [英] What is com.sun.proxy.$Proxy

查看:41
本文介绍了什么是 com.sun.proxy.$Proxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到,当错误发生在不同框架(例如实现 EJB 规范的框架或某些 JPA 提供程序)的深处时,堆栈跟踪包含类似 com.sun.proxy.$Proxy 的类.我知道代理是什么,但我正在寻找更具技术性和更具体的 Java 答案.

I have seen that when errors occur deep in different frameworks (e.g frameworks implementing the EJB specification or some JPA providers) the stacktrace contain classes like com.sun.proxy.$Proxy. I know what a Proxy is, but I am looking for a more technical and more java specific answer.

  1. 它们是什么?
  2. 它们是如何创建的?
  3. 与 JVM 有什么关系?它们是否特定于 JVM 实现?

推荐答案

  1. 代理是在运行时创建和加载的类.这些类没有源代码.我知道您想知道如果没有代码,如何让他们做某事.答案是,当你创建它们时,你指定了一个实现InvocationHandler的对象,它定义了一个在调用代理方法时调用的方法.

  1. Proxies are classes that are created and loaded at runtime. There is no source code for these classes. I know that you are wondering how you can make them do something if there is no code for them. The answer is that when you create them, you specify an object that implements InvocationHandler, which defines a method that is invoked when a proxy method is invoked.

您可以使用调用来创建它们

You create them by using the call

Proxy.newProxyInstance(classLoader, interfaces, invocationHandler)

参数是:

  1. classLoader.生成类后,将使用此类加载器加载它.
  2. 接口.必须都是接口的类对象数组.生成的代理实现了所有这些接口.
  3. invocationHandler.这就是您的代理在调用方法时知道要做什么的方式.它是一个实现 InvocationHandler 的对象.当调用来自任何受支持接口或 hashCodeequalstoString 的方法时,方法 invoke 在处理程序上调用,传递要调用的方法的 Method 对象和传递的参数.
  1. classLoader. Once the class is generated, it is loaded with this class loader.
  2. interfaces. An array of class objects that must all be interfaces. The resulting proxy implements all of these interfaces.
  3. invocationHandler. This is how your proxy knows what to do when a method is invoked. It is an object that implements InvocationHandler. When a method from any of the supported interfaces, or hashCode, equals, or toString, is invoked, the method invoke is invoked on the handler, passing the Method object for the method to be invoked and the arguments passed.

有关这方面的更多信息,请参阅 的文档代理类.

For more on this, see the documentation for the Proxy class.

1.3 版之后 JVM 的每个实现都必须支持这些.它们以特定于实现的方式加载到 JVM 的内部数据结构中,但保证可以正常工作.

Every implementation of a JVM after version 1.3 must support these. They are loaded into the internal data structures of the JVM in an implementation-specific way, but it is guaranteed to work.

这篇关于什么是 com.sun.proxy.$Proxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆