5

I'm currently trying to set some parameters from an external system. I have a request with named parameters, and in order to properly set the variables, I'm using annotated method arguments on my service calls. A simplified example might be

public Response loginAttempt(@MyParam("username") String username, @MyParam("password") String password) {
    // login logic here
}

Clearly, annotating each argument name is annoying and duplicative (although, it does have the minor advantage of allowing me to change the name over different versions of the API, that's beside the point.)

It would be very, very handy if I was able to, in my reflective portion, to simply reference the name of the argument. Where now, I get the arguments and their annotations, note their order, make an array of that order, and run with it.

I know in previous version of Java this simply cannot be done. I also know Java is releasing versions faster than ever before, with newer and more modern features than ever before. Unfortunately, the signal to noise ratio with 15 year old information is too just not high enough to get a definitive answer. Is this something that can be done with modern Java?

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • How about building your services classes with the parameters flag to get `javac` to add method parameter names in the bytecode? Not sure this is where the problem is though. – ernest_k Mar 19 '19 at 15:44

1 Answers1

5

Since Java 8 if you compile your code with javac -parameters option and the method parameters will be retained, however there are drawbacks. The problem is primarily the class file size, take a look at Obtaining Names of Method Parameters docs.

You can use java.lang.reflect.Parameter.getName() to get the method parameter name:

Method m = getClass().getMethods()[0];
System.out.println(m.getName()); // loginAttempt
Parameter[] parameters = m.getParameters();
for (Parameter parameter : parameters) {
    System.out.print(parameter.getName() + " "); // username password
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Oh very nice. So I can use this instead of the `@MyParam("username")` map - As I look at the compile settings in Eclipse, I suppose that would mean I need to make sure "Add variable attributes to generated class files (used by the debugger)"? – corsiKa Mar 19 '19 at 15:53
  • @corsiKa apologies but I have no idea how to turn this on in Eclipse – Karol Dowbecki Mar 19 '19 at 15:53
  • -parameters would be enough in Eclipse or any other IDE where you tweek your Java compiler settings. – MS90 Mar 19 '19 at 15:56
  • I need to do some tinkering before I can put the green check, but I definitely think this is the route I need to be taking. This is going to save a lot of headaches, at the expense of a little disk space (and security checks...) – corsiKa Mar 19 '19 at 16:00
  • @corsiKa check the linked drawbacks answer, you better have good tests since errors will be hard to spot. – Karol Dowbecki Mar 19 '19 at 16:00
  • @KarolDowbecki Errors will actually be very easy to spot - I mean, they'll be as easy to spot as they are already with the annotation technique... – corsiKa Mar 19 '19 at 16:15