Top Description Inners Fields Constructors Methods
java.security

public Class SecureClassLoader

extends ClassLoader
Class Inheritance
Known Direct Subclasses
jdk.internal.loader.BuiltinClassLoader, jdk.internal.loader.Loader, sun.reflect.misc.MethodUtil, java.net.URLClassLoader
Imports
sun.security.util.Debug, java.util.Map, .Objects, java.util.concurrent.ConcurrentHashMap, java.util.function.Function

This class extends ClassLoader with additional support for defining classes with an associated code source and permissions which are retrieved by the system policy by default.
Authors
Li Gong, Roland Schemers
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
private static class

Field Summary

Modifier and TypeField and Description
private final Map<SecureClassLoader.CodeSourceKey, ProtectionDomain>

Constructor Summary

AccessConstructor and Description
protected
SecureClassLoader(ClassLoader
the parent ClassLoader
parent
)

Creates a new SecureClassLoader using the specified parent class loader for delegation.

protected
SecureClassLoader()

Creates a new SecureClassLoader using the default parent class loader for delegation.

protected
SecureClassLoader(String
class loader name; or null if not named
name
,
ClassLoader
the parent class loader
parent
)

Creates a new SecureClassLoader of the specified name and using the specified parent class loader for delegation.

Method Summary

Modifier and TypeMethod and Description
protected final Class<?>

Returns:

the Class object created from the data, and optional CodeSource.
defineClass
(String
the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.
name
,
byte[]
the bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
int
the start offset in b of the class data
off
,
int
the length of the class data
len
,
CodeSource
the associated CodeSource, or null if none
cs
)

Converts an array of bytes into an instance of class Class, with an optional CodeSource.

protected final Class<?>

Returns:

the Class object created from the data, and optional CodeSource.
defineClass
(String
the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.
name
,
ByteBuffer
the bytes that make up the class data. The bytes from positions b.position() through b.position() + b.limit() -1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
CodeSource
the associated CodeSource, or null if none
cs
)

Converts a ByteBuffer into an instance of class Class, with an optional CodeSource.

protected PermissionCollection

Returns:

the permissions granted to the codesource.
getPermissions
(CodeSource
the codesource.
codesource
)

Returns the permissions for the given CodeSource object.

private ProtectionDomain
private void
resetArchivedStates()

Hides java.lang.ClassLoader.resetArchivedStates.

Called by the VM, during -Xshare:dump

Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindClassfindClassfindLibraryfindLoadedClassfindResourcefindResourcefindResourcesfindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetResourcegetResourceAsStreamgetResourcesgetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners

Field Detail

pdcacheback to summary
private final Map<SecureClassLoader.CodeSourceKey, ProtectionDomain> pdcache

Constructor Detail

SecureClassLoaderback to summary
protected SecureClassLoader(ClassLoader parent)

Creates a new SecureClassLoader using the specified parent class loader for delegation.

If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.

Parameters
parent:ClassLoader

the parent ClassLoader

Exceptions
SecurityException:
if a security manager exists and its checkCreateClassLoader method doesn't allow creation of a class loader.
See Also
SecurityManager#checkCreateClassLoader
SecureClassLoaderback to summary
protected SecureClassLoader()

Creates a new SecureClassLoader using the default parent class loader for delegation.

If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.

Exceptions
SecurityException:
if a security manager exists and its checkCreateClassLoader method doesn't allow creation of a class loader.
See Also
SecurityManager#checkCreateClassLoader
SecureClassLoaderback to summary
protected SecureClassLoader(String name, ClassLoader parent)

Creates a new SecureClassLoader of the specified name and using the specified parent class loader for delegation.

Parameters
name:String

class loader name; or null if not named

parent:ClassLoader

the parent class loader

Exceptions
IllegalArgumentException:
if the given name is empty.
SecurityException:
if a security manager exists and its SecurityManager#checkCreateClassLoader() method doesn't allow creation of a class loader.
Since
9

Method Detail

defineClassback to summary
protected final Class<?> defineClass(String name, byte[] b, int off, int len, CodeSource cs)

Converts an array of bytes into an instance of class Class, with an optional CodeSource. Before the class can be used it must be resolved.

If a non-null CodeSource is supplied a ProtectionDomain is constructed and associated with the class being defined.

Parameters
name:String

the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.

b:byte[]

the bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.

off:int

the start offset in b of the class data

len:int

the length of the class data

cs:CodeSource

the associated CodeSource, or null if none

Returns:Class<?>

the Class object created from the data, and optional CodeSource.

Exceptions
ClassFormatError:
if the data did not contain a valid class
IndexOutOfBoundsException:
if either off or len is negative, or if off+len is greater than b.length.
SecurityException:
if an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if the class name begins with "java.".
defineClassback to summary
protected final Class<?> defineClass(String name, ByteBuffer b, CodeSource cs)

Converts a ByteBuffer into an instance of class Class, with an optional CodeSource. Before the class can be used it must be resolved.

If a non-null CodeSource is supplied a ProtectionDomain is constructed and associated with the class being defined.

Parameters
name:String

the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.

b:ByteBuffer

the bytes that make up the class data. The bytes from positions b.position() through b.position() + b.limit() -1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.

cs:CodeSource

the associated CodeSource, or null if none

Returns:Class<?>

the Class object created from the data, and optional CodeSource.

Exceptions
ClassFormatError:
if the data did not contain a valid class
SecurityException:
if an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if the class name begins with "java.".
Since
1.5
getPermissionsback to summary
protected PermissionCollection getPermissions(CodeSource codesource)

Returns the permissions for the given CodeSource object.

This method is invoked by the defineClass method which takes a CodeSource as an argument when it is constructing the ProtectionDomain for the class being defined.

Parameters
codesource:CodeSource

the codesource.

Returns:PermissionCollection

the permissions granted to the codesource.

getProtectionDomainback to summary
private ProtectionDomain getProtectionDomain(CodeSource cs)
resetArchivedStatesback to summary
private void resetArchivedStates()

Hides java.lang.ClassLoader.resetArchivedStates.

Called by the VM, during -Xshare:dump

java.security back to summary

private final Record SecureClassLoader.CodeSourceKey

extends Record
Class Inheritance
Record Components
cs:CodeSource

Field Summary

Modifier and TypeField and Description
private final CodeSource
cs

Record Component accessed by cs().

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public CodeSource
cs()

Record Component getter of cs.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

csback to summary
private final CodeSource cs

Record Component accessed by cs().

Constructor Detail

CodeSourceKeyback to summary
private CodeSourceKey(CodeSource cs)

Method Detail

csback to summary
public CodeSource cs()

Record Component getter of cs.

equalsback to summary
public boolean equals(Object obj)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this record is equal to the argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Returns:int

a hash code value for this record.

Annotations
@Override
toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

Returns:String

a string representation of the object.

java.security back to summary

private Class SecureClassLoader.DebugHolder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final Debug

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

debugback to summary
private static final Debug debug

Constructor Detail

DebugHolderback to summary
private DebugHolder()