method源码学习

作用:代理类的关联类,主要做反射调用。
当有一个类,你不确定类里面有什么的时候,有一种技术叫做反射,可以获取类中所有的方法和属性,而这个Method类实例化的对象就是用来接受反射后获取的方法的类

部分源码如下:

这是一个可以动态调用的类
/**

  • This class represents a method. Information about the method can be accessed,
  • and the method can be invoked dynamically.
    */

通过比对返回一个方法名称
public static final Comparator<Method> ORDER_BY_SIGNATURE = new Comparator<Method>()

提供调用抽象类中的方法
/**
* @hide
*/
public Method(ArtMethod artMethod) {
super(artMethod);
}
ArtMethod getArtMethod() {
return artMethod;
}
public Annotation[] getAnnotations() {
return super.getAnnotations();
}
@Override public int getModifiers() {
return super.getModifiers();
}
public boolean isVarArgs() {
return super.isVarArgs();
}
public boolean isBridge() {
return super.isBridge();
}
@Override public boolean isSynthetic() {
return super.isSynthetic();
}
@Override public String getName() {
return ArtMethod.getMethodName(artMethod);
}

返回一个异常类型的数组 否则返回空
public Class<?>[] getExceptionTypes() {
if (getDeclaringClass().isProxy()) {
return getExceptionTypesNative();
} else {
// TODO: use dex cache to speed looking up class
return AnnotationAccess.getExceptions(this);
}
}

用与符号做什么?
@Override public int hashCode() {
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
}

如果方法名相同,返回类型是可转让的?
boolean equalNameAndParameters(Method m) {
return getName().equals(m.getName()) &&
ArtMethod.equalMethodParameters(artMethod,m.getParameterTypes());
}

返回一个数组
public Type[] getGenericParameterTypes() {
return Types.getTypeArray(getMethodOrConstructorGenericInfo().genericParameterTypes, false); }

receiver 接收器
args 参数
isAccessible() 是否访问
public Object invoke(Object receiver, Object... args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return invoke(receiver, args, isAccessible());
}

??为什么要做这步
private native Object invoke(Object receiver, Object[] args, boolean accessible)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,776评论 0 9
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • #深度理解Android InstantRun原理以及源码分析 @Author 莫川 ##Instant Run官...
    郑海波mobctrl阅读 1,581评论 2 7
  • Duang…Duang…不知不觉国庆已经过去将近1个月了 2017年没有法定节日可以放假了!!! 生活还在继续,怎...
    趣读书吧阅读 243评论 0 1