]> source.dussan.org Git - aspectj.git/commitdiff
Matthews fix for Bugzilla Bug 77163: Load time weaver default verbose setting V1_2_1
authoraclement <aclement>
Fri, 5 Nov 2004 11:00:41 +0000 (11:00 +0000)
committeraclement <aclement>
Fri, 5 Nov 2004 11:00:41 +0000 (11:00 +0000)
docs/dist/doc/examples/build.xml
weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java
weaver/testsrc/org/aspectj/weaver/WeavingURLClassLoaderTest.java

index 5dbea8f19e0ab971663ef0fd5f6836553446eec3..0797e898f9070c5c6a0c9436ca3fbca527a0d28c 100644 (file)
        </java>
        <echo message="---------- running without tracing - FINISH "/>
 
-       <!-- run appliaction with LTW to add tracing -->
+       <!-- run application with LTW to add tracing -->
        <echo message="---------- running with tracing - START"/>
        <java classname="tracing.ExampleMain"
          fork="true">
                 <jvmarg line="-showversion"/>
                 <sysproperty key="java.system.class.loader" value="org.aspectj.weaver.WeavingURLClassLoader"/>
          <sysproperty key="aj.weaving.verbose" value="True"/>
+                <sysproperty key="org.aspectj.weaver.showWeaveInfo" value="True"/>
          <sysproperty key="aj.class.path" path="${jar.dir}/tracingLib.jar:${jar.dir}/tracingApp.jar"/>
          <sysproperty key="aj.aspect.path" path="${jar.dir}/tracingLib.jar"/>
        </java>
index f89dbb5334c6d087cc93fb3eb808ba364688246b..0e79157bba69d2d0cbe375551c373f1b779c7d78 100644 (file)
 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);
+                       }
                }
        }
 
index 4035ed278fdf4093cb9fb35ed7e918e3feb24db4..3acadcdd2c655025643131784614d3c0966c6d63 100644 (file)
@@ -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);
+               }
+       }
+
 }