哪些框架(和相关语言)支持类替换?

发布于 2024-09-05 21:06:45 字数 877 浏览 2 评论 0原文

我正在写我的硕士论文,其中涉及 .NET 中的 AOP 等,并且我提到缺乏对加载时替换类的支持,这是目前没有 .NET AOP 框架可以实现的一个重要因素。执行真正的动态编织——不强制要求编织类必须扩展ContextBoundObjectMarshalByRefObject或在接口上公开它们的所有语义。

不过,借助 ClassFileTransformer,您可以在 JVM 中使用 Java 来执行此操作:

  • 您可以扩展 ClassFileTransformer
  • 您订阅类加载事件。
  • 在类加载时,您重写该类并替换它。

所有这些都很好,但我的项目总监在最后一刻要求我给他一份支持/不支持类替换的框架(和相关语言)列表。我现在真的没有时间去寻找这个:仅仅进行肤浅的研究并可能在我的论文中加入错误的信息,我会感到不舒服。

所以我问你,全能的编程社区,你能帮忙吗?当然,我并不是要求你们自己去研究这个问题。简而言之,如果您确定某个特定框架支持/不支持此功能,请将其保留为答案。如果您不确定,请不要忘记指出。

非常感谢!


编辑:@ewernli

  • 我问的是(2)。
  • 在 C# 中,您确实可以在运行时发出代码并动态创建新类,但它们是新类,它们不会替换现有类。我想做的是在加载时转换类,就像在 Java 中使用 ClassFileTransformer 所做的那样。
  • 关于修改方法的签名:是的,你是对的。我应该提到,就我而言,我不想修改类的接口,而是修改其方法的内容。

你的回答真的很有帮助。谢谢 :)

I'm writing my master thesis, which deals with AOP in .NET, among other things, and I mention the lack of support for replacing classes at load time as an important factor in the fact that there are currently no .NET AOP frameworks that perform true dynamic weaving -- not without imposing the requirement that woven classes must extend ContextBoundObject or MarshalByRefObject or expose all their semantics on an interface.

You can however do this with Java in the JVM thanks to ClassFileTransformer:

  • You extend ClassFileTransformer.
  • You subscribe to the class load event.
  • On class load, you rewrite the class and replace it.

All this is very well, but my project director has asked me, quite in the last minute, to give him a list of frameworks (and associated languages) that do / do not support class replacement. I really have no time to look for this now: I wouldn't feel comfortable just doing a superficial research and potentially putting erroneous information in my thesis.

So I ask you, oh almighty programming community, can you help out? Of course, I'm not asking you to research this yourselves. Simply, if you know for sure that a particular framework supports / doesn't support this, leave it as an answer. If you're not sure please don't forget to point it out.

Thanks so much!


EDIT: @ewernli

  • I'm asking about (2).
  • In C# you can indeed emit code at run-time and create new classes dynamically, but they are new classes, they do not replace an existing class. What I'd like to do is to transform the class at load-time, like you can do in Java with the ClassFileTransformer.
  • About modifying a method's signature: yes, you're right. I should have mentioned that in my case I don't want to modify the class' interface, but rather the content of its methods.

Your answer was really helpful. Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与酒说心事 2024-09-12 21:06:45

您是在询问(1)运行时真正的类替换,还是(2)在加载类时转换类的设施,或者(3)支持动态类加载的语言?

Java支持使用ClassLoader进行动态类加载,使用ClassFileTransformer进行转换,但不支持真正的类替换。

我不确定 C# 是否适用,但我认为您可以在运行时发出代码并动态创建新类,因此您可以实现 (3) 甚至 (2)。

真正的类替换大多仅由动态语言支持,例如Smalltalk、Ruby、我猜是Python 和其他一些语言。这需要转换类的实例以匹配新的形状。如果类发生变化,他们通常将新字段初始化为零。

AFAIK,移植到 JVM 的动态语言对 ClassLoader 进行了广泛的修改,以支持运行时的类替换。对于 JRuby,请参阅动态调用的初体验以获取更多关于如何执行此操作的指导现在,问题是什么以及即将推出的 invokedynamic 可能会如何提供帮助。

由于类型系统的复杂性,静态类型语言不提供此功能。如果类中的方法签名发生更改,则已加载的其他现有类可能不必遵守新的方法签名,这是不安全的。但是,在 java 中,只要使用 Java 平台调试器架构

已经有人尝试将此功能添加到 Java 和/或静态类型语言中:

  • 对类型安全动态 Java 类的运行时支持
  • 支持应用程序行为的意外动态适应
  • Java 软件动态更新技术

本文提供了以下内容的总体概述:相关问题

  • 类型系统对动态软件演化的影响

不确定这是否能解决您最初的问题,但无论如何,这些指针可能对您的论文感兴趣。

Are you asking about (1) true class replacement at run-time, or (2) facilities to transform the class when it's loaded or (3) languages which support dynamic class loading ?

Java support dynamic class loading with ClassLoader, transformation with ClassFileTransformer, but no true class replacement.

I'm not sure for C#, but I think you can emit code at run-time and create new class dynamically, so you can achieve (3) and probably (2).

True class replacement is mostly supported only by dynamic language, e.g. Smalltalk, Ruby, I guess Python and a few others. This requires the transformation of the instances of the class to match the new shape. They usually initialize the new fields to nil if the class changes.

AFAIK, dynamic languages ported to the JVM make extensive hacking of ClassLoader to support class replacement at run-time. For JRuby, see A first taste of invoke dynamic to get more pointers how they do it now, what's problematic and how the upcoming invokedynamic might help.

This is not offered in statically typed languages because of the complication with the type system. If a method signature change in a class, other existing classes already loaded might not necessary comply with the new method signature which is not safe. In java you can however change a method as long as the signature is the same using the Java Platform Debugger Architecture.

There have been some attempt to add this feature to Java, and/or statically typed languages:

  • Runtime support for type-safe dynamic Java classes
  • Supporting Unanticipated Dynamic Adaptation of Application Behaviour
  • A Technique for Dynamic Updating of Java Software

This paper provide a general overview of related problems

  • Influence of type systems on dynamic software evolution

Not sure exactly if that address you initial question, but these pointers might be interesting for your thesis anyway.

终止放荡 2024-09-12 21:06:45

Java语言不支持类文件替换。 JVM 通过您提到的类公开该功能。因此,所有已移植到 JVM 的语言都可以利用它。

The Java language doesn't support class file replacement. The JVM exposes the feature via the classes you mention. Therefore all languages which have been ported to the JVM can take advantage of it.

两相知 2024-09-12 21:06:45

Erlang 支持热代码交换,如果您也在寻找模拟动态类更新的理论框架,您可以采取查看 Creol 语言(已解释)。

Erlang supports hot code swapping, and if you are looking also for theoretical frameworks that model dynamic class updates, you can take a look at the Creol language (interpreted).

她比我温柔 2024-09-12 21:06:45

Objective-C 的运行时库支持类的动态构造和注册、惰性方法注册和“方法调配”,通过“方法调配”可以在运行时切换方法实现。以前的版本支持“类调配”,可以在运行时用一个类替换另一个类,但现在改用方法调配。 这是参考文档。

Objective-C's runtime library supports dynamic construction and registration of classes, lazy method registration and "method swizzling" by which method implementations can be switched at runtime. Previous versions supported "Class swizzling" by which a class could be substituted for another at runtime, but now method swizzling is used instead. Here's the reference doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文