Since Java8, it is possible to get the parameter name of the method including the constructor of the class through reflection. By compiling with the -parameters
option added to javac
, the information needed for this will be stored in the class file.
In Eclipse, for example, in the case of Java project, you can create the corresponding jar by checking the following and exporting in the settings common to the project or for each project. Perhaps by checking, the -parameters
option is set internally and compiled.
It's simple, but you can check the parameter name by using reflection from the reference to the constructor or method object as follows.
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
System.out.println("constructor: " + constructor.getName());
for (Parameter param : constructor.getParameters()) {
System.out.println(" param: " + param.getName() + ", type: " + param.getType().getSimpleName());
}
}
for (Method method : clazz.getDeclaredMethods()) {
System.out.println("method: " + method.getName());
for (Parameter param : method.getParameters()) {
System.out.println(" param: " + param.getName() + ", type: " + param.getType().getSimpleName());
}
}
However, in the case of a plugin project, for some reason this setting doesn't seem to work, and in the exported jar
(or bundle), the method parameter names are ʻarg0, arg1, arg2…It becomes. Such names are given automatically if you do not specify the
-parametersoption. The Eclipse IDE you are using is
Version: 2020-06 (4.16.0) Build id: 20200615-1200`. (Maybe the latest)
From this, for plug-in projects, even if you check it, it looks like it was actually compiled without the -paramters
option. If you check it, the following line will be added to the following files.
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
By exporting in a project with this setting, it is expected that a jar
that can get the parameter name of the method by reflection will be created.
In the case of a plug-in project, it is useful to create a MANIFEST.MF
that clearly shows the bundle dependencies and easily create an OSGi bundle, but this case was quite inconvenient.
Therefore, once created a Java project with the same source as the plug-in project, refer to the template of MANIFEST.MF
that solved the bundle dependency once created in the plug-in project. I decided to create an OSGi bundle. (It's easy)
This is a memorandum. (Maybe something is wrong?)
Recommended Posts