]> source.dussan.org Git - aspectj.git/commitdiff
Fix for Bug 150254 "Provide context for LTW verbose messages" (IWeavingContext.getId...
authormwebster <mwebster>
Wed, 12 Jul 2006 14:26:21 +0000 (14:26 +0000)
committermwebster <mwebster>
Wed, 12 Jul 2006 14:26:21 +0000 (14:26 +0000)
loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java
loadtime/src/org/aspectj/weaver/loadtime/DefaultWeavingContext.java
loadtime/src/org/aspectj/weaver/loadtime/IWeavingContext.java
loadtime/testdata/META-INF/aop.xml [new file with mode: 0644]
loadtime/testsrc/org/aspectj/weaver/loadtime/WeavingContextTest.java [new file with mode: 0644]

index 2b1eef064d2f619f4f6b06eef5c533689eebe11d..2f02ca67dc34e559ba3ac264eb83530ff37b7369 100644 (file)
@@ -55,6 +55,8 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
 
     private final static String AOP_XML = "META-INF/aop.xml";
 
+    private boolean initialized;
+    
     private List m_dumpTypePattern = new ArrayList();
     private boolean m_dumpBefore = false;
     private List m_includeTypePattern = new ArrayList();
@@ -67,14 +69,25 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
     private List m_aspectIncludeStartsWith = new ArrayList();
 
     private StringBuffer namespace;
+    private ClassLoader classLoader;
     private IWeavingContext weavingContext;
 
     public ClassLoaderWeavingAdaptor(final ClassLoader loader, IWeavingContext wContext) {
+       super();
+       this.classLoader = loader;
        this.weavingContext = wContext;
     }
 
-    protected void initialize(final ClassLoader loader, IWeavingContext wContext) {
+    protected void initialize (final ClassLoader deprecatedLoader, IWeavingContext deprecatedContext) {
         //super(null);// at this stage we don't have yet a generatedClassHandler to define to the VM the closures
+       if (initialized) return;
+
+        if (weavingContext == null) {
+               weavingContext = new DefaultWeavingContext(classLoader);
+        }
+
+        createMessageHandler();
+       
         this.generatedClassHandler = new GeneratedClassHandler() {
             /**
              * Callback when we need to define a Closure in the JVM
@@ -91,23 +104,17 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
                     throwable.printStackTrace();
                 }
 
-                defineClass(loader, name, bytes);// could be done lazily using the hook
+                defineClass(classLoader, name, bytes);// could be done lazily using the hook
             }
         };
 
-        if(wContext==null){
-               weavingContext = new DefaultWeavingContext(loader);
-        }else{
-               weavingContext = wContext ;
-        }
-
-        List definitions = parseDefinitions(loader);
+        List definitions = parseDefinitions(classLoader);
         if (!enabled) {
                return;
         }
         
         bcelWorld = new LTWWorld(
-                loader, getMessageHandler(), new ICrossReferenceHandler() {
+                classLoader, getMessageHandler(), new ICrossReferenceHandler() {
                     public void addCrossReference(ISourceLocation from, ISourceLocation to, IRelationship.Kind kind, boolean runtimeTest) {
                         ;// for tools only
                     }
@@ -122,7 +129,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
         weaver = new BcelWeaver(bcelWorld);
 
         // register the definitions
-        registerDefinitions(weaver, loader, definitions);
+        registerDefinitions(weaver, classLoader, definitions);
         if (enabled) {
 
             //bcelWorld.setResolutionLoader(loader.getParent());//(ClassLoader)null);//
@@ -134,6 +141,8 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
                bcelWorld = null;
                weaver = null;
         }
+        
+        initialized = true;
     }
 
     /**
@@ -312,6 +321,10 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
        Kind kind = lint.getLintKind(name);
        kind.signal(infos,null,null);
     }
+       
+       protected String getContextId () {
+               return weavingContext.getId();
+       }
     
     /**
      * Register the aspect, following include / exclude rules
index 3817ae5b09733c7ddea07b3793753ea58192f140..e3d7264e8200862e3caae7bddb3d03ee8f11b6ff 100644 (file)
@@ -23,6 +23,8 @@ import java.util.Enumeration;
 public class DefaultWeavingContext implements IWeavingContext {
        
        protected ClassLoader loader;
+       
+       private String shortName;
 
        /**
         * Construct a new WeavingContext to use the specifed ClassLoader
@@ -60,4 +62,20 @@ public class DefaultWeavingContext implements IWeavingContext {
        public String getFile(URL url) {
        return url.getFile();
        }
+
+       /**
+        * @return unqualifiedclassname@hashcode 
+        */
+       public String getId () {
+               if (shortName == null) {
+                       shortName = getClassLoaderName();
+                       int index = shortName.lastIndexOf(".");
+                       shortName = shortName.substring(index + 1);
+               }
+               return shortName;
+       }
+       
+       public String getSuffix () {
+               return getClassLoaderName();
+       }
 }
index 5b4a0c21c0c9862cf1e48777ab46ccacde7b108e..8311f6e0c043a2d4cab0598ac0c3692d8e3504ef 100644 (file)
@@ -39,12 +39,13 @@ public interface IWeavingContext {
         * In a non-OSGi environment, implementors should return <code>null<code>.
         * @param url
         * @return
+        * @deprecated use getFile() or getClassLoaderName()
         */
        public String getBundleIdFromURL(URL url);
        
        /**
         * In an environment with multiple class loaders allows each to be
-        * identified using something safer and than toString
+        * identified using something safer and possibly shorter than toString
         * @return name of the associated class loader
         */
        public String getClassLoaderName ();
@@ -54,5 +55,12 @@ public interface IWeavingContext {
         * @return filename
         */
        public String getFile(URL url);
+       
+       /**
+        * In an environment with multiple class loaders allows messages
+        * to identified according to the weaving context
+        * @return short name 
+        */
+       public String getId ();
 
 }
diff --git a/loadtime/testdata/META-INF/aop.xml b/loadtime/testdata/META-INF/aop.xml
new file mode 100644 (file)
index 0000000..9f4b1bc
--- /dev/null
@@ -0,0 +1,3 @@
+<aspectj>
+       <weaver options="-verbose"/>
+</aspectj>
\ No newline at end of file
diff --git a/loadtime/testsrc/org/aspectj/weaver/loadtime/WeavingContextTest.java b/loadtime/testsrc/org/aspectj/weaver/loadtime/WeavingContextTest.java
new file mode 100644 (file)
index 0000000..7d53182
--- /dev/null
@@ -0,0 +1,153 @@
+/*******************************************************************************
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     Matthew Webster - initial implementation
+ *******************************************************************************/
+package org.aspectj.weaver.loadtime;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+
+import junit.framework.TestCase;
+
+public class WeavingContextTest extends TestCase {
+
+       private boolean called;
+       
+       public void testWeavingContext() {
+               URLClassLoader loader = new URLClassLoader(new URL[] {},null);
+               IWeavingContext context = new TestWeavingContext(loader);
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+       }
+
+       public void testGetResources() {
+               URLClassLoader loader = new URLClassLoader(new URL[] {},null);
+               IWeavingContext context = new TestWeavingContext(loader) {
+
+                       public Enumeration getResources(String name) throws IOException {
+                               called = true;
+                               return super.getResources(name);
+                       }
+                       
+               };
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+               adaptor.initialize(null,null);
+               
+               assertTrue("IWeavingContext not called",called);
+       }
+
+       public void testGetBundleIdFromURL() {
+               URLClassLoader loader = new URLClassLoader(new URL[] {},null);
+               IWeavingContext context = new TestWeavingContext(loader) {
+
+                       public String getBundleIdFromURL(URL url) {
+                               throw new UnsupportedOperationException();
+                       }
+                       
+               };
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+               try {
+                       adaptor.initialize(null,null);
+               }
+               catch (UnsupportedOperationException ex) {
+                       fail("IWeavingContect.getBundleIdFromURL() is deprecated");
+               }
+       }
+
+       public void testGetClassLoaderName() {
+               URLClassLoader loader = new URLClassLoader(new URL[] {},null);
+               IWeavingContext context = new TestWeavingContext(loader) {
+
+                       public String getClassLoaderName () {
+                               called = true;
+                               return super.getClassLoaderName();
+                       }
+                       
+               };
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+               adaptor.initialize(null,null);
+               
+               assertTrue("IWeavingContext not called",called);
+       }
+
+       public void testGetFile() throws IOException {
+               File file = new File("../loadtime/testdata");
+               URL fileURL = file.getCanonicalFile().toURL();
+               URLClassLoader loader = new URLClassLoader(new URL[] { fileURL },null);
+               IWeavingContext context = new TestWeavingContext(loader) {
+
+                       public String getFile (URL url) {
+                               called = true;
+                               return super.getFile(url);
+                       }
+                       
+               };
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+               adaptor.initialize(null,null);
+               
+               assertTrue("IWeavingContext not called",called);
+       }
+
+       public void testGetId() throws IOException {
+               File file = new File("../loadtime/testdata");
+               URL fileURL = file.getCanonicalFile().toURL();
+               URLClassLoader loader = new URLClassLoader(new URL[] { fileURL },null);
+               IWeavingContext context = new TestWeavingContext(loader) {
+
+                       public String getId () {
+                               called = true;
+                               return super.getId();
+                       }
+                       
+               };
+               ClassLoaderWeavingAdaptor adaptor = new ClassLoaderWeavingAdaptor(loader,context);
+               adaptor.initialize(null,null);
+               
+               assertTrue("IWeavingContext not called",called);
+       }
+
+       private static class TestWeavingContext implements IWeavingContext {
+
+               private ClassLoader loader;
+               
+               public TestWeavingContext (ClassLoader classLoader) {
+                       this.loader = classLoader;
+               }
+               
+               public String getBundleIdFromURL(URL url) {
+                       return null;
+               }
+
+               public String getClassLoaderName() {
+                       return "ClassLoaderName";
+               }
+
+               public String getFile(URL url) {
+                       return "File";
+               }
+
+               public String getId() {
+                       return "Id";
+               }
+
+               public Enumeration getResources(String name) throws IOException {
+                       return loader.getResources(name);
+               }
+               
+       }
+
+       protected void setUp() throws Exception {
+               super.setUp();
+
+               this.called = false;
+       }
+       
+}