The main research in this article is to obtain Java method signatures, and the following are specific implementation examples.
Example code:
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;/** * Get Java method signature, refer to javah -jni classpath/javap -s classpath. * @author WuJianHua * @date September 5, 2017 at 3:25:51 pm * @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);}}The above is the entire content of this article about obtaining instance code for Java method signatures. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!