Top Description Inners Fields Constructors Methods
org.springframework.cglib.core

public abstract Class AbstractClassGenerator<T>

extends Object
implements ClassGenerator
Class Inheritance
All Implemented Interfaces
org.springframework.cglib.core.ClassGenerator
Known Direct Subclasses
org.springframework.cglib.core.KeyFactory.Generator, org.springframework.cglib.proxy.Enhancer, org.springframework.cglib.proxy.InterfaceMaker, org.springframework.cglib.proxy.Mixin.Generator, org.springframework.cglib.reflect.ConstructorDelegate.Generator, org.springframework.cglib.reflect.FastClass.Generator, org.springframework.cglib.reflect.MethodDelegate.Generator, org.springframework.cglib.reflect.MulticastDelegate.Generator, org.springframework.cglib.util.ParallelSorter.Generator, org.springframework.cglib.util.StringSwitcher.Generator, org.springframework.cglib.beans.BeanCopier.Generator, org.springframework.cglib.beans.BeanGenerator, org.springframework.cglib.beans.BeanMap.Generator, org.springframework.cglib.beans.BulkBean.Generator, org.springframework.cglib.beans.ImmutableBean.Generator
Annotations
@SuppressWarnings:rawtypes, unchecked
Imports
java.lang.ref.WeakReference, java.security.ProtectionDomain, java.util.HashSet, .Map, .Set, .WeakHashMap, org.springframework.asm.ClassReader, org.springframework.cglib.core.internal.Function, .LoadingCache

Abstract class for all code-generating CGLIB utilities. In addition to caching generated classes for performance, it provides hooks for customizing the ClassLoader, name of the generated class, and transformations applied before generation.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
protected static class

Field Summary

Modifier and TypeField and Description
private boolean
private static volatile Map<ClassLoader, AbstractClassGenerator.ClassLoaderData>
private ClassLoader
private String
private Class<T>
private static final ThreadLocal<T>
private static final boolean
private static final boolean
private Object
private String
private NamingPolicy
private AbstractClassGenerator.Source
private GeneratorStrategy
private boolean

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected Object
protected abstract Object
protected Class<T>
private String
generateClassName(Predicate nameTestPredicate)

public boolean
public ClassLoader
protected final String
public static AbstractClassGenerator<T>
getCurrent()

Used internally by CGLIB.

protected abstract ClassLoader
public NamingPolicy
protected ProtectionDomain

Returns:

the protection domain (null for using a default)
getProtectionDomain
()

Returns the protection domain to use when defining the class.

public GeneratorStrategy
public boolean
protected abstract Object
nextInstance(Object instance)

public void
setAttemptLoad(boolean attemptLoad)

If set, CGLIB will attempt to load classes from the specified ClassLoader before generating them.

public void
setClassLoader(ClassLoader
the loader to generate the new class with, or null to use the default
classLoader
)

Set the ClassLoader in which the class will be generated.

private void
setClassName(String className)

public void
setContextClass(Class<T> contextClass)

protected void
setNamePrefix(String namePrefix)

public void
setNamingPolicy(NamingPolicy
the custom policy, or null to use the default
namingPolicy
)

Override the default naming policy.

public void
setStrategy(GeneratorStrategy strategy)

Set the strategy to use to create the bytecode from this generator.

public void
setUseCache(boolean useCache)

Whether use and update the static cache of generated classes for a class with the same properties.

protected Object
protected T
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attemptLoadback to summary
private boolean attemptLoad
CACHEback to summary
private static volatile Map<ClassLoader, AbstractClassGenerator.ClassLoaderData> CACHE
classLoaderback to summary
private ClassLoader classLoader
classNameback to summary
private String className
contextClassback to summary
private Class<T> contextClass
CURRENTback to summary
private static final ThreadLocal<T> CURRENT
DEFAULT_USE_CACHEback to summary
private static final boolean DEFAULT_USE_CACHE
inNativeImageback to summary
private static final boolean inNativeImage
keyback to summary
private Object key
namePrefixback to summary
private String namePrefix
namingPolicyback to summary
private NamingPolicy namingPolicy
sourceback to summary
private AbstractClassGenerator.Source source
strategyback to summary
private GeneratorStrategy strategy
useCacheback to summary
private boolean useCache

Constructor Detail

AbstractClassGeneratorback to summary
protected AbstractClassGenerator(AbstractClassGenerator.Source source)

Method Detail

createback to summary
protected Object create(Object key)
firstInstanceback to summary
protected abstract Object firstInstance(Class<T> type) throws Exception
generateback to summary
protected Class<T> generate(AbstractClassGenerator.ClassLoaderData data)
generateClassNameback to summary
private String generateClassName(Predicate nameTestPredicate)
getAttemptLoadback to summary
public boolean getAttemptLoad()
getClassLoaderback to summary
public ClassLoader getClassLoader()
getClassNameback to summary
protected final String getClassName()
getCurrentback to summary
public static AbstractClassGenerator<T> getCurrent()

Used internally by CGLIB. Returns the AbstractClassGenerator that is being used to generate a class in the current thread.

getDefaultClassLoaderback to summary
protected abstract ClassLoader getDefaultClassLoader()
getNamingPolicyback to summary
public NamingPolicy getNamingPolicy()
See Also
setNamingPolicy
getProtectionDomainback to summary
protected 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:ProtectionDomain

the protection domain (null for using a default)

getStrategyback to summary
public GeneratorStrategy getStrategy()
See Also
setStrategy
getUseCacheback to summary
public boolean getUseCache()
See Also
setUseCache
nextInstanceback to summary
protected abstract Object nextInstance(Object instance) throws Exception
setAttemptLoadback to summary
public void setAttemptLoad(boolean attemptLoad)

If set, CGLIB will attempt to load classes from the specified ClassLoader before generating them. Because generated class names are not guaranteed to be unique, the default is false.

setClassLoaderback to summary
public void setClassLoader(ClassLoader classLoader)

Set the 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.

Parameters
classLoader:ClassLoader

the loader to generate the new class with, or null to use the default

setClassNameback to summary
private void setClassName(String className)
setContextClassback to summary
public void setContextClass(Class<T> contextClass)
setNamePrefixback to summary
protected void setNamePrefix(String namePrefix)
setNamingPolicyback to summary
public void setNamingPolicy(NamingPolicy namingPolicy)

Override the default naming policy.

Parameters
namingPolicy:NamingPolicy

the custom policy, or null to use the default

See Also
DefaultNamingPolicy
setStrategyback to summary
public void setStrategy(GeneratorStrategy strategy)

Set the strategy to use to create the bytecode from this generator. By default an instance of DefaultGeneratorStrategy is used.

setUseCacheback to summary
public void setUseCache(boolean useCache)

Whether use and update the static cache of generated classes for a class with the same properties. Default is true.

unwrapCachedValueback to summary
protected Object unwrapCachedValue(T cached)
wrapCachedClassback to summary
protected T wrapCachedClass(Class<T> klass)
org.springframework.cglib.core back to summary

protected Class AbstractClassGenerator.ClassLoaderData

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.cglib.core.AbstractClassGenerator.ClassLoaderData

Field Summary

Modifier and TypeField and Description
private final WeakReference<ClassLoader>
classLoader

Note

ClassLoaderData object is stored as a value of WeakHashMap<ClassLoader, ...

private final LoadingCache<AbstractClassGenerator<T>, Object, Object>
generatedClasses

AbstractClassGenerator here holds "cache key" (e.g. org.springframework.cglib.proxy.Enhancer configuration), and the value is the generated class plus some additional values (see unwrapCachedValue(Object).

private static final Function<AbstractClassGenerator<T>, Object>
private final Set<String>
private final Predicate

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
get(AbstractClassGenerator<T> gen, boolean useCache)

public ClassLoader
public Predicate
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classLoaderback to summary
private final WeakReference<ClassLoader> classLoader

Note

ClassLoaderData object is stored as a value of WeakHashMap<ClassLoader, ...> thus this classLoader reference should be weak otherwise it would make classLoader strongly reachable and alive forever. Reference queue is not required since the cleanup is handled by WeakHashMap.

generatedClassesback to summary
private final LoadingCache<AbstractClassGenerator<T>, Object, Object> generatedClasses

AbstractClassGenerator here holds "cache key" (e.g. org.springframework.cglib.proxy.Enhancer configuration), and the value is the generated class plus some additional values (see unwrapCachedValue(Object).

The generated classes can be reused as long as their classloader is reachable.

Note

the only way to access a class is to find it through generatedClasses cache, thus the key should not expire as long as the class itself is alive (its classloader is alive).

GET_KEYback to summary
private static final Function<AbstractClassGenerator<T>, Object> GET_KEY
reservedClassNamesback to summary
private final Set<String> reservedClassNames
uniqueNamePredicateback to summary
private final Predicate uniqueNamePredicate

Constructor Detail

ClassLoaderDataback to summary
public ClassLoaderData(ClassLoader classLoader)

Method Detail

getback to summary
public Object get(AbstractClassGenerator<T> gen, boolean useCache)
getClassLoaderback to summary
public ClassLoader getClassLoader()
getUniqueNamePredicateback to summary
public Predicate getUniqueNamePredicate()
reserveNameback to summary
public void reserveName(String name)
org.springframework.cglib.core back to summary

protected Class AbstractClassGenerator.Source

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv String

Constructor Summary

AccessConstructor and Description
public
Source(String name)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nameback to summary
pack-priv String name

Constructor Detail

Sourceback to summary
public Source(String name)