亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

JAVA 8 反射獲取參數(shù)名

shiweifu / 3017人閱讀

摘要:前言在之前編譯是不會把構(gòu)造器和方法的參數(shù)名編譯進(jìn)中,如果需要獲取參數(shù)名,可以在方法上加上注解,反射獲取注解的值從而獲取參數(shù)名,比如的和。帶在中添加命令行在后面加運(yùn)行結(jié)果構(gòu)造器方法一方法二方法三方法四這樣就把參數(shù)名給打印出來了,為。

前言

在JDK8之前javac編譯是不會把構(gòu)造器和方法的參數(shù)名編譯進(jìn)class中,如果需要獲取參數(shù)名,可以在方法上加上注解,反射獲取注解的值從而獲取參數(shù)名,比如Jackson的@JsonCreator@JsonProperty 。而JDK8新增了這一個(gè)功能,可以直接調(diào)用java.lang.reflect.Parameter.getName()獲取到,前提是javac需要添加-parameters這個(gè)參數(shù)。通常來說不建議這樣做,因?yàn)檫@會增大.class和在JVM中會占用更多的內(nèi)存。

正文 代碼

直接上代碼。

用來打印類信息
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import static java.lang.System.out;

public class MethodParameterSpy {

    private static final String  fmt = "%24s: %s%n";


    public static void printClassConstructors(Class c) {
        Constructor[] allConstructors = c.getConstructors();
        out.format(fmt, "Number of constructors", allConstructors.length);
        for (Constructor currentConstructor : allConstructors) {
            printConstructor(currentConstructor);
        }
        Constructor[] allDeclConst = c.getDeclaredConstructors();
        out.format(fmt, "Number of declared constructors",
                allDeclConst.length);
        for (Constructor currentDeclConst : allDeclConst) {
            printConstructor(currentDeclConst);
        }
    }

    public static void printClassMethods(Class c) {
        Method[] allMethods = c.getDeclaredMethods();
        out.format(fmt, "Number of methods", allMethods.length);
        for (Method m : allMethods) {
            printMethod(m);
        }
    }

    public static void printConstructor(Constructor c) {
        out.format("%s%n", c.toGenericString());
        Parameter[] params = c.getParameters();
        out.format(fmt, "Number of parameters", params.length);
        for (int i = 0; i < params.length; i++) {
            printParameter(params[i]);
        }
    }

    public static void printMethod(Method m) {
        out.format("%s%n", m.toGenericString());
        out.format(fmt, "Return type", m.getReturnType());
        out.format(fmt, "Generic return type", m.getGenericReturnType());

        Parameter[] params = m.getParameters();
        for (int i = 0; i < params.length; i++) {
            printParameter(params[i]);
        }
    }

    public static void printParameter(Parameter p) {
        out.format(fmt, "Parameter class", p.getType());
        out.format(fmt, "Parameter name", p.getName());
        out.format(fmt, "Modifiers", p.getModifiers());
        out.format(fmt, "Is implicit?", p.isImplicit());
        out.format(fmt, "Is name present?", p.isNamePresent());
        out.format(fmt, "Is synthetic?", p.isSynthetic());
    }

    public static void main(String... args) {

        printClassConstructors(ExampleMethods.class);
        printClassMethods(ExampleMethods.class);
    }
}
包含各種類型方法的類
import java.util.*;

public class ExampleMethods {

    public boolean simpleMethod(String stringParam, int intParam) {
        System.out.println("String: " + stringParam + ", integer: " + intParam);
        return true;
    }

    public int varArgsMethod(String... manyStrings) {
        return manyStrings.length;
    }

    public boolean methodWithList(List listParam) {
        return listParam.isEmpty();
    }

    public  void genericMethod(T[] a, Collection c) {
        System.out.println("Length of array: " + a.length);
        System.out.println("Size of collection: " + c.size());
    }

}
不帶-parameters

不帶-parameters運(yùn)行結(jié)果:

  Number of constructors: 1
public ExampleMethods()
    Number of parameters: 0
Number of declared constructors: 1

# 構(gòu)造器
public ExampleMethods()
    Number of parameters: 0
       Number of methods: 4
       
# 方法一
public boolean ExampleMethods.simpleMethod(java.lang.String,int)
             Return type: boolean
     Generic return type: boolean
         Parameter class: class java.lang.String
          Parameter name: arg0
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false
         Parameter class: int
          Parameter name: arg1
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false
           
# 方法二
public boolean ExampleMethods.methodWithList(java.util.List)
             Return type: boolean
     Generic return type: boolean
         Parameter class: interface java.util.List
          Parameter name: arg0
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false

# 方法三
public  void ExampleMethods.genericMethod(T[],java.util.Collection)
             Return type: void
     Generic return type: void
         Parameter class: class [Ljava.lang.Object;
          Parameter name: arg0
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false
         Parameter class: interface java.util.Collection
          Parameter name: arg1
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false

# 方法四
public int ExampleMethods.varArgsMethod(java.lang.String...)
             Return type: int
     Generic return type: int
         Parameter class: class [Ljava.lang.String;
          Parameter name: arg0
               Modifiers: 0
            Is implicit?: false
        Is name present?: false
           Is synthetic?: false

可以看出Parameter name全都是arg0~argN,因?yàn)閰?shù)名在編譯期已經(jīng)丟失了。Is name present為false。

帶-parameters
maven在pom.xml中添加

    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            
                8
                8
                -parameters
            
        
    

命令行在javac 后面加 -parameters

運(yùn)行結(jié)果
  Number of constructors: 1
public ExampleMethods()
    Number of parameters: 0
Number of declared constructors: 1

# 構(gòu)造器
public ExampleMethods()
    Number of parameters: 0
       Number of methods: 4
           
# 方法一
public boolean ExampleMethods.methodWithList(java.util.List)
             Return type: boolean
     Generic return type: boolean
         Parameter class: interface java.util.List
          Parameter name: listParam
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false

# 方法二
public int ExampleMethods.varArgsMethod(java.lang.String...)
             Return type: int
     Generic return type: int
         Parameter class: class [Ljava.lang.String;
          Parameter name: manyStrings
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false

# 方法三
public  void ExampleMethods.genericMethod(T[],java.util.Collection)
             Return type: void
     Generic return type: void
         Parameter class: class [Ljava.lang.Object;
          Parameter name: a
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false
         Parameter class: interface java.util.Collection
          Parameter name: c
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false
                                 
# 方法四
public boolean ExampleMethods.simpleMethod(java.lang.String,int)
             Return type: boolean
     Generic return type: boolean
         Parameter class: class java.lang.String
          Parameter name: stringParam
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false
         Parameter class: int
          Parameter name: intParam
               Modifiers: 0
            Is implicit?: false
        Is name present?: true
           Is synthetic?: false

這樣就把參數(shù)名給打印出來了,Is name present為true。

留個(gè)問題

出于好奇,以十六進(jìn)制打開ExampleMethods的class文件,maven項(xiàng)目在idea中build出來的class不管有沒有帶-parameters都會把參數(shù)名編譯進(jìn)去,但是多了MethodParameters這幾個(gè)字眼。如下圖:

然后嘗試直接用javac -parameters編譯,打開后

很明顯是沒有把參數(shù)名編譯進(jìn)去的。

好像找不到idea執(zhí)行build的時(shí)候執(zhí)行了什么,所有我猜測控制java.lang.reflect.Parameter.getName()返回是否真實(shí)的參數(shù)名就是在于MethodParameters這詞,在jvm加載class時(shí)識別是否有MethodParameters,而決定是否加載參數(shù)名。

這僅是猜測,望大家相告是其真正的原理。
文中也可能存在錯誤,也望大家指出。

代碼來源

上述代碼全部來自#參考資料中的Obtaining Names of Method Parameters

參考資料

Obtaining Names of Method Parameters

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/76827.html

相關(guān)文章

  • Java反射詳細(xì)介紹

    摘要:通過反射獲取帶參無返回值成員方法并使用設(shè)置安全檢查,訪問私有構(gòu)造函數(shù)必須創(chuàng)建實(shí)例這種不行,注意和方法需要傳遞參數(shù)測試復(fù)制這個(gè)功能獲取私有方法,同樣注意和的區(qū)別賦予訪問權(quán)限調(diào)用方法。 反射 目錄介紹 1.反射概述 1.1 反射概述 1.2 獲取class文件對象的三種方式 1.3 反射常用的方法介紹 1.4 反射的定義 1.5 反射的組成 1.6 反射的作用有哪些 2.反射的...

    ingood 評論0 收藏0
  • Java反射機(jī)制及API使用

    摘要:有一個(gè)參數(shù)的構(gòu)造方法姓名有多個(gè)參數(shù)的構(gòu)造方法姓名年齡這的執(zhí)行效率有問題,以后解決。私有構(gòu)造方法私有的構(gòu)造方法年齡反射獲取對象的三種方式通過對象名方法獲取通過類名方式獲得通過方法獲得在運(yùn)行期間,一個(gè)類,只有一個(gè)對象產(chǎn)生。 原文地址 反射簡單來說,就是動態(tài)加載對象,并對對象進(jìn)行剖析。在Java中的反射機(jī)制是指在運(yùn)行狀態(tài)中,對于任意一個(gè)類,都能夠知道并獲取這個(gè)類的所有屬性和方法。 Java反...

    vibiu 評論0 收藏0
  • 進(jìn)擊的Android工程師之Java基礎(chǔ): 反射

    摘要:不包含父類或父接口的方法返回,根據(jù)方法名和類型獲取。類或接口的及父類父接口公共成員方法。是的返回方法名,不包括修飾符,參數(shù)和返回值。打印打印拋出因?yàn)榈脑L問權(quán)限為拋出,因?yàn)槭歉割惖姆椒ā? 反射機(jī)制呢就是在程序運(yùn)行時(shí),動態(tài)的獲取類(class),類的方法(method)屬性(field)等。主要的注意點(diǎn)就是程序運(yùn)行時(shí)動態(tài)的獲取。這里主要是從代碼的角度來講解Java反射。在使用中我們用的較多...

    aaron 評論0 收藏0
  • 1、類加載器 2、反射構(gòu)造方法 3、反射成員變量 4、反射成員方法 5、反射配置文件運(yùn)行類中的方法

    摘要:通過反射獲取無參構(gòu)造方法并使用得到無參構(gòu)造方法獲取所有的修飾的構(gòu)造方法。如果方法沒有返回值,返回的是反射獲取空參數(shù)成員方法并運(yùn)行代碼演示反射獲取成員方法并運(yùn)行獲取對象中的成員方法獲取的是文件中的所有公共成員方法包括繼承的類是描述 01類加載器 * A.類的加載 當(dāng)程序要使用某個(gè)類時(shí),如果該類還未被加載到內(nèi)存中,則系統(tǒng)會通過加載,連接,初始化三步來實(shí)現(xiàn)對這個(gè)類進(jìn)行初始化。 ? ...

    Profeel 評論0 收藏0
  • Reflection:Java反射機(jī)制基礎(chǔ)

    摘要:反射機(jī)制是什么反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個(gè)類,都能夠知道這個(gè)類的所有屬性和方法對于任意一個(gè)對象,都能夠調(diào)用它的任意一個(gè)方法和屬性這種動態(tài)獲取的信息以及動態(tài)調(diào)用對象的方法的功能稱為語言的反射機(jī)制反射機(jī)制能做什么反射機(jī)制主要提供了以下功 反射機(jī)制是什么 反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個(gè)類,都能夠知道這個(gè)類的所有屬性和方法;對于任意一個(gè)對象,都能夠調(diào)用它的任意一個(gè)方法和屬性;這種...

    hizengzeng 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<