Ayeshmantha Perera
Ayeshmantha Perera

Reputation: 909

Persist method argument names in bytecode so that they are available through reflection when building with Maven

To use from reflection I need to store informations about method parameters.

It's possible to do it manually from eclipse by following (Window -> Preferences -> Java -> Compiler)

But how can I enable this through maven build

import java.lang.reflect.Method;  
import java.lang.reflect.Parameter;


public class GetParams {

       public static void main(String[] args) throws Exception {
             Method method = MyInterface.class.getMethod("myway", String.class);
             Parameter p = m.getParameters()[0];
             System.out.println(p.isNamePresent());
             System.out.println(p.getName());
        }  

        public interface MyInterface {
             String myway(String str);
        }  
  }

Upvotes: 3

Views: 1323

Answers (1)

Aki
Aki

Reputation: 1754

To store parameter names in generated bytecode you must pass the -parameters flag to the java compiler. If you've using maven, you can do so via the maven-compiler-plugin:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <compilerArgs>
        <arg>-parameters</arg>
      </compilerArgs>
    </configuration>
  </plugin>
</plugins>

Upvotes: 5

Related Questions