Bug 416897 - Support post-processing within org.aspectj.weaver.loadtime.WeavingURLClassLoader
Summary: Support post-processing within org.aspectj.weaver.loadtime.WeavingURLClassLoader
Status: NEW
Alias: None
Product: AspectJ
Classification: Tools
Component: LTWeaving (show other bugs)
Version: 1.8.0.M1   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-10 05:11 EDT by Christian Schulze CLA
Modified: 2013-09-10 05:11 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Schulze CLA 2013-09-10 05:11:11 EDT
Currently it's not possible to post-process the byte code after it has been woven. It would be a great feature to support this so that we can apply custom transformations after the aspects have been applied, e.g. using ASM.

A simple extension could look like this:

// class org.aspectj.weaver.loadtime.WeavingURLClassLoader

/**
 * Override to apply transformations after aspects have been applied.
 */
protected byte[] postProcess(String name, byte[] byteCode)
{
  return byteCode;
}


@Override
protected Class defineClass(String name, byte[] b, CodeSource cs) throws IOException
{
    if (trace.isTraceEnabled())
    {
        trace.enter("defineClass", this, new Object[] { name, b, cs });
    }
    // System.err.println("? WeavingURLClassLoader.defineClass(" + name + ", [" + b.length + "])");
    byte orig[] = b;
    /* Avoid recursion during adaptor initialization */
    if (!initializingAdaptor)
    {
        /* Need to defer creation because of possible recursion during constructor execution */
        if (adaptor == null && !initializingAdaptor)
        {
            createAdaptor();
        }

        try
        {
            b = adaptor.weaveClass(name, b, false);
        }
        catch (AbortException ex)
        {
            trace.error("defineClass", ex);
            throw ex;
        }
        catch (Throwable th)
        {
            trace.error("defineClass", th);
        }
     }
  
     // perform custom post-processing
     b = postProcess(name, b);

     Class clazz;
     // On error, define the original form of the class and log the issue
     try
     {
         clazz = super.defineClass(name, b, cs);
     }
     catch (Throwable th)
     {
         trace.error("Weaving class problem. Original class has been returned. The error was caused because of: "
                    + th, th);
         clazz = super.defineClass(name, orig, cs);
     }
     if (trace.isTraceEnabled())
     {
         trace.exit("defineClass", clazz);
     }
     return clazz;
}