• HttpServlet中getAllDeclaredMethods()方法


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

    很好用的方法..

  • 相关阅读:
    洛谷—— P1196 银河英雄传说
    MySQL练习题参考答案
    January 16 2017 Week 3 Monday
    January 15 2017 Week 3 Sunday
    January 14 2017 Week 2nd Saturday
    January 13 2017 Week 2 Friday
    January 12 2017 Week 2 Thursday
    January 11 2017 Week 2nd Wednesday
    January 10 2017 Week 2nd Tuesday
    January 09 2017 Week 2nd Monday
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_httpservlet.html
Copyright © 2020-2023  润新知