Java方法签名的获取实例代码
程序员文章站
2022-06-05 08:46:10
本文研究的主要是java方法签名的获取,下面是具体实现实例。
实例代码:
package com.yunshouhu;
import java.lang.re...
本文研究的主要是java方法签名的获取,下面是具体实现实例。
实例代码:
package com.yunshouhu; import java.lang.reflect.field; import java.lang.reflect.method; import java.lang.reflect.type; import java.util.collection; import com.alibaba.fastjson.parser.defaultjsonparser; import com.alibaba.fastjson.parser.jsonlexer; import com.alibaba.fastjson.parser.deserializer.objectdeserializer; /** * 获取java的方法签名,参考javah -jni 类路径/javap -s 类路径。 * @author wujianhua * @date 2017年9月5日 下午3:25:51 * @url http://blog.csdn.net/earbao */ public class asmutilsforjavah { public static void main(string[] args) throws exception { system.out.println(asmutilsforjavah.getdesc(system.class)); system.out.println(asmutilsforjavah.getdesc(string.class)); system.out.println(asmutilsforjavah.getdesc(integer.class)); system.out.println(asmutilsforjavah.getdesc(int.class)); method method=asmutilsforjavah.class.getdeclaredmethod("main", string[].class); system.out.println("javah -jni"); system.out.println(asmutilsforjavah.getdesc(method)); system.out.println(asmutilsforjavah.gettype(system.class)); system.out.println(asmutilsforjavah.gettype(asmutilsforjavah.class)); } public static boolean isandroid(final string vmname) { final string lowervmname = vmname.tolowercase(); return lowervmname.contains("dalvik") || lowervmname.contains("lemur"); } public static boolean isandroid() { return isandroid(system.getproperty("java.vm.name")); } public static string getdesc(final method method) { final stringbuffer buf = new stringbuffer(); buf.append("("); final class<?>[] types = method.getparametertypes(); for (int i = 0; i < types.length; ++i) { buf.append(getdesc(types[i])); } buf.append(")"); buf.append(getdesc(method.getreturntype())); return buf.tostring(); } public static string getdesc(final class<?> returntype) { if (returntype.isprimitive()) { return getprimitiveletter(returntype); } if (returntype.isarray()) { return "[" + getdesc(returntype.getcomponenttype()); } return "l" + gettype(returntype) + ";"; } public static string gettype(final class<?> parametertype) { if (parametertype.isarray()) { return "[" + getdesc(parametertype.getcomponenttype()); } if (!parametertype.isprimitive()) { final string clsname = parametertype.getname(); return clsname.replaceall("\\.", "/"); } return getprimitiveletter(parametertype); } public static string getprimitiveletter(final class<?> type) { if (integer.type.equals(type)) { return "i"; } if (void.type.equals(type)) { return "v"; } if (boolean.type.equals(type)) { return "z"; } if (character.type.equals(type)) { return "c"; } if (byte.type.equals(type)) { return "b"; } if (short.type.equals(type)) { return "s"; } if (float.type.equals(type)) { return "f"; } if (long.type.equals(type)) { return "j"; } if (double.type.equals(type)) { return "d"; } throw new illegalstateexception("type: " + type.getcanonicalname() + " is not a primitive type"); } public static type getmethodtype(final class<?> clazz, final string methodname) { try { final method method = clazz.getmethod(methodname, (class<?>[]) new class[0]); return method.getgenericreturntype(); } catch (exception ex) { return null; } } public static type getfieldtype(final class<?> clazz, final string fieldname) { try { final field field = clazz.getfield(fieldname); return field.getgenerictype(); } catch (exception ex) { return null; } } public static void parsearray(final collection collection, final objectdeserializer deser, final defaultjsonparser parser, final type type, final object fieldname) { final jsonlexer lexer = parser.getlexer(); if (lexer.token() == 8) { lexer.nexttoken(16); } parser.accept(14, 14); int index = 0; while (true) { final object item = deser.deserialze(parser, type, (object) index); collection.add(item); ++index; if (lexer.token() != 16) { break; } lexer.nexttoken(14); } parser.accept(15, 16); } }
总结
以上就是本文关于java方法签名的获取实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
上一篇: 七个Spring核心模块详解
下一篇: Java并发编程this逃逸问题总结