Top Description Methods
org.springframework.core

public Interface SmartClassLoader

Imports
java.security.ProtectionDomain, org.springframework.lang.Nullable

Interface to be implemented by a reloading-aware ClassLoader (e.g. a Groovy-based ClassLoader). Detected for example by Spring's CGLIB proxy factory for making a caching decision.

If a ClassLoader does not implement this interface, then all the classes obtained from it should be considered as not reloadable (i.e. cacheable).

Author
Juergen Hoeller
Since
2.5.1

Method Summary

Modifier and TypeMethod and Description
public default ClassLoader

Returns:

the original ClassLoader (the same reference by default)
getOriginalClassLoader
()

Return the original ClassLoader for this SmartClassLoader, or potentially the present loader itself if it is self-sufficient.

public default boolean

Returns:

whether the class should be expected to appear in a reloaded version (with a different Class object) later on
isClassReloadable
(Class<?>
the class to check (usually loaded from this ClassLoader)
clazz
)

Determine whether the given class is reloadable (in this ClassLoader).

public default Class<?>

Returns:

the newly created class
publicDefineClass
(String
the name of the class
name
,
byte[]
the bytes defining the class
b
,
ProtectionDomain
the protection domain for the class, if any
protectionDomain
)

Define a custom class (typically a CGLIB proxy class) in this class loader.

Method Detail

getOriginalClassLoaderback to summary
public default ClassLoader getOriginalClassLoader()

Return the original ClassLoader for this SmartClassLoader, or potentially the present loader itself if it is self-sufficient.

The default implementation returns the local ClassLoader reference as-is. In case of a reloadable or other selectively overriding ClassLoader which commonly deals with unaffected classes from a base application class loader, this should get implemented to return the original ClassLoader that the present loader got derived from (e.g. through return getParent();).

This gets specifically used in Spring's AOP framework to determine the class loader for a specific proxy in case the target class has not been defined in the present class loader. In case of a reloadable class loader, we prefer the base application class loader for proxying general classes not defined in the reloadable class loader itself.

Returns:ClassLoader

the original ClassLoader (the same reference by default)

Since
5.3.5
See Also
ClassLoader#getParent(), org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
isClassReloadableback to summary
public default boolean isClassReloadable(Class<?> clazz)

Determine whether the given class is reloadable (in this ClassLoader).

Typically used to check whether the result may be cached (for this ClassLoader) or whether it should be reobtained every time. The default implementation always returns false.

Parameters
clazz:Class<?>

the class to check (usually loaded from this ClassLoader)

Returns:boolean

whether the class should be expected to appear in a reloaded version (with a different Class object) later on

publicDefineClassback to summary
public default Class<?> publicDefineClass(String name, byte[] b, ProtectionDomain protectionDomain)

Define a custom class (typically a CGLIB proxy class) in this class loader.

This is a public equivalent of the protected defineClass(String, byte[], int, int, ProtectionDomain) method in ClassLoader which is traditionally invoked via reflection. A concrete implementation in a custom class loader should simply delegate to that protected method in order to make classloader-specific definitions publicly available without "illegal access" warnings on JDK 9+: return defineClass(name, b, 0, b.length, protectionDomain). Note that the JDK 9+ Lookup#defineClass method does not support a custom target class loader for the new definition; it rather always defines the class in the same class loader as the lookup's context class.

Parameters
name:String

the name of the class

b:byte[]

the bytes defining the class

protectionDomain:ProtectionDomain

the protection domain for the class, if any

Returns:Class<?>

the newly created class

Exceptions
LinkageError:
in case of a bad class definition
SecurityException:
in case of an invalid definition attempt
UnsupportedOperationException:
in case of a custom definition attempt not being possible (thrown by the default implementation in this interface)
Since
5.3.4
See Also
ClassLoader#defineClass(String, byte[], int, int, ProtectionDomain)