java8动态编译保留方法参数名字
编译保留方法参数名字
javac 文档
javac doc
-parameters Stores formal parameter names of constructors and methods in the generated class file so that the method java.lang.reflect.Executable.getParameters from the Reflection API can retrieve them.
获取动态编译支持的参数列表
反射获取参数名这个问题本身和解决的方法很简单,但动态编译相关资料和问题不多,也没有找到介绍动态编译时 options 相关参数的文章和问题,此处做一下记录,方便参考 。
调用 JavaCompiler 接口的 getTask 方法时,可以使用参数 options
CompilationTask getTask(Writer out, JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits);
参数列表
获取 option 参数列表的方法如下
断点打在 processArgs 方法上,执行 Evaluate Expression 片段
com.sun.tools.javac.main.Main.processArgs
此处类引用和参数类型出现缺失,IDE 报红,不能在一行完成循环打印
for (int p=0;p<recognizedOptions.length;p++) { System.out.print(recognizedOptions[p].name);
System.out.print(" ");
System.out.print(recognizedOptions[p].text);
System.out.print(" ");
System.out.print(recognizedOptions[p].descrKey+"");
System.out.print(" ");
System.out.print(recognizedOptions[p].group.name+"");
System.out.print(" ");
System.out.printf(recognizedOptions[p].kind.name+"");
System.out.println();
}
recognizedOptions
输出
共有61个参数,第二列是 option 参数名称和格式,详细释义和 demo 见开头的 javac 官方文档
G -g opt.g BASIC STANDARDG_NONE -g:none opt.g.none BASIC STANDARD
G_CUSTOM -g: opt.g.lines.vars.source BASIC STANDARD
XLINT -Xlint opt.Xlint BASIC EXTENDED
XLINT_CUSTOM -Xlint: opt.Xlint.suboptlist BASIC EXTENDED
XDOCLINT -Xdoclint opt.Xdoclint BASIC EXTENDED
XDOCLINT_CUSTOM -Xdoclint: opt.Xdoclint.custom BASIC EXTENDED
NOWARN -nowarn opt.nowarn BASIC STANDARD
VERBOSE -verbose opt.verbose BASIC STANDARD
DEPRECATION -deprecation opt.deprecation BASIC STANDARD
CLASSPATH -classpath opt.classpath FILEMANAGER STANDARD
CP -cp opt.classpath FILEMANAGER STANDARD
SOURCEPATH -sourcepath opt.sourcepath FILEMANAGER STANDARD
BOOTCLASSPATH -bootclasspath opt.bootclasspath FILEMANAGER STANDARD
XBOOTCLASSPATH_PREPEND -Xbootclasspath/p: opt.Xbootclasspath.p FILEMANAGER EXTENDED
XBOOTCLASSPATH_APPEND -Xbootclasspath/a: opt.Xbootclasspath.a FILEMANAGER EXTENDED
XBOOTCLASSPATH -Xbootclasspath: opt.bootclasspath FILEMANAGER EXTENDED
EXTDIRS -extdirs opt.extdirs FILEMANAGER STANDARD
DJAVA_EXT_DIRS -Djava.ext.dirs= opt.extdirs FILEMANAGER EXTENDED
ENDORSEDDIRS -endorseddirs opt.endorseddirs FILEMANAGER STANDARD
DJAVA_ENDORSED_DIRS -Djava.endorsed.dirs= opt.endorseddirs FILEMANAGER EXTENDED
PROC -proc: opt.proc.none.only BASIC STANDARD
PROCESSOR -processor opt.processor BASIC STANDARD
PROCESSORPATH -processorpath opt.processorpath FILEMANAGER STANDARD
PARAMETERS -parameters opt.parameters BASIC STANDARD
D -d opt.d FILEMANAGER STANDARD
S -s opt.sourceDest FILEMANAGER STANDARD
H -h opt.headerDest FILEMANAGER STANDARD
IMPLICIT -implicit: opt.implicit BASIC STANDARD
ENCODING -encoding opt.encoding FILEMANAGER STANDARD
SOURCE -source opt.source BASIC STANDARD
TARGET -target opt.target BASIC STANDARD
PROFILE -profile opt.profile BASIC STANDARD
VERSION -version opt.version INFO STANDARD
FULLVERSION -fullversion null INFO HIDDEN
DIAGS -XDdiags= null INFO HIDDEN
HELP -help opt.help INFO STANDARD
A -A opt.A BASIC STANDARD
X -X opt.X INFO STANDARD
J -J opt.J INFO STANDARD
MOREINFO -moreinfo null BASIC HIDDEN
WERROR -Werror opt.Werror BASIC STANDARD
PROMPT -prompt null BASIC HIDDEN
DOE -doe null BASIC HIDDEN
PRINTSOURCE -printsource null BASIC HIDDEN
WARNUNCHECKED -warnunchecked null BASIC HIDDEN
XMAXERRS -Xmaxerrs opt.maxerrs BASIC EXTENDED
XMAXWARNS -Xmaxwarns opt.maxwarns BASIC EXTENDED
XSTDOUT -Xstdout opt.Xstdout INFO EXTENDED
XPRINT -Xprint opt.print BASIC EXTENDED
XPRINTROUNDS -XprintRounds opt.printRounds BASIC EXTENDED
XPRINTPROCESSORINFO -XprintProcessorInfo opt.printProcessorInfo BASIC EXTENDED
XPREFER -Xprefer: opt.prefer BASIC EXTENDED
XPKGINFO -Xpkginfo: opt.pkginfo BASIC EXTENDED
O -O null BASIC HIDDEN
XJCOV -Xjcov null BASIC HIDDEN
PLUGIN -Xplugin: opt.plugin BASIC EXTENDED
XDIAGS -Xdiags: opt.diags BASIC EXTENDED
XD -XD null BASIC HIDDEN
AT @ opt.AT INFO STANDARD
SOURCEFILE sourcefile null INFO HIDDEN
验证参数名字
粗略验证,如果输出的参数名不是 arg0 和 arg1 这种格式则说明设置生效
Stream.of(clz.getDeclaredMethods()).forEach( method -> {
Stream.of( method.getParameters()).forEach(
parameter -> {
System.out.printf("%s( %s,%s,%s)
",method.getName(),parameter.getName(),
parameter.getRealName(),parameter.isVarArgs());
}
);
}
);
以上是 java8动态编译保留方法参数名字 的全部内容, 来源链接: utcz.com/z/512030.html