From b4a9f97fa9eb3b57aaff27fca62063185e76d0d6 Mon Sep 17 00:00:00 2001 From: aclement Date: Tue, 27 May 2008 18:50:34 +0000 Subject: [PATCH] 210470: preventing weaver leaks: use a weak classloader ref --- .../ReflectionBasedReferenceTypeDelegate.java | 7 ++++--- .../aspectj/weaver/reflect/ReflectionWorld.java | 15 ++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/weaver/src/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegate.java b/weaver/src/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegate.java index 01c16ef82..1e3628c47 100644 --- a/weaver/src/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegate.java +++ b/weaver/src/org/aspectj/weaver/reflect/ReflectionBasedReferenceTypeDelegate.java @@ -30,6 +30,7 @@ import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.SourceContextImpl; import org.aspectj.weaver.TypeVariable; import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.WeakClassLoaderReference; import org.aspectj.weaver.WeaverStateInfo; import org.aspectj.weaver.World; import org.aspectj.weaver.patterns.PerClause; @@ -42,10 +43,10 @@ import org.aspectj.weaver.patterns.PerClause; */ public class ReflectionBasedReferenceTypeDelegate implements ReferenceTypeDelegate { - private static final ClassLoader BootClassLoader = new URLClassLoader(new URL[0]); + private static final ClassLoader BootClassLoader = new URLClassLoader(new URL[0]);// ReflectionBasedReferenceTypeDelegate.class.getClassLoader(); protected Class myClass = null; - protected ClassLoader classLoader = null; + protected WeakClassLoaderReference classLoaderReference = null; private World world; private ReferenceType resolvedType; private ResolvedMember[] fields = null; @@ -63,7 +64,7 @@ public class ReflectionBasedReferenceTypeDelegate implements ReferenceTypeDelega this.myClass = aClass; this.resolvedType = aType; this.world = aWorld; - this.classLoader = (aClassLoader != null) ? aClassLoader : BootClassLoader; + this.classLoaderReference = new WeakClassLoaderReference((aClassLoader != null) ? aClassLoader : BootClassLoader); } protected Class getBaseClass() { diff --git a/weaver/src/org/aspectj/weaver/reflect/ReflectionWorld.java b/weaver/src/org/aspectj/weaver/reflect/ReflectionWorld.java index 7994d8f39..12107649c 100644 --- a/weaver/src/org/aspectj/weaver/reflect/ReflectionWorld.java +++ b/weaver/src/org/aspectj/weaver/reflect/ReflectionWorld.java @@ -25,6 +25,7 @@ import org.aspectj.weaver.ResolvedMember; import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.ResolvedTypeMunger; import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.WeakClassLoaderReference; import org.aspectj.weaver.World; import org.aspectj.weaver.AjAttribute.AdviceAttribute; import org.aspectj.weaver.patterns.Pointcut; @@ -38,23 +39,23 @@ import org.aspectj.weaver.patterns.PerClause.Kind; */ public class ReflectionWorld extends World implements IReflectionWorld { - private ClassLoader classLoader; + private WeakClassLoaderReference classLoaderReference; private AnnotationFinder annotationFinder; private ReflectionWorld() { super(); this.setMessageHandler(new ExceptionBasedMessageHandler()); setBehaveInJava5Way(LangUtil.is15VMOrGreater()); - this.classLoader = ReflectionWorld.class.getClassLoader(); - this.annotationFinder = makeAnnotationFinderIfAny(classLoader, this); + this.classLoaderReference = new WeakClassLoaderReference(ReflectionWorld.class.getClassLoader()); + this.annotationFinder = makeAnnotationFinderIfAny(classLoaderReference.getClassLoader(), this); } public ReflectionWorld(ClassLoader aClassLoader) { super(); this.setMessageHandler(new ExceptionBasedMessageHandler()); setBehaveInJava5Way(LangUtil.is15VMOrGreater()); - this.classLoader = aClassLoader; - this.annotationFinder = makeAnnotationFinderIfAny(classLoader, this); + this.classLoaderReference = new WeakClassLoaderReference(aClassLoader); + this.annotationFinder = makeAnnotationFinderIfAny(classLoaderReference.getClassLoader(), this); } public static AnnotationFinder makeAnnotationFinderIfAny(ClassLoader loader, World world) { @@ -78,7 +79,7 @@ public class ReflectionWorld extends World implements IReflectionWorld { } public ClassLoader getClassLoader() { - return this.classLoader; + return this.classLoaderReference.getClassLoader(); } public AnnotationFinder getAnnotationFinder() { @@ -107,7 +108,7 @@ public class ReflectionWorld extends World implements IReflectionWorld { * @see org.aspectj.weaver.World#resolveDelegate(org.aspectj.weaver.ReferenceType) */ protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) { - return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, this.classLoader); + return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, this.classLoaderReference.getClassLoader()); } /* (non-Javadoc) -- 2.39.5