package org.aspectj.weaver.tools;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.PrintWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.StringTokenizer;
import org.aspectj.bridge.AbortException;
import org.aspectj.bridge.IMessage;
-import org.aspectj.bridge.IMessageHandler;
+import org.aspectj.bridge.MessageUtil;
+import org.aspectj.bridge.MessageWriter;
import org.aspectj.bridge.IMessage.Kind;
import org.aspectj.util.FileUtil;
import org.aspectj.weaver.IClassFileProvider;
* 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";
private boolean enabled = true;
private boolean verbose = getVerbose();
private BcelWorld bcelWorld = null;
private BcelWeaver weaver = null;
- private IMessageHandler messageHandler = null;
+ private WeavingAdaptorMessageHandler messageHandler = null;
private GeneratedClassHandler generatedClassHandler;
private Map generatedClasses = new HashMap(); /* String -> UnwovenClassFile */
list.addAll(0,FileUtil.makeClasspath(urls));
}
else {
- if (verbose) System.err.println("WeavingAdaptor: Warning - could not determine classpath for " + loader);
+ warn("cannot determine classpath");
}
}
}
private void init(List classPath, List aspectPath) {
- if (verbose) System.out.println("WeavingAdaptor: classPath='" + classPath + "'");
+ messageHandler = new WeavingAdaptorMessageHandler(new PrintWriter(System.err));
+ if (verbose) messageHandler.dontIgnore(IMessage.INFO);
+ else messageHandler.ignore(IMessage.INFO);
+ if (Boolean.getBoolean(SHOW_WEAVE_INFO_PROPERTY)) messageHandler.dontIgnore(IMessage.WEAVEINFO);
+
+ info("using classpath: " + classPath);
+ info("using aspectpath: " + aspectPath);
- // make sure the weaver can find all types...
- messageHandler = new MessageHandler();
bcelWorld = new BcelWorld(classPath,messageHandler,null);
bcelWorld.setXnoInline(false);
bcelWorld.getLint().loadDefaultProperties();
* @param url to be appended to search path
*/
public void addURL(URL url) {
+ File libFile = new File(url.getPath());
try {
- weaver.addLibraryJarFile(new File(url.getPath()));
+ weaver.addLibraryJarFile(libFile);
}
catch (IOException ex) {
+ warn("bad library: '" + libFile + "'");
}
}
*/
public byte[] weaveClass (String name, byte[] bytes) throws IOException {
if (shouldWeave(name)) {
+ info("weaving '" + name + "'");
bytes = getWovenBytes(name, bytes);
}
return bytes;
private boolean shouldWeave (String name) {
name = name.replace('/','.');
boolean b = (enabled && !generatedClasses.containsKey(name) && shouldWeaveName(name) && shouldWeaveAspect(name));
- if (verbose) System.out.println("WeavingAdaptor: shouldWeave('" + name + "') " + b);
return b;
}
private void registerAspectLibraries(List aspectPath) {
// System.err.println("? WeavingAdaptor.registerAspectLibraries(" + aspectPath + ")");
for (Iterator i = aspectPath.iterator(); i.hasNext();) {
- String lib = (String)i.next();
- File libFile = new File(lib);
- if (libFile.isFile() && lib.endsWith(".jar")) {
- try {
- if (verbose) System.out.println("WeavingAdaptor: adding aspect '" + lib + "' to weaver");
- addAspectLibrary(new File(lib));
- } catch (IOException ioEx) {
- if (verbose) System.err.println(
- "WeavingAdaptor: Warning - could not load aspect path entry "
- + lib + " : " + ioEx);
- }
- } else {
- if (verbose) System.err.println(
- "WeavingAdaptor: Warning - ignoring aspect path entry: " + lib);
- }
+ String libName = (String)i.next();
+ addAspectLibrary(libName);
}
weaver.prepareForWeave();
* @param aspectLibraryJarFile a jar file representing an aspect library
* @throws IOException
*/
- private void addAspectLibrary(File aspectLibraryJarFile) throws IOException {
- weaver.addLibraryJarFile(aspectLibraryJarFile);
-// weaver.prepareForWeave();
+ private void addAspectLibrary(String aspectLibraryName) {
+ File aspectLibrary = new File(aspectLibraryName);
+ if (aspectLibrary.isFile() && aspectLibraryName.endsWith(".jar")) {
+ try {
+ info("adding aspect library: '" + aspectLibrary + "'");
+ weaver.addLibraryJarFile(aspectLibrary);
+ } catch (IOException ex) {
+ error("exception adding aspect library: '" + ex + "'");
+ }
+ } else {
+ error("bad aspect library: '" + aspectLibrary + "'");
+ }
}
private static List makeClasspath(String cp) {
}
return ret;
}
+
+ private boolean info (String message) {
+ return MessageUtil.info(messageHandler,message);
+ }
+
+ private boolean warn (String message) {
+ return MessageUtil.warn(messageHandler,message);
+ }
+
+ private boolean error (String message) {
+ return MessageUtil.error(messageHandler,message);
+ }
/**
* Processes messages arising from weaver operations.
- * Tell weaver to abort on any non-informational error.
+ * Tell weaver to abort on any message more severe than warning.
*/
- private class MessageHandler implements IMessageHandler {
+ private class WeavingAdaptorMessageHandler extends MessageWriter {
+
+ private Set ignoring = new HashSet();
+ private IMessage.Kind failKind;
+
+ public WeavingAdaptorMessageHandler (PrintWriter writer) {
+ super(writer,true);
+
+ ignore(IMessage.WEAVEINFO);
+ this.failKind = IMessage.ERROR;
+
+ }
public boolean handleMessage(IMessage message) throws AbortException {
- if (!isIgnoring(message.getKind())) {
- System.err.println(message.getSourceLocation()+": "+message.getKind()+" "+message.getMessage());
- if (message.getKind() == IMessage.ERROR) throw new AbortException(message);
+ boolean result = super.handleMessage(message);
+ if (0 <= message.getKind().compareTo(failKind)) {
+ throw new AbortException(message);
+ }
+ return true;
+ }
+
+ public boolean isIgnoring (Kind kind) {
+ return ((null != kind) && (ignoring.contains(kind)));
+ }
+
+ /**
+ * Set a message kind to be ignored from now on
+ */
+ public void ignore (IMessage.Kind kind) {
+ if ((null != kind) && (!ignoring.contains(kind))) {
+ ignoring.add(kind);
}
- return true;
}
- public boolean isIgnoring(Kind kind) {
- if (verbose) return false;
- else return ((kind == IMessage.INFO) || (kind == IMessage.DEBUG));
+ /**
+ * Remove a message kind from the list of those ignored from now on.
+ */
+ public void dontIgnore (IMessage.Kind kind) {
+ if (null != kind) {
+ ignoring.remove(kind);
+ }
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
+import java.util.Enumeration;
+import java.util.Properties;
import junit.framework.TestCase;
private final static String ITD_ASPECTS = BcweaverTests.TESTDATA_PATH + "/ltw-itdaspects.jar";
private final static String PER_ASPECTS = BcweaverTests.TESTDATA_PATH + "/ltw-peraspects.jar";
+ private final static String NULL = "null";
+
+ private Properties savedProperties;
public WeavingURLClassLoaderTest(String name) {
super(name);
- System.setProperty(WeavingAdaptor.WEAVING_ADAPTOR_VERBOSE,"true");
}
public void testLoadClass () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testLoadWovenClass () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,WOVEN_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,WOVEN_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testWeaveWovenClass () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + WOVEN_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + WOVEN_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testWeaveAdvice () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
+
+ try {
+ Class clazz = loader.loadClass("LTWHelloWorld");
+ invokeMain(clazz,new String[] { "LTWAspect" });
+ }
+ catch (Exception ex) {
+ fail(ex.toString());
+ }
+ }
+
+ public void testWeaveAdviceWithVerbose () {
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingAdaptor.WEAVING_ADAPTOR_VERBOSE,"true");
+ WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
+
+ try {
+ Class clazz = loader.loadClass("LTWHelloWorld");
+ invokeMain(clazz,new String[] { "LTWAspect" });
+ }
+ catch (Exception ex) {
+ fail(ex.toString());
+ }
+ }
+
+ public void testWeaveAdviceWithWeaveInfo () {
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingAdaptor.SHOW_WEAVE_INFO_PROPERTY,"true");
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testWeaveDeclareWarningAdvice () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,DW_ADVICE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,DW_ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,DW_ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,DW_ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testWeaveDeclareErrorAdvice () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,DE_ADVICE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,DE_ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,DE_ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,DE_ADVICE_ASPECTS + File.pathSeparator + CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
public void testWeaveAroundClosure () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,AROUNDCLOSURE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,AROUNDCLOSURE_ASPECTS + File.pathSeparator + CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,AROUNDCLOSURE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,AROUNDCLOSURE_ASPECTS + File.pathSeparator + CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
}
+ public void testJunkAspectJar () {
+ File junkJar = new File(JUNK_JAR);
+ assertFalse(junkJar + " should not exist",junkJar.exists());
+
+ URL aspects = FileUtil.getFileURL(junkJar);
+ URL[] classURLs = new URL[] { aspects };
+ URL[] aspectURLs = new URL[] { aspects };
+
+ try {
+ WeavingURLClassLoader loader = new WeavingURLClassLoader(classURLs,aspectURLs,getClass().getClassLoader());
+ fail("Expecting org.aspectj.bridge.AbortException");
+ }
+ catch (Exception ex) {
+ assertTrue("Expecting org.aspectj.bridge.AbortException caught " + ex,(ex instanceof org.aspectj.bridge.AbortException));
+ }
+ }
+
public void testAddURL () {
URL classes = FileUtil.getFileURL(new File(CLASSES_JAR));
URL aspects = FileUtil.getFileURL(new File(ADVICE_ASPECTS));
* Aspects on ASPECTPATH but missing from CLASSPATH
*/
public void testIncompletePath () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,ADVICE_ASPECTS);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
* Ensure package object is correct
*/
public void testPackage () {
- System.setProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
- System.setProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
+ setSystemProperty(WeavingURLClassLoader.WEAVING_ASPECT_PATH,"");
+ setSystemProperty(WeavingURLClassLoader.WEAVING_CLASS_PATH,CLASSES_JAR);
WeavingURLClassLoader loader = new WeavingURLClassLoader(getClass().getClassLoader());
try {
}
}
+ private void setSystemProperty (String key, String value) {
+ Properties systemProperties = System.getProperties();
+ copyProperty(key,systemProperties,savedProperties);
+ systemProperties.setProperty(key,value);
+ }
+
+ private static void copyProperty (String key, Properties from, Properties to) {
+ String value = from.getProperty(key,NULL);
+ to.setProperty(key,value);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ savedProperties = new Properties();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ /* Restore system properties */
+ Properties systemProperties = System.getProperties();
+ for (Enumeration enum = savedProperties.keys(); enum.hasMoreElements(); ) {
+ String key = (String)enum.nextElement();
+ String value = savedProperties.getProperty(key);
+ if (value == NULL) systemProperties.remove(key);
+ else systemProperties.setProperty(key,value);
+ }
+ }
+
}