图说jdk1.8新特性(5)--- 编译器新特性


/**
     * Returns the name of the parameter.  If the parameter's name is
     * {@linkplain #isNamePresent() present}, then this method returns
     * the name provided by the class file. Otherwise, this method
     * synthesizes a name of the form argN, where N is the index of
     * the parameter in the descriptor of the method which declares
     * the parameter.
     *
     * @return The name of the parameter, either provided by the class
     *         file or synthesized if the class file does not provide
     *         a name.
     */
    public String getName() {
        // Note: empty strings as paramete names are now outlawed.
        // The .equals("") is for compatibility with current JVM
        // behavior.  It may be removed at some point.
        if(name == null || name.equals(""))
            return "arg" + index;
        else
            return name;
    }

说明:

  • jdk1.8反射包增加了Parameter类,通过该类的getName方法能在运行时得到参数的名称
  • 如果没有通过-parameters指定编译器在编译的时候将参数名编译进去,那么得到的参数名称将会是arg1、arg2、arg3这种默认的参数名