La investigación principal en este artículo es obtener firmas del método Java, y los siguientes son ejemplos de implementación específicos.
Código de ejemplo:
paquete 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.Parpar.Parpar. com.alibaba.fastjson.parser.deserializer.objectDeserializer;/*** Get Java Method Firma, consulte Javah -Jni classpath/javap -s classpath. * @author wujianhua * @Date 5 de septiembre de 2017 a las 3:25:51 pm * @url http://blog.csdn.net/earbao */public class asmutilsforjavah {public static void main (string [] args) Excepción {System.out.println (asmutilsforjavah.getdesc (system.class)); system.out.println (asmutilsforjavah.getdesc (string.class)); S ystem.out.println (asmutilsforjavah.getdesc (integer.class)); system.out.println (asmutilsforjavah.getdesc (int.class)); método método = asmutilsforjavah.class.getDeclaredMethod ("main", string []. class); system.out.println ("Javah -jni "); system.out.println (asmutilsforjavah.getDesc (método)); system.out.println (asmutilsforjavah.gettype (system.class)); system.out.println (asmutilsforJavaH.gettype (asmutilsforjavah.class); vmname) {String final 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 (método final del método) {StringBuffer Buf = new StringBuffer (); buf.append ("("); Tipos de clase final <?> [] [] método.getParametertyPes (); for (int i = 0; i <tipos.length; ++ i) {buf.append (getDesc (tipos [i]));} buf.append (")"); buf.append (getDesc (método.getRetRetnype ()); return Buf.toString ();} Public static string getDesc (final classype (? {if (returnType.IsPrimitive ()) {return getPrimitiveLetter (returnType);} if (returntype.isArray ()) {return "[" + getDesc (returnType.getComponentType ();} return "l" + getType (returnType) + ";";} public static string getType class parametertype) {if (parametertype.isArray ()) {return "[" + getDesc (parametType.getComponentType ();} if (! ParametType.IsPrimitive ()) {final de cadena clsname = parametType.getName (); return clsname.replaceall ("// getPrimitiveLetter (parametertype);} public static string getPrimitiveLetter (final de clase final <?> type) {if (integer.type.equals (type)) {return "i";} if (void.type.equals (type) {return "v";} if (boolean.type.equals (type)) {returns)};};};} (Caracteres.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) (double.type.equals (type)) {return "d";} throw new IlegalStateException ("type:" + type.getCanonicalName () + "no es un tipo primitivo");} public static type getMethodPe (clase final <?> Clazz, final de cadena MethodName) {intento {método final Method = Clazz.getMethod (MethodNetmet <s Class <s Class <? Clase [0]); return Method.getGenericReturnType ();} Catch (Exception ex) {return null;}} public static static type getFieldType (clase final <?> Clazz, string final fieldName) {try {Field Field final = Clazz.getfield (fieldName); return.getGenericType ();} Catch (Exception ex) {regreso null;} Parsearray (colección final, colección final, final de objectDeserializer deser, final DefaultjsonParser Parser, tipo de tipo final, nombre de objeto final) {final jsonlexer lexer = parser.getlexer (); if (lexer.token () == 8) {lexer.nexttoken (16);} parser.accept (14, 14); int index) deser.deserialze (parser, type, (objeto) índice); colección.add (item); ++ index; if (lexer.token ()! = 16) {break;} lexer.nexttoken (14);} parser.accept (15, 16);}}}Lo anterior es todo el contenido de este artículo sobre la obtención del código de instancia para las firmas del método Java. Espero que sea útil para todos. Los amigos interesados pueden continuar referiéndose a otros temas relacionados en este sitio. Si hay alguna deficiencia, deje un mensaje para señalarlo. ¡Gracias amigos por su apoyo para este sitio!