Named Parameters in Java 8

With Java 8 and the compiler flag: javac -parameters method parameter names are available via reflection. For example: the parameter names of the method hello:


public class Boundary {

    public void hello(String name, int age) {

    }
}

become accessible to the following code:


    public static void main(String[] args) {
        Method[] methods = Boundary.class.getMethods();
        for (Method method : methods) {
            System.out.print(method.getName() + "(");
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                System.out.print(parameter.getType().getName() + " " + parameter.getName() + " ");
            }
            System.out.println(")");
        }
    }
}

Compilation with javac -parameters produces the following output:


hello(java.lang.String name int age )

Without the -parameters flag the names are not available:


hello(java.lang.String arg0 int arg1 )

Comments:

Cool! Thanks!

Posted by Mircea on October 14, 2014 at 02:32 PM CEST #

Nice - but who needs this?

Posted by gustav on October 14, 2014 at 07:46 PM CEST #

gustav: Frameworks can use it to some mapping, like spring mvc, aspectj, cxf.. You can use something like jws.WebParam annotation to be sure there will be param name information for the framework at runtime, but frameworks can try to get it another way - via asm from bytecode compiled with debug informations and now via reflection.

Posted by libor on October 20, 2014 at 12:30 PM CEST #

The JDK libraries are compiled without named parameters and no guarantee is offered that the parameter names won't change.

Named parameters seems like a feature that offers very little benefit considering it's brittleness.

Posted by Mike on October 21, 2014 at 04:38 PM CEST #

Very nice option but unfortunately not on by default. VALJOGen (valjogen.41concepts.com) supports the -parameters option which it uses to produce best possible Java 8 value classes from java interfaces.

Posted by Morten Christensen on November 12, 2014 at 09:01 PM CET #

The feature should be welcomed.

Features are not only mend for existing frameworks but also for new frameworks to make java forward.

I expect more frameworks would be built on top of these.

Atleast some in-house solutions could make use of this feature and get their things simple.

Posted by Venkteswara Rao Desu on November 13, 2014 at 02:08 AM CET #

One use case can be code generation tools.

Posted by Venkteswara Rao Desu on November 13, 2014 at 02:11 AM CET #

we specifically are having trouble with the new Java eight update, which usually furnishes support for art and drawing websites software applets. All of the thousands of users on all these art websites, can no longer get their applets to run, two draw and paint, using Java number eight update. It automatically puts up this sign: "security block! Java eight update will not run, you do not have security clearance." Similar to that. Since we have thousands of users on art websites, that are required to use Java to run their software applets, all these websites are completely at a standstill.there must be a fix, or a patch for Java number eight update.? If there is not, someone must find another company to provide a substitute for Java number eight update, to run all the art drawing and painting websites, that need a Java program to run their software applets. Please advise.

Posted by D.H. Bucher on November 15, 2014 at 06:23 AM CET #

Nice for debugging and decompilation I guess

Posted by Icu on February 22, 2018 at 06:47 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License