Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.

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. eclipse-setting.png 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());
    }
}

Symptoms of the problem

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 isVersion: 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.

/.settings/org.eclipse.jdt.core.prefs

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.

temporary solution

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

Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
Call a method of the parent class by explicitly specifying the name in Ruby
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
When the certificate cannot be obtained by https-portal production (Response Code: 403)
Part of the class called from the class under test in JMockit Mock method memo
Be careful when setting the class name when creating a document-based app in Swift
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Cause of is not visible when calling a method of another class in java
[Java] Handling of JavaBeans in the method chain
[Java] Dynamic method call by reflection of enum (enum)
When you get lost in the class name
Method name of method chain in Java Builder + α
When the project is not displayed in eclipse
[Rails] Bootstrap's text-light cannot be inherited by the link_to method because the class is not written as an argument in the method.
[Java] Integer information of characters in a text file acquired by the read () method
[Kotlin] Get the argument name of the constructor by reflection
Read the packet capture obtained by tcpdump in Java
Get the name of the test case in the JUnit test class
Summarize the additional elements of the Optional class in Java 9
[Java] It seems that `0 <hoge <10` cannot be written in the conditional expression of the ʻif` statement.
When using the constructor of Java's Date class, the date advances by 1900.
Things to be aware of when writing code in Java
Summary of values returned by the Spliterator characteristics method #java
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
Method definition location Summary of how to check When defined in the project and Rails / Gem