Announcement Announcement Module
No announcement yet.
Instrumenting classloader to transform classes before being instantiated Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Instrumenting classloader to transform classes before being instantiated

    Using Spring 3.1+

    Is it possible to get at the Application context loader's classloader to transform a Class definition? Seen the weaving instrumentation stuff but haven't found a solid example online. Not sure how Spring handles class loading but assuming the application context is a "common" shareholder for classes in the classpath and any child classloader (made for whatever reasons) has the App context as it's parent.

    What I want to do is intercept the registration of or reload certain classes to add a field with a specific annotation.

    Looks like the @EnableLoadTimeWeaving allows for setting up a private LoadTimeWeavingConfigurer. Just not sure what to use. Testing is done in Eclipse but the target environment will not be forced into a particular container (i.e. Weblogic, Tomcat and so forth). Would like to not use the -javaagent to jack in a LoadTimeWeaver which is what I assume the EnableLoadTimeWeaving does? Should be able to add a transformer that switches out my bytecode.

    Last edited by young_matthewd; Aug 7th, 2012, 06:56 AM. Reason: Found the EnableLoadTimeWeaving class

  • #2
    Tried using @EnableLoadTimeWeaving as follows

    public class ApplicationConfig implements LoadTimeWeavingConfigurer {

    public LoadTimeWeaver getLoadTimeWeaver() {
    LoadTimeWeaver ltw = new SimpleLoadTimeWeaver();

    ltw.addTransformer(new ClassFileTransformer() {

    public byte[] transform(ClassLoader loader, String className,
    Class<?> classBeingRedefined,
    ProtectionDomain protectionDomain, byte[] classfileBuffer)
    throws IllegalClassFormatException {
    System.out.println("Transformer to Transform Class: "
    + className);
    return classfileBuffer;
    return ltw;

    But the transform method never gets called.