};
List definitions = parseDefinitions(classLoader);
- if (!enabled) {
- if (trace.isTraceEnabled()) trace.exit("initialize",enabled);
+ if (!isEnabled()) {
+ if (trace.isTraceEnabled()) trace.exit("initialize",false);
return;
}
// register the definitions
registerDefinitions(weaver, classLoader, definitions);
- if (enabled) {
+ if (isEnabled()) {
//bcelWorld.setResolutionLoader(loader.getParent());//(ClassLoader)null);//
}
initialized = true;
- if (trace.isTraceEnabled()) trace.exit("initialize",enabled);
+ if (trace.isTraceEnabled()) trace.exit("initialize",isEnabled());
}
/**
}
}
if (definitions.isEmpty()) {
- enabled = false;// will allow very fast skip in shouldWeave()
+ disable();// will allow very fast skip in shouldWeave()
info("no configuration found. Disabling weaver for class loader " + getClassLoaderName(loader));
}
} catch (Exception e) {
- enabled = false;// will allow very fast skip in shouldWeave()
+ disable();// will allow very fast skip in shouldWeave()
warn("parse definitions failed",e);
}
return definitions;
registerIncludeExclude(weaver, loader, definitions);
registerDump(weaver, loader, definitions);
} catch (Exception e) {
- enabled = false;// will allow very fast skip in shouldWeave()
+ disable();// will allow very fast skip in shouldWeave()
warn("register definition failed",(e instanceof AbortException)?null:e);
}
}
kind.signal(infos,null,null);
}
- protected String getContextId () {
+ public String getContextId () {
return weavingContext.getId();
}
/* We didn't register any aspects so disable the adaptor */
if (namespace == null) {
- enabled = false;
+ disable();
info("no aspects registered. Disabling weaver for class loader " + getClassLoaderName(loader));
}
- if (trace.isTraceEnabled()) trace.exit("registerAspects",enabled);
+ if (trace.isTraceEnabled()) trace.exit("registerAspects",isEnabled());
}
/**
if (trace.isTraceEnabled()) trace.exit("defineClass",clazz);
}
-}
+}
\ No newline at end of file
<compile
files="TracingTest.java"
/>
- <ant file="ant.xml" target="Tracing" verbose="true"/>
+ <ant file="ant.xml" target="Tracing" verbose="true">
+ <stdout>
+ <line text="? TracingTest.main() trace="/>
+ </stdout>
+ </ant>
</ajc-test>
<ajc-test dir="tracing" title="Default tracing" keywords="tracing">
<compile
files="DefaultTracingTest.java"
/>
- <ant file="ant.xml" target="Default tracing" verbose="true"/>
+ <ant file="ant.xml" target="Default tracing" verbose="true">
+ <stdout>
+ <line text="? DefaultTracingTest.main() trace=org.aspectj.weaver.tools.DefaultTrace"/>
+ </stdout>
+ </ant>
</ajc-test>
- <ajc-test dir="tracing" title="Default TraceMessageHandler" keywords="tracing">
+ <ajc-test dir="tracing" title="Trace messages" keywords="tracing">
<compile
- files="HelloWorld.java, MyDefaultTraceMessageHandler.java"
+ files="HelloWorld.java"
/>
<compile
files="IncludedAspect.aj, ExcludedAspect.aj"
/>
- <run class="HelloWorld" ltw="aop-defaulttracemessagehandler.xml">
+ <ant file="ant.xml" target="Trace messages" verbose="true">
<stdout>
<line text="Hello World!"/>
</stdout>
+ <!--
+ Always get info messages even without -verbose because output determined
+ by tracing infrastructure.
+
+ Duplicate warning and error messages: first through trace then message
+ writer
+ -->
<stderr>
+ <line text="info AspectJ Weaver Version DEVELOPMENT built on"/>
+ <line text="info register classloader"/>
+ <line text="info using configuration"/>
<line text="info register aspect IncludedAspect"/>
<line text="warning aspect ExcludedAspect exluded"/>
+ <line text="warning aspect ExcludedAspect exluded"/>
+ <line text="error Cannot find m_parent aspect"/>
<line text="error Cannot find m_parent aspect"/>
- <line text="error Concrete-aspect 'IncludedMissingAspect' could not be registered"/>
- <line text="info weaving 'HelloWorld'"/>
+ <line text="warning register definition failed"/>
+ <line text="warning register definition failed"/>
</stderr>
- </run>
+ </ant>
</ajc-test>
-
</suite>
import org.aspectj.bridge.AbortException;
import org.aspectj.bridge.IMessage;
+import org.aspectj.bridge.IMessageContext;
import org.aspectj.bridge.IMessageHandler;
import org.aspectj.bridge.Message;
import org.aspectj.bridge.MessageUtil;
import org.aspectj.bridge.MessageWriter;
import org.aspectj.bridge.Version;
+import org.aspectj.bridge.WeaveMessage;
import org.aspectj.bridge.IMessage.Kind;
import org.aspectj.org.objectweb.asm.ClassReader;
import org.aspectj.util.FileUtil;
* weaving messages to be written to the console.
*
*/
-public class WeavingAdaptor {
+public class WeavingAdaptor implements IMessageContext {
/**
* System property used to turn on verbose weaving messages
*/
public static final String WEAVING_ADAPTOR_VERBOSE = "aj.weaving.verbose";
public static final String SHOW_WEAVE_INFO_PROPERTY = "org.aspectj.weaver.showWeaveInfo";
+ public static final String TRACE_MESSAGES_PROPERTY = "org.aspectj.tracing.messages";
- protected boolean enabled = true;
+ private boolean enabled = true;
protected boolean verbose = getVerbose();
protected BcelWorld bcelWorld;
protected BcelWeaver weaver;
}
protected void setMessageHandler (IMessageHandler mh) {
- if (messageHolder != null) {
- messageHolder.flushMessages();
- messageHolder = null;
+ if (mh instanceof ISupportsMessageContext) {
+ ISupportsMessageContext smc = (ISupportsMessageContext)mh;
+ smc.setMessageContext(this);
}
- messageHandler = mh;
- bcelWorld.setMessageHandler(mh);
+ if (mh != messageHolder) messageHolder.setDelegate(mh);
+ messageHolder.flushMessages();
+ }
+
+ protected void disable () {
+ enabled = false;
+ messageHolder.flushMessages();
+ }
+
+ protected boolean isEnabled () {
+ return enabled;
}
/**
return MessageUtil.error(messageHandler,message);
}
- protected String getContextId () {
+ public String getContextId () {
return "WeavingAdaptor";
}
* Processes messages arising from weaver operations.
* Tell weaver to abort on any message more severe than warning.
*/
- protected class WeavingAdaptorMessageHandler extends MessageWriter {
+ protected class WeavingAdaptorMessageHandler implements IMessageHandler {
- private Set ignoring = new HashSet();
- private IMessage.Kind failKind;
+ private IMessageHandler delegate;
private boolean accumulating = true;
private List messages = new ArrayList();
+ protected boolean traceMessages = Boolean.getBoolean(TRACE_MESSAGES_PROPERTY);
+
public WeavingAdaptorMessageHandler (PrintWriter writer) {
+
+ this.delegate = new WeavingAdaptorMessageWriter(writer);
+ }
+
+ public boolean handleMessage(IMessage message) throws AbortException {
+ if (traceMessages) traceMessage(message);
+ if (accumulating) {
+ boolean result = addMessage(message);
+ if (0 <= message.getKind().compareTo(IMessage.ERROR)) {
+ throw new AbortException(message);
+ }
+ return result;
+ }
+ else return delegate.handleMessage(message);
+ }
+
+ private void traceMessage (IMessage message) {
+ if (message instanceof WeaveMessage) {
+ trace.debug(render(message));
+ }
+ else if (message.isDebug()) {
+ trace.debug(render(message));
+ }
+ else if (message.isInfo()) {
+ trace.info(render(message));
+ }
+ else if (message.isWarning()) {
+ trace.warn(render(message),message.getThrown());
+ }
+ else if (message.isError()) {
+ trace.error(render(message),message.getThrown());
+ }
+ else if (message.isFailed()) {
+ trace.fatal(render(message),message.getThrown());
+ }
+ else if (message.isAbort()) {
+ trace.fatal(render(message),message.getThrown());
+ }
+ else {
+ trace.error(render(message),message.getThrown());
+ }
+ }
+
+ protected String render(IMessage message) {
+ return "[" + getContextId() + "] " + message.toString();
+ }
+
+ public boolean isIgnoring (Kind kind) {
+ return delegate.isIgnoring(kind);
+ }
+
+ public void dontIgnore (IMessage.Kind kind) {
+ if (null != kind) {
+ delegate.dontIgnore(kind);
+ }
+ }
+
+ private boolean addMessage (IMessage message) {
+ messages.add(message);
+ return true;
+ }
+
+ public void flushMessages () {
+ for (Iterator iter = messages.iterator(); iter.hasNext();) {
+ IMessage message = (IMessage)iter.next();
+ delegate.handleMessage(message);
+ }
+ accumulating = false;
+ messages.clear();
+ }
+
+ public void setDelegate (IMessageHandler messageHandler) {
+ delegate = messageHandler;
+ }
+ }
+
+ protected class WeavingAdaptorMessageWriter extends MessageWriter {
+
+ private Set ignoring = new HashSet();
+ private IMessage.Kind failKind;
+
+ public WeavingAdaptorMessageWriter (PrintWriter writer) {
super(writer,true);
ignore(IMessage.WEAVEINFO);
}
public boolean handleMessage(IMessage message) throws AbortException {
- addMessage(message);
boolean result = super.handleMessage(message);
if (0 <= message.getKind().compareTo(failKind)) {
throw new AbortException(message);
public void dontIgnore (IMessage.Kind kind) {
if (null != kind) {
ignoring.remove(kind);
- if (kind.equals(IMessage.INFO)) accumulating = false;
- }
- }
-
- private void addMessage (IMessage message) {
- if (accumulating && isIgnoring(message.getKind())) {
- messages.add(message);
}
}
-
- public void flushMessages () {
- for (Iterator iter = messages.iterator(); iter.hasNext();) {
- IMessage message = (IMessage)iter.next();
- super.handleMessage(message);
- }
- accumulating = false;
- messages.clear();
- }
protected String render(IMessage message) {
return "[" + getContextId() + "] " + super.render(message);