博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpServlet中getAllDeclaredMethods()方法
阅读量:6982 次
发布时间:2019-06-27

本文共 1728 字,大约阅读时间需要 5 分钟。

在看apache-tomcat-7.0.40中的HttpServlet的时候,看到它里面的方法getAllDeclaredMethods()写的很不错!

1  private static Method[] getAllDeclaredMethods(Class
c) { 2 3 if (c.equals(javax.servlet.http.HttpServlet.class)) { 4 return null; 5 } 6 7 Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass()); 8 Method[] thisMethods = c.getDeclaredMethods(); 9 10 if ((parentMethods != null) && (parentMethods.length > 0)) {11 Method[] allMethods =12 new Method[parentMethods.length + thisMethods.length];13 System.arraycopy(parentMethods, 0, allMethods, 0,14 parentMethods.length);15 System.arraycopy(thisMethods, 0, allMethods, parentMethods.length,16 thisMethods.length);17 18 thisMethods = allMethods;19 }20 21 return thisMethods;22 }

我想说的有两个地方:

one:if ((parentMethods != null) && (parentMethods.length > 0))

我们在判断一个数组的时候是否为空的时候,应该先判断该数组是否为<code>null</code>,在判断数组的长度...

two:System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length);

这里提到的方法是:

1 public static native void arraycopy(Object src,  int  srcPos,2                                         Object dest, int destPos,3                                         int length);

这是一个数组复制数组的函数,在  java.lang.System 类中。

参数含义:

1      * @param      src      the source array. //原数组2      * @param      srcPos   starting position in the source array. //原数组的起始位置3      * @param      dest     the destination array.//目标数组4      * @param      destPos  starting position in the destination data.//目标数组起始位置5      * @param      length   the number of array elements to be copied.//需要复制的长度

很好用的方法..

转载地址:http://zvvpl.baihongyu.com/

你可能感兴趣的文章
LaTeX使用listings宏包插入代码时,将代码字体设为 Monaco
查看>>
设计模式之迭代子模式
查看>>
代码评审的不可能三角
查看>>
揭秘ThreadLocal
查看>>
七年蜕变 感恩献礼
查看>>
共享经济、短视频、新零售、AI:寻觅2019年新经济未来走向
查看>>
zabbix配置邮箱报警
查看>>
使用ulimit设置文件最大打开数
查看>>
[Step By Step]SAP HANA PAL指数回归预测分析Exponential Regression编程实例EXPREGRESSION(模型)...
查看>>
VMware Data Recovery备份恢复vmware虚拟机
查看>>
solr多core的处理
查看>>
解决DeferredResult 使用 @ResponseBody 注解返回中文乱码
查看>>
C# WinForm开发系列 - TextBox
查看>>
28岁少帅统领旷视南京研究院,LAMDA魏秀参专访
查看>>
java文件传输
查看>>
Xen虚拟机迁移技术
查看>>
安装Sql Server 2005出现“性能监视器计数器要求”错误解决方法。
查看>>
[.NET领域驱动设计实战系列]专题八:DDD案例:网上书店分布式消息队列和分布式缓存的实现...
查看>>
Icomparer和Icomparable集合排序
查看>>
【poi xlsx报错】使用POI创建xlsx无法打开
查看>>