public class WeavedClassCache
extends java.lang.Object
"-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
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
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CACHE_IMPL |
static java.lang.String |
WEAVED_CLASS_CACHE_ENABLED |
static byte[] |
ZERO_BYTES |
Modifier | Constructor and Description |
---|---|
protected |
WeavedClassCache(GeneratedClassHandler existingClassHandler,
IMessageHandler messageHandler,
java.lang.String name,
CacheBacking backing,
CacheKeyResolver resolver) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear the entire cache
|
static WeavedClassCache |
createCache(java.lang.ClassLoader loader,
java.util.List<java.lang.String> aspects,
GeneratedClassHandler existingClassHandler,
IMessageHandler messageHandler)
Creates a new cache using the resolver and backing returned by the DefaultCacheFactory.
|
CachedClassReference |
createCacheKey(java.lang.String className,
byte[] originalBytes)
Create a key for a normal weaved class
|
CachedClassReference |
createGeneratedCacheKey(java.lang.String className)
Created a key for a generated class
|
protected void |
error(java.lang.String message) |
protected void |
error(java.lang.String message,
java.lang.Throwable th) |
CachedClassEntry |
get(CachedClassReference ref,
byte[] classBytes)
Get a cache value
|
static java.util.List<WeavedClassCache> |
getCaches()
Return a list of all WeavedClassCaches which have been initialized
|
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
|
java.lang.String |
getName() |
CacheStatistics |
getStats()
Get the statistics associated with this cache, or
null if statistics have not been enabled.
|
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.
|
protected void |
info(java.lang.String message) |
static boolean |
isEnabled()
Has caching been enabled through the System property,
WEAVED_CLASS_CACHE_ENABLED
|
void |
put(CachedClassReference ref,
byte[] classBytes,
byte[] weavedBytes)
Put a class in the cache
|
void |
remove(CachedClassReference ref)
Invalidate a cache entry
|
static void |
setDefaultCacheFactory(CacheFactory factory)
The Cache and be extended in two ways, through a specialized CacheKeyResolver and
a specialized CacheBacking.
|
public static final java.lang.String WEAVED_CLASS_CACHE_ENABLED
public static final java.lang.String CACHE_IMPL
public static final byte[] ZERO_BYTES
protected WeavedClassCache(GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler, java.lang.String name, CacheBacking backing, CacheKeyResolver resolver)
public static WeavedClassCache createCache(java.lang.ClassLoader loader, java.util.List<java.lang.String> aspects, GeneratedClassHandler existingClassHandler, IMessageHandler messageHandler)
loader
- classloader for this cacheaspects
- list of aspects used by the WeavingAdapterexistingClassHandler
- the existing GeneratedClassHandler used by the weavermessageHandler
- the existing messageHandler used by the weaverpublic java.lang.String getName()
public static void setDefaultCacheFactory(CacheFactory factory)
factory
- public CachedClassReference createGeneratedCacheKey(java.lang.String className)
className
- ClassName, e.g. "com.foo.Bar"public CachedClassReference createCacheKey(java.lang.String className, byte[] originalBytes)
className
- ClassName, e.g. "com.foo.Bar"originalBytes
- Original byte array of the classpublic GeneratedClassHandler getCachingClassHandler()
public static boolean isEnabled()
public void put(CachedClassReference ref, byte[] classBytes, byte[] weavedBytes)
ref
- reference to the entry, as created through createCacheKeyclassBytes
- pre-weaving class bytesweavedBytes
- bytes to cachepublic CachedClassEntry get(CachedClassReference ref, byte[] classBytes)
ref
- reference to the cache entry, created through createCacheKeyclassBytes
- Pre-weaving class bytes - required to ensure that
cached aspects refer to an unchanged original classpublic void ignore(CachedClassReference ref, byte[] classBytes)
ref
- The cache referenceclassBytes
- The un-weaved class bytespublic void remove(CachedClassReference ref)
ref
- public void clear()
public CacheStatistics getStats()
public static java.util.List<WeavedClassCache> getCaches()
protected void error(java.lang.String message, java.lang.Throwable th)
protected void error(java.lang.String message)
protected void info(java.lang.String message)