Klasse WeavedClassCache

java.lang.Object
org.aspectj.weaver.tools.cache.WeavedClassCache

public class WeavedClassCache extends Object
Manages a cache of weaved and generated classes similar to Eclipse Equinox, except designed to operate across multiple restarts of the JVM and with one cache per classloader; allowing URLClassLoaders with the same set of URI paths to share the same cache (with the default configuration).

To enable the default configuration two system properties must be set:

    "-Daj.weaving.cache.enabled=true"
    "-Daj.weaving.cache.dir=/some/directory"
 

The class cache is often something that application developers or containers would like to manage, so there are a few interfaces for overriding the default behavior and performing other management functions.

CacheBacking
Provides an interface for implementing a custom backing store for the cache; The default implementation in DefaultFileCacheBacking provides a naive file-backed cache. An alternate implementation may ignore caching until signaled explicitly by the application, or only cache files for a specific duration. This class delegates the locking and synchronization requirements to the CacheBacking implementation.

CacheKeyResolver
Provides methods for creating keys from classes to be cached and for creating the "scope" of the cache itself for a given classloader and aspect list. The default implementation is provided by DefaultCacheKeyResolver but an alternate implementation may want to associate a cache with a particular application running underneath a container.

This naive cache does not normally invalidate *any* classes; the interfaces above must be used to implement more intelligent behavior. Cache invalidation problems may occur in at least three scenarios:

  1. New aspects are added dynamically somewhere in the classloader hierarchy; affecting other classes elsewhere.
  2. Use of declare parent in aspects to change the type hierarchy; if the cache has not invalidated the right classes in the type hierarchy aspectj may not be reconstruct the class incorrectly.
  3. Similarly to (2), the addition of fields or methods on classes which have already been weaved and cached could have inter-type conflicts.
  • Felddetails

    • WEAVED_CLASS_CACHE_ENABLED

      public static final String WEAVED_CLASS_CACHE_ENABLED
      Siehe auch:
    • CACHE_IMPL

      public static final String CACHE_IMPL
      Siehe auch:
    • ZERO_BYTES

      public static final byte[] ZERO_BYTES
  • Konstruktordetails

  • Methodendetails

    • createCache

      public static WeavedClassCache createCache(ClassLoader loader, List<String> aspects, GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler)
      Creates a new cache using the resolver and backing returned by the DefaultCacheFactory.
      Parameter:
      loader - classloader for this cache
      aspects - list of aspects used by the WeavingAdapter
      existingClassHandler - the existing GeneratedClassHandler used by the weaver
      messageHandler - the existing messageHandler used by the weaver
      Gibt zurück:
    • getName

      public String getName()
    • setDefaultCacheFactory

      public static void setDefaultCacheFactory(CacheFactory factory)
      The Cache and be extended in two ways, through a specialized CacheKeyResolver and a specialized CacheBacking. The default factory used to create these classes can be set with this method. Since each weaver will create a cache, this method must be called before the weaver is first initialized.
      Parameter:
      factory -
    • createGeneratedCacheKey

      public CachedClassReference createGeneratedCacheKey(String className)
      Created a key for a generated class
      Parameter:
      className - ClassName, e.g. "com.foo.Bar"
      Gibt zurück:
      the cache key, or null if no caching should be performed
    • createCacheKey

      public CachedClassReference createCacheKey(String className, byte[] originalBytes)
      Create a key for a normal weaved class
      Parameter:
      className - ClassName, e.g. "com.foo.Bar"
      originalBytes - Original byte array of the class
      Gibt zurück:
      a cache key, or null if no caching should be performed
    • getCachingClassHandler

      public GeneratedClassHandler getCachingClassHandler()
      Returns a generated class handler which wraps the handler this cache was initialized with; this handler should be used to make sure that generated classes are added to the cache
    • isEnabled

      public static boolean isEnabled()
      Has caching been enabled through the System property, WEAVED_CLASS_CACHE_ENABLED
      Gibt zurück:
      true if caching is enabled
    • put

      public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes)
      Put a class in the cache
      Parameter:
      ref - reference to the entry, as created through createCacheKey
      classBytes - pre-weaving class bytes
      weavedBytes - bytes to cache
    • get

      public CachedClassEntry get(CachedClassReference ref, byte[] classBytes)
      Get a cache value
      Parameter:
      ref - reference to the cache entry, created through createCacheKey
      classBytes - Pre-weaving class bytes - required to ensure that cached aspects refer to an unchanged original class
      Gibt zurück:
      the CacheEntry, or null if no entry exists in the cache
    • ignore

      public void ignore(CachedClassReference ref, byte[] classBytes)
      Put a cache entry to indicate that the class should not be weaved; the original bytes of the class should be used.
      Parameter:
      ref - The cache reference
      classBytes - The un-weaved class bytes
    • remove

      public void remove(CachedClassReference ref)
      Invalidate a cache entry
      Parameter:
      ref -
    • clear

      public void clear()
      Clear the entire cache
    • getStats

      public CacheStatistics getStats()
      Get the statistics associated with this cache, or null if statistics have not been enabled.
      Gibt zurück:
    • getCaches

      public static List<WeavedClassCache> getCaches()
      Return a list of all WeavedClassCaches which have been initialized
      Gibt zurück:
    • error

      protected void error(String message, Throwable th)
    • error

      protected void error(String message)
    • info

      protected void info(String message)