java java.lang.reflect.Parameter .getName() 取method的参数变量名(Obtaining Names of Method Parameters)

       根据 oracle 官方解释(https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html) , 编译器为了压缩 .class 大小,压缩了参数名,默认用 argN, N代表方法参数列表下标,通过 java.lang.reflect.Parameter .getName() 获取到的参数名如 arg0arg1arg2 etc。

    如果要在保持原始变量名:在 javac 命令后面加上 -parameters 参数即可,如果是用 maven 编译,可以这样写


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <compilerArgument>-parameters</compilerArgument>
                <parameters>true</parameters>
                <testCompilerArgument>-parameters</testCompilerArgument>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
 

猜你喜欢

转载自blog.csdn.net/starzxf/article/details/83105225