From edd6539e53d96e60f257952e582783c7d8aac25c Mon Sep 17 00:00:00 2001 From: aclement Date: Fri, 5 Nov 2004 11:00:41 +0000 Subject: [PATCH] Matthews fix for Bugzilla Bug 77163: Load time weaver default verbose setting --- docs/dist/doc/examples/build.xml | 3 +- .../aspectj/weaver/tools/WeavingAdaptor.java | 118 ++++++++++++------ .../weaver/WeavingURLClassLoaderTest.java | 118 +++++++++++++++--- 3 files changed, 184 insertions(+), 55 deletions(-) diff --git a/docs/dist/doc/examples/build.xml b/docs/dist/doc/examples/build.xml index 5dbea8f19..0797e898f 100644 --- a/docs/dist/doc/examples/build.xml +++ b/docs/dist/doc/examples/build.xml @@ -437,7 +437,7 @@ - + @@ -447,6 +447,7 @@ + diff --git a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java index f89dbb533..0e79157bb 100644 --- a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java +++ b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java @@ -14,20 +14,25 @@ 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; @@ -57,12 +62,13 @@ public class WeavingAdaptor { * 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 */ @@ -103,7 +109,7 @@ public class WeavingAdaptor { list.addAll(0,FileUtil.makeClasspath(urls)); } else { - if (verbose) System.err.println("WeavingAdaptor: Warning - could not determine classpath for " + loader); + warn("cannot determine classpath"); } } @@ -129,10 +135,14 @@ public class WeavingAdaptor { } 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(); @@ -146,10 +156,12 @@ public class WeavingAdaptor { * @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 + "'"); } } @@ -162,6 +174,7 @@ public class WeavingAdaptor { */ public byte[] weaveClass (String name, byte[] bytes) throws IOException { if (shouldWeave(name)) { + info("weaving '" + name + "'"); bytes = getWovenBytes(name, bytes); } return bytes; @@ -170,7 +183,6 @@ public class WeavingAdaptor { 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; } @@ -209,21 +221,8 @@ public class WeavingAdaptor { 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(); @@ -238,9 +237,18 @@ public class WeavingAdaptor { * @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) { @@ -253,24 +261,64 @@ public class WeavingAdaptor { } 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); + } } } diff --git a/weaver/testsrc/org/aspectj/weaver/WeavingURLClassLoaderTest.java b/weaver/testsrc/org/aspectj/weaver/WeavingURLClassLoaderTest.java index 4035ed278..3acadcdd2 100644 --- a/weaver/testsrc/org/aspectj/weaver/WeavingURLClassLoaderTest.java +++ b/weaver/testsrc/org/aspectj/weaver/WeavingURLClassLoaderTest.java @@ -16,6 +16,8 @@ import java.io.File; 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; @@ -41,15 +43,17 @@ public class WeavingURLClassLoaderTest extends 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 { @@ -62,8 +66,8 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -76,8 +80,8 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -106,8 +110,38 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -120,8 +154,8 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -134,8 +168,8 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -149,8 +183,8 @@ public class WeavingURLClassLoaderTest extends TestCase { } 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 { @@ -231,6 +265,23 @@ public class WeavingURLClassLoaderTest extends TestCase { } } + 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)); @@ -274,8 +325,8 @@ public class WeavingURLClassLoaderTest extends TestCase { * 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 { @@ -291,8 +342,8 @@ public class WeavingURLClassLoaderTest extends TestCase { * 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 { @@ -325,4 +376,33 @@ public class WeavingURLClassLoaderTest extends TestCase { } } + 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); + } + } + } -- 2.39.5