前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HttpServlet中getAllDeclaredMethods()方法

HttpServlet中getAllDeclaredMethods()方法

作者头像
Hongten
发布2018-09-13 14:24:16
4110
发布2018-09-13 14:24:16
举报
文章被收录于专栏:HongtenHongtenHongten

在看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.//需要复制的长度

很好用的方法..

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-05-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档