La principale recherche de cet article est d'obtenir des signatures de méthode Java, et les éléments suivants sont des exemples de mise en œuvre spécifiques.
Exemple de code:
Package com.yunshouhu; importer java.lang.reflect.field; import java.lang.reflect.method; import java.lang.reflect.type; import java.util.collection; import com.alibaba.fastjson.parser.defaultjson-basser; importation com.alibaba.fastjson.parser.jsonser; import com.alibaba.fastjson.pathser.jsonser; import com.alibaba.fastjson.pathser.jsonser; import com.alibaba. com.alibaba.fastjson.parser.deserializer.ObjectDeserializer; / ** * Obtenez la signature de la méthode Java, reportez-vous à Javah -jni classpath / javap -s classpath. * @author wujianhua * @Date 5 septembre 2017 à 15:25:51 PM * @url http://blog.csdn.net/earbao * / classe publique Asmutilsforjavah {public static void main (String [] args) lance une exception exception {System.out.println (asmutilSforjavah.getDesc (System.class)); System.out.println (asmutilSforjavah.getdesc (String.class)); ystem.out.println (asmutilsforjavah.getdescs (Integer.class)); System.out.println (Asmutilsforjavah.getDesc (int.class)); méthode Method = asmutilsforjavah.class.getDeclaredMethod ("Main", String []. Class); System.out.println ("Javah -jni "); System.out.println (asmutilsforjavah.getdescs (méthode)); System.out.println (Asmutilsforjavah.getType (System.Class)); System.out.println (Asmutilsforjavah.getType (AsmulsforjavaH.class));} Public Static Isandroid (FinalSforjavah.class));} Public Static Isandroid (FinalSforjavah.Class));} Public Static Isandroid (FinalSforjavaH. vmName) {final string bowervmname = vmname.tolowercase (); return bowervmname.contains ("dalvik") || LowerVmName.Contains ("LeMur");} public static boolean isAndroid () {return isAndroid (System.getProperty ("java.vm.name"));} public static stating getDesc (méthode finale) {final stringbuffer buf = new stringbuffer (); buf.endend ("("); final class <?] Tapys = Method.getParameterTypes (); for (int i = 0; i <types.length; ++ i) {buf.append (getDesc (types [i]));} buf.append (")"); buf.append (getDesc (méthode.getReturnType ())); retour buf.tostring ();} public static getdesc (final class <? {if (returnType.isPrimitive ()) {return getPrimiteLetter (returnType);} if (returnType.isArray ()) {return "[" + getDesc (returnType.getComponentType ());} return "l" + getType (returnType) + ";; ParameterType) {if (ParameterType.IsArray ()) {return "[" + getDesc (ParameterType.getComponentType ());} if (! ParameterType.isprimitive ()) {Final String clSname = ParameterType.getName (); return clSname.replaceall ("/" ") getPrimiteLetter (ParameterType);} chaîne statique publique getPrimiteLetter (Final class <?> type) {if (Integer.type.equals (type)) {return "i";} if (void.type.equals (type)) {return "v";} if (boolean.type.equal (Caractères.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)) {retour "j"; (double.type.equals (type)) {return "d";} lance un nouvel illégalstateException ("type:" + type.getCanonicalName () + "n'est pas un type primitif");} public static type getMethodType (final class <?> Clazz, final String Methodname) {try {final methody Method = Clazz.getMethod (Methodnname, (CLASS <] Class [0]); return method.getGerericRetTurnType ();} catch (exception ex) {return null;}} public static type getFieldType (final class <?> Clazz, final string fieldname) {try {final field = cllazz.getfield (fieldname); return field.getGenericType ();} catch (exception ex) {return Null PARSEARRAY (Collection finale Collection, Final ObjectDeserializer Deser, Final DefaultJSonParser Parser, type final Type, Final Object FieldName) {final JSonlexer lexer = PARSER.Getlexer (); if (Lexer.Token ()) = Deser.deserialze (analyser, type, (objet) index); collection.add (item); ++ index; if (lexer.token ()! = 16) {break;} lexer.nextToken (14);} parser.accept (15, 16);}}Ce qui précède est l'intégralité du contenu de cet article sur l'obtention du code d'instance pour les signatures de la méthode Java. J'espère que ce sera utile à tout le monde. Les amis intéressés peuvent continuer à se référer à d'autres sujets connexes sur ce site. S'il y a des lacunes, veuillez laisser un message pour le signaler. Merci vos amis pour votre soutien pour ce site!