Created
April 23, 2014 08:54
-
-
Save ytoshima/11207754 to your computer and use it in GitHub Desktop.
JDK8 Parameter Names sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* JDK 8 javac has a new option -parameters. | |
* If this option was used, parameter name is stored in class file. | |
* The parameter name can be obtained via reflection. New class | |
* Parameter and a new method Method#getParameters() was added. | |
* $ ~/local/jdk1.8.0/bin/java ParamEx | |
* method: main | |
* param: java.lang.String[] arg0 | |
* method: doit | |
* param: java.lang.String arg0 | |
* param: int arg1 | |
* $ ~/local/jdk1.8.0/bin/javac -parameters ParamEx.java | |
* $ ~/local/jdk1.8.0/bin/java ParamEx | |
* method: main | |
* param: java.lang.String[] args | |
* method: doit | |
* param: java.lang.String name | |
* param: int iarg1 | |
*/ | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
public class ParamEx { | |
public static void main(String[] args) { | |
showParamInfo(ParamEx.class, "main", String[].class); | |
showParamInfo(ParamEx.class, "doit", String.class, int.class); | |
} | |
static void showParamInfo(Class<?> c, String mn, Class<?>... parameterTypes) { | |
try { | |
Method m = c.getMethod(mn, parameterTypes); | |
P("method: " + mn); | |
for (Parameter p : m.getParameters()) { | |
P(" param: " + p); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void doit(String name, int iarg1) { | |
System.out.println(name + "" + iarg1); | |
} | |
static void P(Object o) { System.out.println(o); } | |
static void I(Object o) { System.out.println("I: " + o); } | |
static void W(Object o) { System.out.println("I: " + o); } | |
static void E(Object o) { System.out.println("I: " + o); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment