如何在Eclipse中使用任何javac标志编译web应用程序

huangapple go评论75阅读模式
英文:

How to compile web application with any javac flag in Eclipse

问题

我有一个网络应用程序,我想用反射 java.lang.reflect.Method.getParameters 获取方法的参数名称。然而,只有在使用 -parameters 标志编译Java文件时,才能检索到参数名称。

您可以使用方法 java.lang.reflect.Executable.getParameters 获取任何方法或构造函数的形式参数的名称。 (类 Method 和 Constructor 扩展了类 Executable,因此继承了方法 Executable.getParameters。) 但是,默认情况下,.class 文件不会存储形式参数名称。这是因为许多生成和消耗 class 文件的工具可能不希望处理包含参数名称的 .class 文件的较大的静态和动态占用空间。特别是,这些工具必须处理更大的 .class 文件,并且 Java 虚拟机 (JVM) 将使用更多的内存。此外,某些参数名称(例如 secret 或 password)可能会暴露有关安全敏感方法的信息。

要在特定的 .class 文件中存储形式参数名称,从而使反射 API 能够检索形式参数名称,请使用 -parameters 选项编译源文件到 javac 编译器

来源:https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

如何指示 Eclipse 使用 -parameters 编译我的网络应用程序,以便我稍后可以在 Eclipse 中使用使用 -parameters 编译的代码启动 Tomcat 服务器?

英文:

I have a web application in which I want to retrieve method's parameter names with reflection java.lang.reflect.Method.getParameters. Howerver parameter names can be retrieved only if Java files were compiled with -parameters flag.

> You can obtain the names of the formal parameters of any method or
> constructor with the method
> java.lang.reflect.Executable.getParameters. (The classes Method and
> Constructor extend the class Executable and therefore inherit the
> method Executable.getParameters.) However, .class files do not store
> formal parameter names by default
. This is because many tools that
> produce and consume class files may not expect the larger static and
> dynamic footprint of .class files that contain parameter names. In
> particular, these tools would have to handle larger .class files, and
> the Java Virtual Machine (JVM) would use more memory. In addition,
> some parameter names, such as secret or password, may expose
> information about security-sensitive methods.
>
> To store formal parameter names in a particular .class file, and thus
> enable the Reflection API to retrieve formal parameter names, compile
> the source file with the -parameters option to the javac compiler
.

Source: https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

How to instruct Eclipse to compile my web application with -parameters so I could later start Tomcat server in Eclipse with my code compiled with -parameters?

答案1

得分: 1

Eclipse拥有自己的Java编译器,而-parameters标志的等效功能可以在项目 > 属性:Java编译器中通过勾选最后一个复选框进行设置:存储有关方法参数的信息(可通过反射使用)(默认情况下未启用,需要Java 8或更高版本)。

英文:

Eclipse has its own Java compiler and the equivalent of the -parameters flag can be set in Project > Properties: Java Compiler by ticking the last checkbox: Store information about method parameters (usable via reflection) (which is not enabled by default and requires Java 8 or higher).

huangapple
  • 本文由 发表于 2020年10月9日 00:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64267004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定