[Java] Temporary correspondence memo when the method parameter name of Java class cannot be acquired by reflection in Eclipse plug-in project

1 minute read

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 is stored in the class file.

In Eclipse, for example, in the case of a Java project, you can create a corresponding jar by checking and exporting the following checks in the settings common to each project or in each project. Most likely, the check will compile with the -parameters option set internally. eclipse-setting.png Although it is simple, you can check the parameter name by using reflection from the reference to the constructor or method object as shown below.

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());
    }
}

Problem symptoms

However, in the case of a plug-in project, it seems that this setting is not working for some reason, and in the exported jar (or bundle), the method parameter names are arg0, arg1, arg2…. Will be. Such names are automatically given if you do not specify the -parameters option. The Eclipse IDE used is Version: 2020-06 (4.16.0) Build id: 20200615-1200. (Maybe the latest)

From this, it seems that the plugin project is actually compiled without the -paramters option even if it is checked. If you check it, the following line will be added to the following files.

/.settings/org.eclipse.jdt.core.prefs ``` org.eclipse.jdt.core.compiler.codegen.methodParameters=generate ``` By exporting in a project that contains this setting, it is expected that a `jar` that can acquire the parameter name of the method by reflection will be created. In the case of a plug-in project, it is convenient to create a `MANIFEST.MF` that clearly shows the dependency of the bundle, and it is convenient to create an OSGi bundle, but this time, I felt it was quite inconvenient. ## temporary solution Therefore, once create the Java project of the same source as the plug-in project, and refer to it from the template of the `MANIFEST.MF` created in the plug-in project that solved the bundle dependency. I decided to create an OSGi bundle. (Although it's easy) This is a memorandum. (Is there something wrong?)