views:

69

answers:

4

Hi,

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 :)

+2  A: 

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.

Pete Kirkham
True, thank you. I often forget to make a distinction between language and framework. (I know, it's a very big distinction). That's very helpful, thank you. I'll edit the question to make it more accurate. However, do you know if those other languages that have been ported to the JVM *do* take advantage of it? I mean: do they offer any way to replace a class?
Alix
+3  A: 

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.

ewernli
This is a great answer :), thanks! I've edited my question to reply to some of your points. Thanks for the resources, they're really helpful.
Alix
+1  A: 

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).

ShiDoiSi
Thanks :) *[stupid 15-character restriction, grumble grumble]*
Alix
+1  A: 

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.

Graham Lee
Great, thanks for the explanation and the link! You guys are really helping me out :)
Alix