Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there some way to detect if already loaded class is redefined? #206

Closed
dzharikhin opened this issue Oct 29, 2016 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@dzharikhin
Copy link

dzharikhin commented Oct 29, 2016

Hi! Is there a way to detect class is redefined for already loaded class?
As far as I know new fields, methods, and aanotations are forbidden for already loaded classes.

I'm trying to use ByteBuddy(with javaagent) to add the advice to the loaded class which modifies return value of one method.
My target app works on OSGi, and I have to provide transformation logic as a bundle andI want to make transformation of the target class idempotent - avoid double application of the transformation in case of bundle reinstallation.
I use inlining because of OSGi class visibility - target class doesn't see interceptor class(target class loaded with webappCL, and interceptor with BundleCL)

So, I need to detect somehow if the class is already redefined(perfectly I need to get some "version" of the class, but a boolean flag is ok). It's obvious that better is not to keep such info outside of the transformed class)

Or maybe I could somehow preserve original method to be able to restore original state first, before trying to transform it second time. Looking on this way 2 questions I see
1)Is it possible to store original method somehow?
2)Is it possible to order two transformations for same method of the same class? first I need
Thanks!

@raphw raphw self-assigned this Oct 31, 2016
@raphw raphw added this to the 1.5.2 milestone Oct 31, 2016
@raphw raphw added the question label Oct 31, 2016
@raphw
Copy link
Owner

raphw commented Oct 31, 2016

While you cannot add any field or method, you can add meta data to any class that you retransform. I would suggest you to add an annotation for your purposes.

One problem with this approach is that you would need to make this annotation type available universally. Otherwise, when Byte Buddy uses the reflection API, where annotations that are not loadable by the respective class loader are suppressed.

If you are using an agent builder, you can however work around this by:

  1. Using a DescriptionStrategy.POOL_ONLY. When Byte Buddy parses a class's description, the same constraint does not apply as for the reflection API.
  2. Implementing a custom PoolStrategy where you return a TypePool.Default and give it a parent type pool of TypePool.Explicit that references all types that are relevant for your annotation.

The original method is not stored by Byte Buddy but it is available by the JVM. If you reapply a retransformation, the retransformation always starts from the base class implementation. You do therefore not need to worry about transformations to apply twice. All transformations are chainable if you for example chain two Advice implementations.

@raphw raphw closed this as completed Oct 31, 2016
@dzharikhin
Copy link
Author

Wow!!!Cool!!
It seems it works!!! Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants