当前位置:   article > 正文

获取Java的方法签名_java获取方法签名

java获取方法签名
  1. package com.yunshouhu;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Type;
  5. import java.util.Collection;
  6. import com.alibaba.fastjson.parser.DefaultJSONParser;
  7. import com.alibaba.fastjson.parser.JSONLexer;
  8. import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
  9. /**
  10. * 获取Java的方法签名,参考javah -jni 类路径/javap -s 类路径。
  11. * @author WuJianHua
  12. * @date 2017年9月5日 下午3:25:51
  13. * @url http://blog.csdn.net/earbao
  14. */
  15. public class ASMUtilsForJavaH {
  16. public static void main(String[] args) throws Exception {
  17. System.out.println(ASMUtilsForJavaH.getDesc(System.class));
  18. System.out.println(ASMUtilsForJavaH.getDesc(String.class));
  19. System.out.println(ASMUtilsForJavaH.getDesc(Integer.class));
  20. System.out.println(ASMUtilsForJavaH.getDesc(int.class));
  21. Method method=ASMUtilsForJavaH.class.getDeclaredMethod("main", String[].class);
  22. System.out.println("javah -jni");
  23. System.out.println(ASMUtilsForJavaH.getDesc(method));
  24. System.out.println(ASMUtilsForJavaH.getType(System.class));
  25. System.out.println(ASMUtilsForJavaH.getType(ASMUtilsForJavaH.class));
  26. }
  27. public static boolean isAndroid(final String vmName) {
  28. final String lowerVMName = vmName.toLowerCase();
  29. return lowerVMName.contains("dalvik") || lowerVMName.contains("lemur");
  30. }
  31. public static boolean isAndroid() {
  32. return isAndroid(System.getProperty("java.vm.name"));
  33. }
  34. public static String getDesc(final Method method) {
  35. final StringBuffer buf = new StringBuffer();
  36. buf.append("(");
  37. final Class<?>[] types = method.getParameterTypes();
  38. for (int i = 0; i < types.length; ++i) {
  39. buf.append(getDesc(types[i]));
  40. }
  41. buf.append(")");
  42. buf.append(getDesc(method.getReturnType()));
  43. return buf.toString();
  44. }
  45. public static String getDesc(final Class<?> returnType) {
  46. if (returnType.isPrimitive()) {
  47. return getPrimitiveLetter(returnType);
  48. }
  49. if (returnType.isArray()) {
  50. return "[" + getDesc(returnType.getComponentType());
  51. }
  52. return "L" + getType(returnType) + ";";
  53. }
  54. public static String getType(final Class<?> parameterType) {
  55. if (parameterType.isArray()) {
  56. return "[" + getDesc(parameterType.getComponentType());
  57. }
  58. if (!parameterType.isPrimitive()) {
  59. final String clsName = parameterType.getName();
  60. return clsName.replaceAll("\\.", "/");
  61. }
  62. return getPrimitiveLetter(parameterType);
  63. }
  64. public static String getPrimitiveLetter(final Class<?> type) {
  65. if (Integer.TYPE.equals(type)) {
  66. return "I";
  67. }
  68. if (Void.TYPE.equals(type)) {
  69. return "V";
  70. }
  71. if (Boolean.TYPE.equals(type)) {
  72. return "Z";
  73. }
  74. if (Character.TYPE.equals(type)) {
  75. return "C";
  76. }
  77. if (Byte.TYPE.equals(type)) {
  78. return "B";
  79. }
  80. if (Short.TYPE.equals(type)) {
  81. return "S";
  82. }
  83. if (Float.TYPE.equals(type)) {
  84. return "F";
  85. }
  86. if (Long.TYPE.equals(type)) {
  87. return "J";
  88. }
  89. if (Double.TYPE.equals(type)) {
  90. return "D";
  91. }
  92. throw new IllegalStateException("Type: " + type.getCanonicalName() + " is not a primitive type");
  93. }
  94. public static Type getMethodType(final Class<?> clazz, final String methodName) {
  95. try {
  96. final Method method = clazz.getMethod(methodName, (Class<?>[]) new Class[0]);
  97. return method.getGenericReturnType();
  98. } catch (Exception ex) {
  99. return null;
  100. }
  101. }
  102. public static Type getFieldType(final Class<?> clazz, final String fieldName) {
  103. try {
  104. final Field field = clazz.getField(fieldName);
  105. return field.getGenericType();
  106. } catch (Exception ex) {
  107. return null;
  108. }
  109. }
  110. public static void parseArray(final Collection collection, final ObjectDeserializer deser,
  111. final DefaultJSONParser parser, final Type type, final Object fieldName) {
  112. final JSONLexer lexer = parser.getLexer();
  113. if (lexer.token() == 8) {
  114. lexer.nextToken(16);
  115. }
  116. parser.accept(14, 14);
  117. int index = 0;
  118. while (true) {
  119. final Object item = deser.deserialze(parser, type, (Object) index);
  120. collection.add(item);
  121. ++index;
  122. if (lexer.token() != 16) {
  123. break;
  124. }
  125. lexer.nextToken(14);
  126. }
  127. parser.accept(15, 16);
  128. }
  129. }

描述符与特征签名的区别_不穿铠甲的穿山甲的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/860819
推荐阅读
相关标签
  

闽ICP备14008679号