Package net.sf.cglib.core
Class AbstractClassGenerator<T>
- java.lang.Object
-
- net.sf.cglib.core.AbstractClassGenerator<T>
-
- All Implemented Interfaces:
ClassGenerator
- Direct Known Subclasses:
BeanCopier.Generator
,BeanGenerator
,BeanMap.Generator
,BulkBean.Generator
,ConstructorDelegate.Generator
,Enhancer
,FastClass.Generator
,ImmutableBean.Generator
,InterfaceMaker
,KeyFactory.Generator
,MethodDelegate.Generator
,Mixin.Generator
,MulticastDelegate.Generator
,ParallelSorter.Generator
,StringSwitcher.Generator
public abstract class AbstractClassGenerator<T> extends java.lang.Object implements ClassGenerator
Abstract class for all code-generating CGLIB utilities. In addition to caching generated classes for performance, it provides hooks for customizing theClassLoader
, name of the generated class, and transformations applied before generation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractClassGenerator.ClassLoaderData
protected static class
AbstractClassGenerator.Source
-
Field Summary
Fields Modifier and Type Field 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 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
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractClassGenerator(AbstractClassGenerator.Source source)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method 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 specifiedClassLoader
before generating them.void
setClassLoader(java.lang.ClassLoader classLoader)
Set theClassLoader
in which the class will be generated.private void
setClassName(java.lang.String className)
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)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sf.cglib.core.ClassGenerator
generateClass
-
-
-
-
Field Detail
-
CURRENT
private static final java.lang.ThreadLocal CURRENT
-
CACHE
private static volatile java.util.Map<java.lang.ClassLoader,AbstractClassGenerator.ClassLoaderData> CACHE
-
DEFAULT_USE_CACHE
private static final boolean DEFAULT_USE_CACHE
-
strategy
private GeneratorStrategy strategy
-
namingPolicy
private NamingPolicy namingPolicy
-
source
private AbstractClassGenerator.Source source
-
classLoader
private java.lang.ClassLoader classLoader
-
namePrefix
private java.lang.String namePrefix
-
key
private java.lang.Object key
-
useCache
private boolean useCache
-
className
private java.lang.String className
-
attemptLoad
private boolean attemptLoad
-
-
Constructor Detail
-
AbstractClassGenerator
protected AbstractClassGenerator(AbstractClassGenerator.Source source)
-
-
Method Detail
-
wrapCachedClass
protected T wrapCachedClass(java.lang.Class klass)
-
unwrapCachedValue
protected java.lang.Object unwrapCachedValue(T cached)
-
setNamePrefix
protected void setNamePrefix(java.lang.String namePrefix)
-
getClassName
protected final java.lang.String getClassName()
-
setClassName
private void setClassName(java.lang.String className)
-
generateClassName
private java.lang.String generateClassName(Predicate nameTestPredicate)
-
setClassLoader
public void setClassLoader(java.lang.ClassLoader classLoader)
Set theClassLoader
in which the class will be generated. Concrete subclasses ofAbstractClassGenerator
(such asEnhancer
) will try to choose an appropriate default if this is unset.Classes are cached per-
ClassLoader
using aWeakHashMap
, to allow the generated classes to be removed when the associated loader is garbage collected.- Parameters:
classLoader
- the loader to generate the new class with, or null to use the default
-
setNamingPolicy
public void setNamingPolicy(NamingPolicy namingPolicy)
Override the default naming policy.- Parameters:
namingPolicy
- the custom policy, or null to use the default- See Also:
DefaultNamingPolicy
-
getNamingPolicy
public NamingPolicy getNamingPolicy()
-
setUseCache
public void setUseCache(boolean useCache)
Whether use and update the static cache of generated classes for a class with the same properties. Default istrue
.
-
getUseCache
public boolean getUseCache()
- See Also:
setUseCache(boolean)
-
setAttemptLoad
public void setAttemptLoad(boolean attemptLoad)
If set, CGLIB will attempt to load classes from the specifiedClassLoader
before generating them. Because generated class names are not guaranteed to be unique, the default isfalse
.
-
getAttemptLoad
public boolean getAttemptLoad()
-
setStrategy
public void setStrategy(GeneratorStrategy strategy)
Set the strategy to use to create the bytecode from this generator. By default an instance of {@see DefaultGeneratorStrategy} is used.
-
getStrategy
public GeneratorStrategy getStrategy()
-
getCurrent
public static AbstractClassGenerator getCurrent()
Used internally by CGLIB. Returns theAbstractClassGenerator
that is being used to generate a class in the current thread.
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
-
getDefaultClassLoader
protected abstract java.lang.ClassLoader getDefaultClassLoader()
-
getProtectionDomain
protected java.security.ProtectionDomain getProtectionDomain()
Returns the protection domain to use when defining the class.Default implementation returns
null
for using a default protection domain. Sub-classes may override to use a more specific protection domain.- Returns:
- the protection domain (
null
for using a default)
-
create
protected java.lang.Object create(java.lang.Object key)
-
generate
protected java.lang.Class generate(AbstractClassGenerator.ClassLoaderData data)
-
firstInstance
protected abstract java.lang.Object firstInstance(java.lang.Class type) throws java.lang.Exception
- Throws:
java.lang.Exception
-
nextInstance
protected abstract java.lang.Object nextInstance(java.lang.Object instance) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-