public abstract class AbstractClassGenerator<T>
extends java.lang.Object
ClassLoader
, name of the generated class, and transformations
applied before generation.Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractClassGenerator.ClassLoaderData |
protected static class |
AbstractClassGenerator.Source |
Modifier and Type | Field and Description |
---|---|
private boolean |
attemptLoad |
private static java.util.Map<java.lang.ClassLoader,AbstractClassGenerator.ClassLoaderData> |
CACHE |
private java.lang.ClassLoader |
classLoader |
private java.lang.String |
className |
private java.lang.Class |
contextClass |
private static java.lang.ThreadLocal |
CURRENT |
private static boolean |
DEFAULT_USE_CACHE |
private java.lang.Object |
key |
private java.lang.String |
namePrefix |
private NamingPolicy |
namingPolicy |
private AbstractClassGenerator.Source |
source |
private GeneratorStrategy |
strategy |
private boolean |
useCache |
Modifier | Constructor and Description |
---|---|
protected |
AbstractClassGenerator(AbstractClassGenerator.Source source) |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
create(java.lang.Object key) |
protected abstract java.lang.Object |
firstInstance(java.lang.Class type) |
protected java.lang.Class |
generate(AbstractClassGenerator.ClassLoaderData data) |
private java.lang.String |
generateClassName(Predicate nameTestPredicate) |
boolean |
getAttemptLoad() |
java.lang.ClassLoader |
getClassLoader() |
protected java.lang.String |
getClassName() |
static AbstractClassGenerator |
getCurrent()
Used internally by CGLIB.
|
protected abstract java.lang.ClassLoader |
getDefaultClassLoader() |
NamingPolicy |
getNamingPolicy() |
protected java.security.ProtectionDomain |
getProtectionDomain()
Returns the protection domain to use when defining the class.
|
GeneratorStrategy |
getStrategy() |
boolean |
getUseCache() |
protected abstract java.lang.Object |
nextInstance(java.lang.Object instance) |
void |
setAttemptLoad(boolean attemptLoad)
If set, CGLIB will attempt to load classes from the specified
ClassLoader before generating them. |
void |
setClassLoader(java.lang.ClassLoader classLoader)
Set the
ClassLoader in which the class will be generated. |
private void |
setClassName(java.lang.String className) |
void |
setContextClass(java.lang.Class contextClass) |
protected void |
setNamePrefix(java.lang.String namePrefix) |
void |
setNamingPolicy(NamingPolicy namingPolicy)
Override the default naming policy.
|
void |
setStrategy(GeneratorStrategy strategy)
Set the strategy to use to create the bytecode from this generator.
|
void |
setUseCache(boolean useCache)
Whether use and update the static cache of generated classes
for a class with the same properties.
|
protected java.lang.Object |
unwrapCachedValue(T cached) |
protected T |
wrapCachedClass(java.lang.Class klass) |
private static final java.lang.ThreadLocal CURRENT
private static volatile java.util.Map<java.lang.ClassLoader,AbstractClassGenerator.ClassLoaderData> CACHE
private static final boolean DEFAULT_USE_CACHE
private GeneratorStrategy strategy
private NamingPolicy namingPolicy
private AbstractClassGenerator.Source source
private java.lang.ClassLoader classLoader
private java.lang.Class contextClass
private java.lang.String namePrefix
private java.lang.Object key
private boolean useCache
private java.lang.String className
private boolean attemptLoad
protected AbstractClassGenerator(AbstractClassGenerator.Source source)
protected T wrapCachedClass(java.lang.Class klass)
protected java.lang.Object unwrapCachedValue(T cached)
protected void setNamePrefix(java.lang.String namePrefix)
protected final java.lang.String getClassName()
private void setClassName(java.lang.String className)
private java.lang.String generateClassName(Predicate nameTestPredicate)
public void setClassLoader(java.lang.ClassLoader classLoader)
ClassLoader
in which the class will be generated.
Concrete subclasses of AbstractClassGenerator
(such as Enhancer
)
will try to choose an appropriate default if this is unset.
Classes are cached per-ClassLoader
using a WeakHashMap
, to allow
the generated classes to be removed when the associated loader is garbage collected.
classLoader
- the loader to generate the new class with, or null to use the defaultpublic void setContextClass(java.lang.Class contextClass)
public void setNamingPolicy(NamingPolicy namingPolicy)
namingPolicy
- the custom policy, or null to use the defaultDefaultNamingPolicy
public NamingPolicy getNamingPolicy()
setNamingPolicy(NamingPolicy)
public void setUseCache(boolean useCache)
true
.public boolean getUseCache()
setUseCache(boolean)
public void setAttemptLoad(boolean attemptLoad)
ClassLoader
before generating them. Because generated
class names are not guaranteed to be unique, the default is false
.public boolean getAttemptLoad()
public void setStrategy(GeneratorStrategy strategy)
DefaultGeneratorStrategy
is used.public GeneratorStrategy getStrategy()
setStrategy(GeneratorStrategy)
public static AbstractClassGenerator getCurrent()
AbstractClassGenerator
that is being used to generate a class in the current thread.public java.lang.ClassLoader getClassLoader()
protected abstract java.lang.ClassLoader getDefaultClassLoader()
protected java.security.ProtectionDomain getProtectionDomain()
Default implementation returns null
for using a default protection domain. Sub-classes may
override to use a more specific protection domain.
null
for using a default)protected java.lang.Object create(java.lang.Object key)
protected java.lang.Class generate(AbstractClassGenerator.ClassLoaderData data)
protected abstract java.lang.Object firstInstance(java.lang.Class type) throws java.lang.Exception
java.lang.Exception
protected abstract java.lang.Object nextInstance(java.lang.Object instance) throws java.lang.Exception
java.lang.Exception