You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WeakClassLoaderReference.java 2.9KB

15 years ago
12 years ago
15 years ago
12 years ago
15 years ago
12 years ago
15 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* *******************************************************************
  2. * Copyright (c) 2008 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.lang.ref.WeakReference;
  14. /**
  15. * Wraps a reference to a classloader inside a WeakReference. This should be used where we do not want the existence of a
  16. * classloader reference to prevent garbage collection of that classloader (and possibly an associated weaver instance in the case
  17. * of load time weaving).
  18. * <p>
  19. * In more detail:<br>
  20. * When load time weaving, the class Aj maintains a WeakHashMap from the classloader instance to a weaver instance. The aim is that
  21. * the weaver is around as long as the classloader is and should the classloader be dereferenced then the weaver can also be garbage
  22. * collected. The problem is that if there are many references to the classloader from within the weaver, these are considered hard
  23. * references and cause the classloader to be long lived - even if the user of the classloader has dereferenced it in their code.
  24. * The solution is that the weaver should use instances of WeakClassLoaderReference objects - so that when the users hard reference
  25. * to the classloader goes, nothing in the weaver will cause it to hang around. There is a big assertion here that the
  26. * WeakClassLoaderReference instances will not 'lose' their ClassLoader references until the top level ClassLoader reference is
  27. * null'd. This means there is no need to check for the null case on get() in this WeakReference logic below, because we shouldn't
  28. * be using this weaver if its associated ClassLoader has been collected. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=210470
  29. *
  30. *
  31. * @author Andy Clement, Abraham Nevado
  32. */
  33. public class WeakClassLoaderReference{
  34. protected final int hashcode;
  35. private final WeakReference loaderRef;
  36. public WeakClassLoaderReference(ClassLoader loader) {
  37. loaderRef = new WeakReference(loader);
  38. if(loader == null){
  39. // Bug: 363962
  40. // Check that ClassLoader is not null, for instance when loaded from BootStrapClassLoader
  41. hashcode = System.identityHashCode(this);
  42. }else{
  43. hashcode = loader.hashCode() * 37;
  44. }
  45. }
  46. public ClassLoader getClassLoader() {
  47. ClassLoader instance = (ClassLoader) loaderRef.get();
  48. // Assert instance!=null
  49. return instance;
  50. }
  51. public boolean equals(Object obj) {
  52. if (!(obj instanceof WeakClassLoaderReference))
  53. return false;
  54. WeakClassLoaderReference other = (WeakClassLoaderReference) obj;
  55. return (other.hashcode == hashcode);
  56. }
  57. public int hashCode() {
  58. return hashcode;
  59. }
  60. }