From 0f5ecb7c4fbf72f9ef19f7fa7aa2d14dc82cb6fc Mon Sep 17 00:00:00 2001 From: aclement Date: Tue, 10 Nov 2009 16:18:14 +0000 Subject: [PATCH] prevent re-entrancy on a per thread basis --- .../aspectj/weaver/tools/WeavingAdaptor.java | 87 +++++++++++-------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java index 2fa2905fe..cb137d8d1 100644 --- a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java +++ b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java @@ -258,6 +258,14 @@ public class WeavingAdaptor implements IMessageContext { return weaveClass(name, bytes, false); } + // Track if the weaver is already running on this thread - don't allow re-entrant calls + private ThreadLocal weaverRunning = new ThreadLocal() { + @Override + protected Boolean initialValue() { + return Boolean.FALSE; + } + }; + /** * Weave a class using aspects previously supplied to the adaptor. * @@ -268,52 +276,61 @@ public class WeavingAdaptor implements IMessageContext { * @exception IOException weave failed */ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IOException { - if (trace.isTraceEnabled()) { - trace.enter("weaveClass", this, new Object[] { name, bytes }); + if (weaverRunning.get()) { + // System.out.println("AJC: avoiding re-entrant call to transform " + name); + return bytes; } - - if (!enabled) { + try { + weaverRunning.set(true); if (trace.isTraceEnabled()) { - trace.exit("weaveClass", false); + trace.enter("weaveClass", this, new Object[] { name, bytes }); } - return bytes; - } - try { - delegateForCurrentClass = null; - name = name.replace('/', '.'); - if (couldWeave(name, bytes)) { - if (accept(name, bytes)) { - // TODO @AspectJ problem - // Annotation style aspects need to be included regardless in order to get - // a valid aspectOf()/hasAspect() generated in them. However - if they are excluded - // (via include/exclude in aop.xml) they really should only get aspectOf()/hasAspect() - // and not be included in the full set of aspects being applied by 'this' weaver - debug("weaving '" + name + "'"); - bytes = getWovenBytes(name, bytes); - } else if (shouldWeaveAnnotationStyleAspect(name, bytes)) { - if (mustWeave) { - if (bcelWorld.getLint().mustWeaveXmlDefinedAspects.isEnabled()) { - bcelWorld.getLint().mustWeaveXmlDefinedAspects.signal(name, null); + if (!enabled) { + if (trace.isTraceEnabled()) { + trace.exit("weaveClass", false); + } + return bytes; + } + + try { + delegateForCurrentClass = null; + name = name.replace('/', '.'); + if (couldWeave(name, bytes)) { + if (accept(name, bytes)) { + // TODO @AspectJ problem + // Annotation style aspects need to be included regardless in order to get + // a valid aspectOf()/hasAspect() generated in them. However - if they are excluded + // (via include/exclude in aop.xml) they really should only get aspectOf()/hasAspect() + // and not be included in the full set of aspects being applied by 'this' weaver + debug("weaving '" + name + "'"); + bytes = getWovenBytes(name, bytes); + } else if (shouldWeaveAnnotationStyleAspect(name, bytes)) { + if (mustWeave) { + if (bcelWorld.getLint().mustWeaveXmlDefinedAspects.isEnabled()) { + bcelWorld.getLint().mustWeaveXmlDefinedAspects.signal(name, null); + } } + // an @AspectJ aspect needs to be at least munged by the aspectOf munger + debug("weaving '" + name + "'"); + bytes = getAtAspectJAspectBytes(name, bytes); + } else { + debug("not weaving '" + name + "'"); } - // an @AspectJ aspect needs to be at least munged by the aspectOf munger - debug("weaving '" + name + "'"); - bytes = getAtAspectJAspectBytes(name, bytes); } else { - debug("not weaving '" + name + "'"); + debug("cannot weave '" + name + "'"); } - } else { - debug("cannot weave '" + name + "'"); + } finally { + delegateForCurrentClass = null; } - } finally { - delegateForCurrentClass = null; - } - if (trace.isTraceEnabled()) { - trace.exit("weaveClass", bytes); + if (trace.isTraceEnabled()) { + trace.exit("weaveClass", bytes); + } + return bytes; + } finally { + weaverRunning.set(false); } - return bytes; } /** -- 2.39.5