java反射机制初体验2

Constructor类

Constructor 提供关于类的单个构造方法的信息以及对它的访问权限。

Constructor 允许在将实参与带有底层构造方法的形参的 newInstance() 匹配时进行扩展转换,但是如果发生收缩转换,则抛出 IllegalArgumentException。

示例代码:

View Code

1 package reflect; java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 import java.util.Iterator; ReflectTest { alan args Exceptionmain(String[] args) throws Exception {* Contructor类解析:类的构造方法没有顺序 Constructor 提供关于类的单个构造方法的信息以及对它的访问权限。18 * Constructor 允许在将实参与带有底层构造方法的形参的 newInstance() 匹配时进行扩展转换,19 * 但是如果发生收缩转换,则抛出 IllegalArgumentException。20 * 21 * 反射比较占用时间,需要缓存。程序性能下降:查看Class源码System.out.println(“Contructor类解析”); String string2=(String) constructor.newInstance(System.out.println(string2);// 编译时无措:二进制代码 运行时出错:Exception in thread “main” java.lang.IllegalArgumentException: argument type mismatch参数不同,运行时出错 }31 }

Class 的newInstance()方法解析。newInstance ()中调用的newInstance0()方法使用缓存机制来保存默认构造方法的实例。

jdk源码解析:

public T newInstance()

throws InstantiationException, IllegalAccessException

{

if (System.getSecurityManager() != null) {

checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader(), false);

}

return newInstance0();

}

private T newInstance0()

throws InstantiationException, IllegalAccessException

{

// NOTE: the following code may not be strictly correct under

// the current Java memory model.

// Constructor lookup

if (cachedConstructor == null) { //缓存为空

if (this == Class.class) {

throw new IllegalAccessException(

“Can not call newInstance() on the Class for java.lang.Class”

);

}

try {

Class<?>[] empty = {};

final Constructor<T> c = getConstructor0(empty, Member.DECLARED);//得到无参的构造方法构造实例对象。

// Disable accessibility checks on the constructor

// since we have to do the security check here anyway

// (the stack depth is wrong for the Constructor’s

// security check to work)

java.security.AccessController.doPrivileged(

new java.security.PrivilegedAction<Void>() {

public Void run() {

还有不愿面对失败的尴尬。曾经怀有远大理想,拥有完美的憧憬。

java反射机制初体验2

相关文章:

你感兴趣的文章:

标签云: