圖說jdk1.8新特性(5)--- 編譯器新特性

  • 2019 年 10 月 31 日
  • 筆記

/**       * 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這種默認的參數名

Tags: