interface接口參數java
jdk1.7及之前使用spring功能實現的:spring
注意:express
1.該功能只能獲取類的方法的參數名,不能獲取接口的方法的參數名。apache
1 public static void test() throws NoSuchMethodException, SecurityException { 2 LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); 3 // Class ac = GenericInvoker.class; 4 Class<?> ac = null; 5 try { 6 /** 7 * 該功能只對類有用,對接口無效 8 */ 9 ac = Class.forName("com.java.example.restexpress.server.boot.TestDemo"); 10 // ac = 11 // Class.forName("cn.ctyun.bigdata.bdcsc2.service.basicdata.CtUserAcctMaskMService"); 12 } catch (ClassNotFoundException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } 16 Method[] methods = ac.getDeclaredMethods(); 17 for (Method m : methods) { 18 String[] parameterNames = discoverer.getParameterNames(m); 19 if (parameterNames == null) { 20 continue; 21 } 22 23 for (String name : parameterNames) { 24 System.out.println(name); 25 } 26 } 27 }
jdk1.8中獲取方式:eclipse
注意:maven
1.該方式用於jdk1.8+ide
2.要獲取的接口的java文件須要用javac -parameters ,進行編譯插件
1 public class Demo 2 { 3 private static void displayParametersMetadata(String className) 4 { 5 Class clazz = null; 6 try { 7 clazz = Class.forName(className); 8 } catch (ClassNotFoundException e) { 9 // TODO Auto-generated catch block 10 e.printStackTrace(); 11 } 12 13 // Get all class's declared methods (does not get inherited methods) 14 final Method[] declaredMethods = clazz.getDeclaredMethods(); 15 for (final Method method : declaredMethods) 16 { 17 writeHeader( 18 "Method " + method.toGenericString() 19 + " has " + method.getParameterCount() + " Parameters:"); 20 int parameterCount = 0; 21 final Parameter[] parameters = method.getParameters(); 22 for (final Parameter parameter : parameters) 23 { 24 25 26 out.println("parameterName="+parameter.getName()); 27 out.println("type="+parameter.getType().getCanonicalName()); 28 out.println("parameter.getParameterizedType()="+parameter.getParameterizedType()); 29 out.println("parameter.isVarArgs()="+parameter.isVarArgs()); 30 } 31 } 32 } 33 34 private static void writeHeader(final String headerText) 35 { 36 out.println("\n=========================================================="); 37 out.println("= " + headerText); 38 out.println("=========================================================="); 39 } 40 41 /** 42 * Indicate whether provided Parameter is final. 43 * 44 * @param parameter Parameter to be tested for 'final' modifier. 45 * @return {@code true} if provided Parameter is 'final'. 46 */ 47 private static boolean isParameterFinal(final Parameter parameter) 48 { 49 return Modifier.isFinal(parameter.getModifiers()); 50 } 51 52 public static void main(final String[] arguments) 53 { 54 55 String arg = "com.java.example.restexpress.server.boot.TestDemo"; 56 // Class.forName("cn.ctyun.bigdata.bdcsc2.service.basicdata.CtUserAcctMaskMService"); 57 58 displayParametersMetadata(arg); 59 } 60 }
----------------------------------------------------------------------------rest
eclipse設置java8的javac 編譯參數,找了好久終於找到了。code
先要將編譯環境設置爲jdk1.8,eclipse最好用新版本,而後就能看到該選項了,打包便可。
終極解決方案:
發現上面的方式不是太好用,因而查找了maven插件,看是否能解決該問題,因而有了在mave的compile插件中增長參數的方式解決編譯問題:在maven插件中增長-parameters參數
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins>