From 928cb4a192b7f8acdfe706a35a6e329cc0dff13c Mon Sep 17 00:00:00 2001 From: mwebster Date: Wed, 12 Jul 2006 14:26:21 +0000 Subject: [PATCH] Fix for Bug 150254 "Provide context for LTW verbose messages" (IWeavingContext.getId(), WeavingContextTest) --- .../loadtime/ClassLoaderWeavingAdaptor.java | 35 ++-- .../loadtime/DefaultWeavingContext.java | 18 +++ .../weaver/loadtime/IWeavingContext.java | 10 +- loadtime/testdata/META-INF/aop.xml | 3 + .../weaver/loadtime/WeavingContextTest.java | 153 ++++++++++++++++++ 5 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 loadtime/testdata/META-INF/aop.xml create mode 100644 loadtime/testsrc/org/aspectj/weaver/loadtime/WeavingContextTest.java diff --git a/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java b/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java index 2b1eef064..2f02ca67d 100644 --- a/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java +++ b/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java @@ -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 diff --git a/loadtime/src/org/aspectj/weaver/loadtime/DefaultWeavingContext.java b/loadtime/src/org/aspectj/weaver/loadtime/DefaultWeavingContext.java index 3817ae5b0..e3d7264e8 100644 --- a/loadtime/src/org/aspectj/weaver/loadtime/DefaultWeavingContext.java +++ b/loadtime/src/org/aspectj/weaver/loadtime/DefaultWeavingContext.java @@ -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(); + } } diff --git a/loadtime/src/org/aspectj/weaver/loadtime/IWeavingContext.java b/loadtime/src/org/aspectj/weaver/loadtime/IWeavingContext.java index 5b4a0c21c..8311f6e0c 100644 --- a/loadtime/src/org/aspectj/weaver/loadtime/IWeavingContext.java +++ b/loadtime/src/org/aspectj/weaver/loadtime/IWeavingContext.java @@ -39,12 +39,13 @@ public interface IWeavingContext { * In a non-OSGi environment, implementors should return null. * @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 index 000000000..9f4b1bc47 --- /dev/null +++ b/loadtime/testdata/META-INF/aop.xml @@ -0,0 +1,3 @@ + + + \ 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 index 000000000..7d5318219 --- /dev/null +++ b/loadtime/testsrc/org/aspectj/weaver/loadtime/WeavingContextTest.java @@ -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; + } + +} -- 2.39.5