java8 dynamic compilation preserves method parameter names

Keywords: Programming encoding Java Fragment

Compile reserved method parameter name

javac document

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. 

Get a list of parameters supported by dynamic compilation

The problem of getting parameter names by reflection itself and the solution is very simple, but there are not many data and problems related to dynamic compilation, and there are no articles and problems about the parameters related to options during dynamic compilation. Here is a record for reference.

When calling the getTask method of the JavaCompiler interface, you can use the parameter options

CompilationTask getTask(Writer out,
                            JavaFileManager fileManager,
                            DiagnosticListener<? super JavaFileObject> diagnosticListener,
                            Iterable<String> options,
                            Iterable<String> classes,
                            Iterable<? extends JavaFileObject> compilationUnits);

parameter list

The method to get the option parameter list is as follows

The breakpoint is on the processArgs method, and the Evaluate Expression fragment is executed com.sun.tools.javac.main.Main.processArgs Class reference and parameter type are missing here, IDE reports red, and can't print circularly in one line

for (int p=0;p<recognizedOptions.length;p++) {
    System.out.print(recognizedOptions[p].name);
    System.out.print("\t");
    System.out.print(recognizedOptions[p].text);
    System.out.print("\t");
    System.out.print(recognizedOptions[p].descrKey+"");
    System.out.print("\t");
    System.out.print(recognizedOptions[p].group.name+"");
    System.out.print("\t");
    System.out.printf(recognizedOptions[p].kind.name+"");
    System.out.println();
}

recognizedOptions

output

There are 61 parameters in total. The second column is the option parameter name and format. For detailed explanation and demo, see the official javac document at the beginning

G	-g	opt.g	BASIC	STANDARD
G_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

Validation parameter name

Rough verification, if the parameter name of the output is not arg0 or arg1, the setting takes effect

Stream.of(clz.getDeclaredMethods()).forEach(
        method -> {
           Stream.of( method.getParameters()).forEach(
               parameter ->     {
                           System.out.printf("%s( %s,%s,%s)\n",method.getName(),parameter.getName(),
                                   parameter.getRealName(),parameter.isVarArgs());
                   }
           );
        }
);   

Posted by nonexistentera on Wed, 25 Dec 2019 13:33:50 -0800