本人出于学习阶段,如有不对,请多多指出。
API的解释:MethodDescriptor 描述了一种特殊方法,即 Java Bean 支持从其他组件对其进行外部访问。
MethodDescriptor和其他的Descriptor一样,其中的特殊知识点仅仅包含了一些常规的引用类(例如弱引用、软引用)知识点。
当前类继承自FeatureDescriptor,所以其中的很多一部分方法也是被直接用到,例如getClass0、getName这些方法。
有一个方法值得去看一下,getMethod()方法除了被本身的构造器调用外,还会被EventSetDescriptor的getListenerMethods方法所调用。
getListenerMethods和getMethod都设置为同步方法,当本身的构造器调用的时候方法的锁还在,但是当其他线程安全的方法调用getMethod()方法时,getMethod()的锁会消除,这个机制叫做锁消除(可以详见后面关于锁的详解)。
锁消除:是Java虚拟机在编译时,通过对运行时上下文的扫描,对不可能存在共享数据竞争的锁进行消除,通过锁消除,可以节省毫无意义的请求锁时间。
/**
* 获取被MethodDescriptor所包裹的方法
*
* @return The low-level description of the method
*/
public synchronized Method getMethod() {
Method method = this.methodRef.get();
if (method == null) {
Class<?> cls = getClass0();
String name = getName();
if ((cls != null) && (name != null)) {
Class<?>[] params = getParams();
if (params == null) {
for (int i = 0; i < 3; i++) {
// Find methods for up to 2 params. We are guessing here.
// This block should never execute unless the classloader
// that loaded the argument classes disappears.
method = Introspector.findMethod(cls, name, i, null);
if (method != null) {
break;
}
}
} else {
method = Introspector.findMethod(cls, name, params.length, params);
}
setMethod(method);
}
}
return method;
}
方法中有一个地方标记了注释,简单理解这个地方,就是当构造MethodDescriptor类传入的params(弱引用)为空时才会执行Introspector.findMethod方法去寻找类,但是,始终要记住一点,类不容易被回收。
其余的方法没有过多可讲的地方,如有不懂的可以留言,我会进行解答