JVM学习笔记16——类加载器重要方法详解

本文详细解析了Java中的defineClass方法,该方法用于将字节数组转换为Class对象。在转换过程中,它会为新定义的类分配一个默认的保护域,该域具有特定的权限设置。如果尝试将类添加到包含不同签名者证书的包中,或者类名以'java.'开头,可能会抛出SecurityException。同时,文章也提到了ClassFormatError和其他可能的异常情况。
摘要由CSDN通过智能技术生成

defineClass详解

protected final Class<?> defineClass(String name,
                                     byte[] b,
                                     int off,
                                     int len)
                             throws ClassFormatError
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 ProtectionDomain to the newly defined class. The ProtectionDomain is effectively granted the same set of permissions returned when Policy.getPolicy().getPermissions(new CodeSource(null, null)) is invoked. The default domain is created on the first invocation of defineClass, and re-used on subsequent invocations.
To assign a specific ProtectionDomain to the class, use the defineClass method that takes a ProtectionDomain as one of its arguments.


Parameters:
name - The expected binary name of the class, or null if not known
b - 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 - The start offset in b of the class data
len - The length of the class data


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


Throws:
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 (which is unsigned), or if name begins with "java.".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值