插入 Java 类方法(无接口) [英] Interposing on Java Class Methods (without interfaces)

查看:25
本文介绍了插入 Java 类方法(无接口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在类方法之间插入以动态扩展对象.

I'd like to interpose between class methods to dynamically extends an object.

我已经知道 java.lang.reflect.Proxy 的东西,但它太有限了,无法进行真正的插入.

I already know about the java.lang.reflect.Proxy stuff, but it's way too limited to do real interposing.

来自 使用 java.lang.reflect.Proxy 来插入 Java 类方法,第一个限制是:

From Using java.lang.reflect.Proxy to Interpose on Java Class Methods, the first limitation is :

(...) 该方法必须通过代理类的实例调用.因此,例如嵌套方法调用将不会被拦截.

(...) the method must be called through an instance of the proxy class. So nested methods calls, for instance, would not be intercepted.

最糟糕的一个:

(...) 该方法必须在被代理的对象实现的接口中定义.不能通过没有实现接口的类的实例调用.

(...) the method must have been defined in an Interface that is implemented by the object being proxied. It can not be called through an instance of a class that does not implement an interface.

我想在运行时扩展的对象没有实现任何接口,最糟糕的是,我需要覆盖的方法是嵌套的和私有的.

The object I'd like to extends at run-time doesn't implement any interface, and worst, the methods I need to override are nested and private.

我知道这在 Python 中相当容易 &C,上面引用的文章说这是可能的:

I know this is fairly easy in Python & C, and the article cited above says that it could be possible :

本系列的下一篇文章将说明克服这些限制的一些技巧.

The next article in this series will illustrate some techniques for overcoming these limitations.

很遗憾,我找不到这篇文章.

Unfortunately, I'm unable to find this article.

推荐答案

尝试使用 CGLIB:

Try using CGLIB:

这是我几年前写的一个教程,它对 CGLIB 的介绍非常简单:CGLIB 介绍

Here is a tutorial I wrote a couple of years ago, its quite simple introduction to CGLIB: CGLIB intro

如果您需要更多功能,请考虑使用 AspectJ

If you need even more power, consider to use AspectJ

希望能帮到你

这篇关于插入 Java 类方法(无接口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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