For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2005 > Using Jen for transforming classes on the fly









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using Jen for transforming classes on the fly
habib

2005-11-03, 7:02 pm

Hi there,

In the discussion with URL
http://groups.google.com/group/comp...88d475006bf10aa
Ross suggested "Jen" tool for transforming classes and he said it can
transform classes even in runtime.

I looked at Jen web site, but unfortunately, there is no sample there
but it claims it's base is upon ASM transformation tool.

When I looked at ASM, I figured out that it indeed can transform
classes but not in runtime.
Programmers can transform classes and then load them using reflection
API, but they can't manipulate them in runtime.

Now, my question is: Am I correct about ASM or Jen? Do they provide any
transformation of loaded classes? What does Ross mean by "runtime"? and
finally, why there is no sample in Jen site? Is it still an active
project?

By the way, Web sites of Jen and ASM are:
http://jen.dev.java.net/
http://asm.objectweb.org/

Thank you in advance,
Habib

Ross Bamford

2005-11-03, 7:02 pm

On Thu, 03 Nov 2005 14:01:18 -0000, habib <habib.seif@gmail.com> wrote:

> Hi there,
>
> In the discussion with URL
> http://groups.google.com/group/comp...88d475006bf10aa
> Ross suggested "Jen" tool for transforming classes and he said it can
> transform classes even in runtime.
>
> I looked at Jen web site, but unfortunately, there is no sample there
> but it claims it's base is upon ASM transformation tool.
>


There are a fair few examples on the Wiki, at
http://wiki.java.net/bin/view/Javatools/JenExamples . Maybe this should be
more prominently linked from the front page. The wiki carries quite a bit
of documentation, and theres also a lot of examples in the unit tests too.

> When I looked at ASM, I figured out that it indeed can transform
> classes but not in runtime.
> Programmers can transform classes and then load them using reflection
> API, but they can't manipulate them in runtime.
>
> Now, my question is: Am I correct about ASM or Jen? Do they provide any
> transformation of loaded classes? What does Ross mean by "runtime"? and
> finally, why there is no sample in Jen site? Is it still an active
> project?


Both of them are 'general tools that modify classes', Jen based on ASM,
with Jen also providing a 'runtime' layer that handles common classworking
tasks like defining new classes and working from java.lang.Class
instances. As I said in my previous message, whether you can do exactly
what you want to do depends on a few things, but basically there are two
ways to do what you're after:

1) call the SoftClass.defineClass() method on your modified SoftClass
instance, after giving it a new name (not
truly modifying the class in-situ, but if you only need to get it's
functionality, it should do).
There are limitations, such as that you cannot retransform such
classes, and instances aren't affected.

2) (ab)use a Java agent (See
http://java.sun.com/j2se/1.5.0/docs...ge-summary.html)
to allow you to get hold of the Instrumentation instance, and then
call the redefineClasses method on
that, passing in the byte array from SoftClass.generateByteCode()
method. Again, retransformation is out, unless
you cache the byte array somewhere before redefining the class.

The second option is of course the more complex, but it's the only way to
actually change (rather than derive from) an existing class at runtime, by
which I mean 'after application start, and before application shutdown'.
There are still limitations, however, in that certain platform classes
can't be transformed, you really shouldn't remove methods, and any
executing methods will of course be unaffected.

Still though, I'd reiterate what I said previously about all of this - the
JVM just isn't designed (at present) for any of this, and while there are
numerous uses for runtime class modification, any kind of true 'dynamic
methods' as discussed before, even if possible, would be very difficult to
make work _within the Java language_, and even more difficult to make
performant.

========

(P.s. This is probably a bit tool-specific for this newsgroup, but there
is a 'users' mailing list for Jen if you need further Jen-specific help,
see: http://jen.dev.java.net/servlets/Pr...ailingListList.
Alternatively, email me directly and I'll be happy to help.)

(P.p.s. Yes, Jen is still very active. The third milestone snapshot, 0.30
was released on Friday).

--
Ross Bamford - rosco@roscopeco.remove.co.uk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com