interface接口参数
jdk1.7及以前使用spring功能实现的:
注意:
1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名。
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中获取方式:
注意:
1.该方式用于jdk1.8+
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 }
----------------------------------------------------------------------------
eclipse设置java8的javac 编译参数,找了很久终于找到了。
先要将编译环境设置为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>