Share via


ClassLoader.DefineClass Method

Definition

Overloads

DefineClass(Byte[], Int32, Int32)
Obsolete.

Converts an array of bytes into an instance of class Class.

DefineClass(String, ByteBuffer, ProtectionDomain)

Converts a java.nio.ByteBuffer <tt>ByteBuffer</tt> into an instance of class Class, with an optional ProtectionDomain.

DefineClass(String, Byte[], Int32, Int32)

Converts an array of bytes into an instance of class Class.

DefineClass(String, Byte[], Int32, Int32, ProtectionDomain)

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

DefineClass(Byte[], Int32, Int32)

Caution

deprecated

Converts an array of bytes into an instance of class Class.

[Android.Runtime.Register("defineClass", "([BII)Ljava/lang/Class;", "")]
[System.Obsolete("deprecated")]
protected Java.Lang.Class? DefineClass (byte[]? b, int off, int len);
[<Android.Runtime.Register("defineClass", "([BII)Ljava/lang/Class;", "")>]
[<System.Obsolete("deprecated")>]
member this.DefineClass : byte[] * int * int -> Java.Lang.Class

Parameters

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 <cite>The Java&trade; Virtual Machine Specification</cite>.

off
Int32

The start offset in b of the class data

len
Int32

The length of the class data

Returns

The Class object that was created from the specified class data

Attributes

Exceptions

if classRep does not contain a valid class.

if offset , length or if offset + length is greater than the length of classRep.

Remarks

Converts an array of bytes into an instance of class Class. Before the Class can be used it must be resolved. This method is deprecated in favor of the version that takes a binary name as its first argument, and is more secure.

This member is deprecated. Replaced by #defineClass(String, byte[], int, int) defineClass(String, byte[], int, int)

Java documentation for java.lang.ClassLoader.defineClass(byte[], int, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

DefineClass(String, ByteBuffer, ProtectionDomain)

Converts a java.nio.ByteBuffer <tt>ByteBuffer</tt> into an instance of class Class, with an optional ProtectionDomain.

[Android.Runtime.Register("defineClass", "(Ljava/lang/String;Ljava/nio/ByteBuffer;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", "")]
protected Java.Lang.Class? DefineClass (string? name, Java.Nio.ByteBuffer? b, Java.Security.ProtectionDomain? protectionDomain);
[<Android.Runtime.Register("defineClass", "(Ljava/lang/String;Ljava/nio/ByteBuffer;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", "")>]
member this.DefineClass : string * Java.Nio.ByteBuffer * Java.Security.ProtectionDomain -> Java.Lang.Class

Parameters

name
String

The expected binary name. of the class, or null if not known

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 <cite>The Java&trade; Virtual Machine Specification</cite>.

protectionDomain
ProtectionDomain

The ProtectionDomain of the class, or null.

Returns

The Class object created from the data, and optional ProtectionDomain.

Attributes

Exceptions

if b does not contain a valid class.

if className is not equal to the name of the class contained in b.

Remarks

Converts a java.nio.ByteBuffer <tt>ByteBuffer</tt> into an instance of class Class, with an optional ProtectionDomain. If the domain is null, then a default domain will be assigned to the class as specified in the documentation for #defineClass(String, byte[], int, int). Before the class can be used it must be resolved.

The rules about the first class defined in a package determining the set of certificates for the package, and the restrictions on class names are identical to those specified in the documentation for #defineClass(String, byte[], int, int, ProtectionDomain).

An invocation of this method of the form cl.defineClass(name,bBuffer,pd) yields exactly the same result as the statements

...<br> byte[] temp = new byte[bBuffer.java.nio.ByteBuffer#remaining remaining()];<br> bBuffer.java.nio.ByteBuffer#get(byte[]) get(temp);<br> return #defineClass(String, byte[], int, int, ProtectionDomain) cl.defineClass(name, temp, 0, temp.length, pd);<br>

Added in 1.5.

Java documentation for java.lang.ClassLoader.defineClass(java.lang.String, java.nio.ByteBuffer, java.security.ProtectionDomain).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

DefineClass(String, Byte[], Int32, Int32)

Converts an array of bytes into an instance of class Class.

[Android.Runtime.Register("defineClass", "(Ljava/lang/String;[BII)Ljava/lang/Class;", "")]
protected Java.Lang.Class? DefineClass (string? name, byte[]? b, int off, int len);
[<Android.Runtime.Register("defineClass", "(Ljava/lang/String;[BII)Ljava/lang/Class;", "")>]
member this.DefineClass : string * byte[] * int * int -> Java.Lang.Class

Parameters

name
String

The expected binary name of the class, or null if not known

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 <cite>The Java&trade; Virtual Machine Specification</cite>.

off
Int32

The start offset in b of the class data

len
Int32

The length of the class data

Returns

The Class object that was created from the specified class data.

Attributes

Exceptions

if classRep does not contain a valid class.

if offset , length or if offset + length is greater than the length of classRep.

Remarks

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

This method assigns a default java.security.ProtectionDomain <tt>ProtectionDomain</tt> to the newly defined class. The ProtectionDomain is effectively granted the same set of permissions returned when java.security.Policy#getPermissions(java.security.CodeSource) <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt> is invoked. The default domain is created on the first invocation of #defineClass(String, byte[], int, int) <tt>defineClass</tt>, and re-used on subsequent invocations.

To assign a specific ProtectionDomain to the class, use the #defineClass(String, byte[], int, int, java.security.ProtectionDomain) <tt>defineClass</tt> method that takes a ProtectionDomain as one of its arguments.

Added in 1.1.

Java documentation for java.lang.ClassLoader.defineClass(java.lang.String, byte[], int, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

DefineClass(String, Byte[], Int32, Int32, ProtectionDomain)

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

[Android.Runtime.Register("defineClass", "(Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", "")]
protected Java.Lang.Class? DefineClass (string? name, byte[]? b, int off, int len, Java.Security.ProtectionDomain? protectionDomain);
[<Android.Runtime.Register("defineClass", "(Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", "")>]
member this.DefineClass : string * byte[] * int * int * Java.Security.ProtectionDomain -> Java.Lang.Class

Parameters

name
String

The expected binary name of the class, or null if not known

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 <cite>The Java&trade; Virtual Machine Specification</cite>.

off
Int32

The start offset in b of the class data

len
Int32

The length of the class data

protectionDomain
ProtectionDomain

The ProtectionDomain of the class

Returns

The Class object created from the data, and optional ProtectionDomain.

Attributes

Exceptions

if classRep does not contain a valid class.

if offset , length or if offset + length is greater than the length of classRep.

if className is not equal to the name of the class contained in classRep.

Remarks

Converts an array of bytes into an instance of class Class, with an optional ProtectionDomain. If the domain is null, then a default domain will be assigned to the class as specified in the documentation for #defineClass(String, byte[], int, int). Before the class can be used it must be resolved.

The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the java.security.CodeSource <tt>CodeSource</tt> within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.

The specified name cannot begin with "java.", since all classes in the "java.* packages can only be defined by the bootstrap class loader. If name is not null, it must be equal to the binary name of the class specified by the byte array "b", otherwise a NoClassDefFoundError <tt>NoClassDefFoundError</tt> will be thrown.

Java documentation for java.lang.ClassLoader.defineClass(java.lang.String, byte[], int, int, java.security.ProtectionDomain).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to