Package org.eclipse.persistence.dynamic
Class DynamicClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- org.eclipse.persistence.dynamic.DynamicClassLoader
-
- Direct Known Subclasses:
DynamicRestClassLoader
public class DynamicClassLoader extends ClassLoader
This custom ClassLoader provides support for dynamically generating classes within an EclipseLink application using byte codes created using aDynamicClassWriter
. A DynamicClassLoader requires a parent or delegate class-loader which is provided to the constructor. This delegate class loader handles the lookup and storage of all created classes.- Author:
- dclarke, mnorman
- Since:
- EclipseLink 1.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DynamicClassLoader.EnumInfo
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,EclipseLinkClassWriter>
classWriters
Map ofDynamicClassWriter
used to dynamically create a class in thefindClass(String)
call.DynamicClassWriter
defaultWriter
Default writer to use if one is not specified.protected Map<String,DynamicClassLoader.EnumInfo>
enumInfoRegistry
-
Constructor Summary
Constructors Constructor Description DynamicClassLoader(ClassLoader delegate)
Create a DynamicClassLoader providing the delegate loader and leaving the defaultWriter asDynamicClassWriter
DynamicClassLoader(ClassLoader delegate, DynamicClassWriter writer)
Create a DynamicClassLoader providing the delegate loader and a defaultDynamicClassWriter
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addClass(String className)
Register a class to be dynamically created using the defaultDynamicClassWriter
.void
addClass(String className, Class<?> parentClass)
Register a class to be dynamically created using a copy of defaultDynamicClassWriter
but specifying a different parent class.void
addClass(String className, EclipseLinkClassWriter writer)
Register a class to be dynamically created using the providedDynamicClassWriter
.void
addEnum(String className, Object... literalLabels)
protected Class<?>
checkAssignable(Class<?> clz)
void
createDynamicAdapter(String className)
Create an adapter for givenclassName
Class<?>
createDynamicClass(String className)
Create a new dynamic entity type for the specified name assuming the use of the default writer and its default parent class.Class<?>
createDynamicClass(String className, Class<?> parentClass)
Create a new dynamic entity type for the specified name with the specified parent class.Class<?>
createDynamicClass(String className, DynamicClassWriter writer)
Create a dynamic class registering a writer and then forcing the provided class name to be loaded.void
createDynamicCollectionAdapter(String className)
Create a collection adapter for givenclassName
void
createDynamicReferenceAdapter(String className)
Create a reference for givenclassName
protected Class<?>
defineDynamicClass(String name, byte[] b)
Converts an array of bytes into an instance of classClass
.protected Class<?>
findClass(String className)
Create a new dynamic class if a ClassWriter is registered for the provided className.EclipseLinkClassWriter
getClassWriter(String className)
protected Map<String,EclipseLinkClassWriter>
getClassWriters()
DynamicClassWriter
getDefaultWriter()
static DynamicClassLoader
lookup(Session session)
Lookup the DynamicConversionManager for the given session.-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Field Detail
-
classWriters
protected Map<String,EclipseLinkClassWriter> classWriters
Map ofDynamicClassWriter
used to dynamically create a class in thefindClass(String)
call. The application must register classes using addClass or createDynameClass prior to thefindClass(String)
being invoked.The map of writers is maintained for the life of this DynamicClassLoader instance to ensure additional requests to create dynamic classes of the same name are properly verified. Duplicate requests for dynamic classes of the same name, same writer type, and the same parent class are permitted but different parent classes or different writer types are not.
-
enumInfoRegistry
protected Map<String,DynamicClassLoader.EnumInfo> enumInfoRegistry
-
defaultWriter
public DynamicClassWriter defaultWriter
Default writer to use if one is not specified.
-
-
Constructor Detail
-
DynamicClassLoader
public DynamicClassLoader(ClassLoader delegate)
Create a DynamicClassLoader providing the delegate loader and leaving the defaultWriter asDynamicClassWriter
-
DynamicClassLoader
public DynamicClassLoader(ClassLoader delegate, DynamicClassWriter writer)
Create a DynamicClassLoader providing the delegate loader and a defaultDynamicClassWriter
.
-
-
Method Detail
-
getDefaultWriter
public DynamicClassWriter getDefaultWriter()
-
getClassWriters
protected Map<String,EclipseLinkClassWriter> getClassWriters()
-
getClassWriter
public EclipseLinkClassWriter getClassWriter(String className)
-
addClass
public void addClass(String className)
Register a class to be dynamically created using the defaultDynamicClassWriter
.- See Also:
addClass(String, EclipseLinkClassWriter)
-
addClass
public void addClass(String className, Class<?> parentClass)
Register a class to be dynamically created using a copy of defaultDynamicClassWriter
but specifying a different parent class.- See Also:
addClass(String, EclipseLinkClassWriter)
-
addClass
public void addClass(String className, EclipseLinkClassWriter writer) throws DynamicException
Register a class to be dynamically created using the providedDynamicClassWriter
. The registered writer is used when thefindClass(String)
method is called back on this loader from theClassLoader.loadClass(String)
call.If a duplicate request is made for the same className and the writers are not compatible a
DynamicException
will be thrown. If the duplicate request contains a compatible writer then the second request is ignored as the class may already have been generated.- Throws:
DynamicException
- See Also:
findClass(String)
-
createDynamicClass
public Class<?> createDynamicClass(String className, DynamicClassWriter writer)
Create a dynamic class registering a writer and then forcing the provided class name to be loaded.
-
createDynamicClass
public Class<?> createDynamicClass(String className)
Create a new dynamic entity type for the specified name assuming the use of the default writer and its default parent class.
-
createDynamicClass
public Class<?> createDynamicClass(String className, Class<?> parentClass)
Create a new dynamic entity type for the specified name with the specified parent class.
-
createDynamicAdapter
public void createDynamicAdapter(String className)
Create an adapter for givenclassName
- Parameters:
className
-
-
createDynamicCollectionAdapter
public void createDynamicCollectionAdapter(String className)
Create a collection adapter for givenclassName
- Parameters:
className
-
-
createDynamicReferenceAdapter
public void createDynamicReferenceAdapter(String className)
Create a reference for givenclassName
- Parameters:
className
-
-
findClass
protected Class<?> findClass(String className) throws ClassNotFoundException
Create a new dynamic class if a ClassWriter is registered for the provided className. This code is single threaded to ensure only one class is created for a given name and that the ClassWriter is removed afterwards.- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
defineDynamicClass
protected Class<?> defineDynamicClass(String name, byte[] b)
Converts an array of bytes into an instance of classClass
. Before theClass
can be used it must be resolved.- Parameters:
name
-b
-- Throws:
ClassFormatError
-
lookup
public static DynamicClassLoader lookup(Session session)
Lookup the DynamicConversionManager for the given session. If the existing ConversionManager is not an instance of DynamicConversionManager then create a new one and replace the existing one.- Parameters:
session
-- Returns:
-
-