Maison  >  Article  >  Java  >  Outils Javap et exemples en Java

Outils Javap et exemples en Java

PHPz
PHPzavant
2023-08-28 08:09:06975parcourir

Outils Javap et exemples en Java

L'outil javap est un moyen utile de récupérer des informations sur une classe ou une interface spécifique. Grâce à ses capacités de désassemblage, la commande javap (également connue sous le nom de désassembleur Java) a pour rôle de fournir des informations internes complètes à l'utilisateur. A partir des deux options -c ou -verbose, dont chacune donne des résultats uniques, l'utilisateur peut se retrouver dans le domaine du bytecode et de l'orchestration du bytecode. Si aucune option n'est utilisée, javap exécute les champs et méthodes publics, protégés et package de la classe d'entrée.

Grammaire

javap [option] [classname]

Lorsque vous n'utilisez pas les options

Exemple

javap class_name

Sortie

C:\Users\Aashi>javap java.lang.Object 
Compiled from "Object.java"
public class java.lang.Object {
   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait (long) throws java.lang.InterruptedException;
   public final void wait(long, int) throws java.lang.InterruptedException;
   public final void wait() throws java.lang.InterruptedException;
   protected void finalize() throws java.lang.Throwable;
   static {};
}

Quand utiliser les options

Voici les instructions pour chaque option et comment l'appliquer -

  • -aide ou --aide ou -? -

Ceci est utilisé pour imprimer un message d'aide pour la commande javap.

Exemple

javap -help

Sortie

C:\Users\Aashi>javap -help 
Usage: javap <options> <classes> 
where possible options include: 
-help --help -?      Print this usage message
-version             Version information
-V  -verbose         Print additional information
-1                   Print line number and local variable tables
-public              Show only public classes and members
-protected           Show protected/public classes and members
-package             Show package/protected/public classes and members (default)
-p -private          Show all classes and members
-C                   Disassemble the code
-S                   Print internal type signatures
-sysinfo             Show system info (path, size, date, MD5 hash) of class being processed
-constants           Show final constants
-classpath <path>    Specify where to find user class files
-cp <path>           Specify where to find user class files
-bootclasspath <path> Override location of bootstrap class files
  • -version

Utilisé pour imprimer les informations sur la version Java.

Exemple

javap -version

Sortie

C:\Users\Aashi>javap -version
1.8.0_151
  • -v ou -verbeux -

Ceci est utilisé pour imprimer des informations supplémentaires telles que la taille de la pile, le nombre de variables locales et les paramètres de la méthode.

Exemple

javap -v class_name

Sortie

C:\Users\Aashi>javap -v java.lang.Object
Classfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/jre/lib/rt.jar! /java/lang/Object.class
Last modified Sep 5, 2017; size 1497 bytes
MD5 checksum 074ebc688a81170b8740f1158648a3c7 
Compiled from "Object.java"
public class java.lang.Object 
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Integer         999999
#2 - String          #16                //@
#3 = String          #38                //nanosecond timeout value out of range
#4 = String          #42               //timeout value is negative
#5 - Utf8            ()I
#6 = Utf8            ()Ljava/lang/Object;
#7 - Utf8            ()Ljava/lang/String;
#8 = Utf8            ()V
#9 - Utf8            (I)Ljava/lang/String;
#10 - Utf8           (J)V
#11 = Utf8           (JI)V
#12 - Utf8           (Ljava/lang/Object;)Z
#13 = Utf8           (Ljava/lang/String;)V
#14 - Utf8           <clinit>
#15 = Utf8           <init>
#16 - Utf8           @
#17 - Utf8           Code
#18 = Utf8           Exceptions
#19 - Utf8           Line Number Table
#20 = Utf8           Signature
#21- Utf8            Source File
#22 = Utf8           StackMapTable
#23 Utf8             Append
#24 Utf8             Clone
#25 = Utf8           Equals
#26 Utf8             finalize
#27 = Utf8           getClass
#28 Utf8             getName
#29 - Utf8           hashCode
#30 = Utf8           java/lang/Class
#31 - Utf8           java/lang/CloneNotSupportedException
#32 = Utf8           java/lang/IllegalArgumentException
#33 - Utf8           Java/lang/Integer
#34 Utf8             java/lang/InterruptedException
  • -l -

Ceci est utilisé pour imprimer les numéros de ligne et les tableaux de variables locales.

Exemple

javap -l class_name

Sortie

C:\Users\Aashi>javap -1 java.lang.Object 
Compiled from "Object.java” 
public class java.lang.Object { 
   public java.lang.Object();
   LineNumberTable:
   line 37: 0

   public final native java.lang.Class<?> getClass();

   public native int hashCode();

   public boolean equals(java.lang.Object);
   LineNumberTable:
   line 149: 0

   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;

   public java.lang.String toString();
   LineNumberTable:
   line 236: 0

   public final native void notify();

   public final native void notifyAll();

   public final native void wait(long) throws Java.lang.InterruptedException;
   public final void wait(long, int) throws java.lang.InterruptedException;
   LineNumberTable:
   line 447: 0
   line 448: 6 
   line 451: 16 
   line 452: 26 
   line 456: 36 
   line 457: 40 
   line 460: 44
   line 461: 49

   public final void wait() throws java.lang.InterruptedException;
   LineNumberTable:
   line 502:0
   line 503: 5
   protected void finalize() throws java.lang.Throwable;
}
  • -Public

Ceci est utilisé pour imprimer uniquement les classes publiques et les membres.

Exemple

javap -public class_name

Sortie

C:\Users\Aashi>javap -public java.lang.Object 
Compiled from "Object.java" 
public class java.lang.Object {
   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait (long) throws java.lang.InterruptedException; public final void wait (long, int) throws java.lang.InterruptedException; public final void wait() throws java.lang.InterruptedException;
}
  • -Protégé

Ceci est utilisé pour imprimer les classes et les membres protégés/publics.

Exemple

javap -protected class_name

Sortie

C:\Users\Aashi>javap -protected java.lang.Object 
Compiled from "Object.java"
public class java.lang.Object {
   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; 
   public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait(long) throws java.lang.InterruptedException;
   public final void wait (long, int) throws java.lang.InterruptedException;
   public final void wait() throws java.lang.InterruptedException;
   protected void finalize() throws java.lang.Throwable;
}
  • -sac

Ceci est utilisé pour imprimer les classes et les membres des packages/protégés/publics (par défaut).

Exemple

javap -package class_name

Sortie

C:\Users\Aashi>javap -package java.lang.Object 
Compiled from "Object.java"
   public class java.lang.Object {
   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; 
   public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait (long) throws java.lang.InterruptedException;
   public final void wait (long, int) throws java.lang.InterruptedException;
   public final void wait() throws java.lang.InterruptedException;
   protected void finalize() throws java.lang.Throwable;
   static {};
}
  • -c

Ceci est utilisé pour imprimer le code de démontage.

Exemple

javap -c class_name

Sortie

C:\Users\Aashi>javap -c java.lang.Object 
Compiled from "Object.java"
public class java.lang.Object (public java.lang.Object();
Code:
0: return
public final native java.lang.Class<?> getClass();

public native int hashCode();

public boolean equals(java.lang.Object);
Code:
0: aload e
1: aload_1
2: 1f_acmpne   9
5:iconst_1 
6: goto        10
9: iconst_0
10: ireturn

protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;

public java.lang.String toString();
Code:
0: new             #52      //class java/lang/StringBuilder
3: dup
4: invokespecial   #74     //Method java/lang/StringBuilder."<init>": ()V
7: aload_e
8: invokevirtual   #73    // Method getClass: ()Ljava/lang/Class;
11: invokevirtual  #67    // Method java/lang/class.getName:()Ljava/lang/String;
14: invokevirtual  #76   // Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder;
17: 1dc            #2    // String @
19: invokevirtual  #76   // Method java/lang/StringBuilder.append: (Ljava/lang/String;)Ljava/lang/StringBuilder;
22: aload_e
23: invokevirtual  #78   // Method hashCode: ()I
26: invokestatic   #69   // Method java/lang/Integer.toHexString: (I) Ljava/lang/String;
29: invokevirtual  #76   // Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder;
32: invokevirtual  #75   // Method java/lang/StringBuilder.toString: ()Ljava/lang/String;
35: areturn
public final native void notify();
  • -s -

Ceci est utilisé pour imprimer des signatures de type internes.

Exemple

javap -s class_name

Sortie

C:\Users\Aashi>javap -s java.lang.Object 
Compiled from "Object.java" 
public class java.lang.Object { 
   public java.lang.Object(); 
   descriptor: ()V

   public final native java.lang.Class<?> getClass(); 
   descriptor: ()Ljava/lang/class;

   public native int hashCode();
   descriptor: ()I

   public boolean equals(java.lang.Object);
   descriptor: (Ljava/lang/Object;)Z

   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; 
   descriptor:()Ljava/lang/Object;

   public java.lang.String toString();
   descriptor: ()Ljava/lang/String;

   public final native void notify();
   descriptor: ()V

   public final native void notifyAll();
   descriptor: ()V

   public final native void wait(long) throws java.lang.InterruptedException; descriptor: (J)V

   public final void wait(long, int) throws java.lang.InterruptedException; descriptor: (JI)V

   public final void wait() throws java.lang.InterruptedException; 
   descriptor: ()V
   protected void finalize() throws java.lang.Throwable;
   descriptor: ()V
   static {}; 
   descriptor: ()V
}
  • -sysinfo -

Ceci est utilisé pour imprimer les informations système (chemin, taille, date, hachage MD5) de la classe en cours de traitement.

Exemple

javap -sysinfo class_name

Sortie

C:\Users\Aashi>javap -sysinfo java.lang.Object
Classfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/jre/lib/rt.jar! /java/lang/Object.class 
Last modified Sep 5, 2017; size 1497 bytes
MD5 checksum 074ebc688a81170b8740f1158648a3c7 
Compiled from "Object.java"
public class java.lang.Object {
   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
   public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait (long) throws java.lang.InterruptedException;
   public final void wait (long, int) throws java.lang.InterruptedException;
   public final void wait() throws java.lang.InterruptedException;
   protected void finalize() throws java.lang.Throwable;
   static {};
}
  • -constant -

Ceci est utilisé pour imprimer les constantes finales de la classe.

Exemple

javap -constants class_name

Sortie

C:\Users\Aashi>javap -constants java.lang.Object 
Compiled from "Object.java"
public class java.lang.Object {

   public java.lang.Object();
   public final native java.lang.Class<?> getClass();
   public native int hashCode();
   public boolean equals(java.lang.Object);
   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; 
   public java.lang.String toString();
   public final native void notify();
   public final native void notifyAll();
   public final native void wait (long) throws java.lang.InterruptedException;
   public final void wait(long, int) throws java.lang.InterruptedException;
   public final void wait() throws java.lang.InterruptedException;
   protected void finalize() throws java.lang.Throwable;
   static {}; 
}

Conclusion

L'outil Javap est un outil multiforme et très puissant pour les codeurs Java, leur permettant de diagnostiquer et de déboguer les documents de classe, d'évaluer les champs et les méthodes et de générer du code propre à partir du bytecode des documents de classe. Cet outil indispensable convient aux développeurs Java de tous niveaux, leur permettant d'élargir facilement leurs connaissances et de dépanner les programmes Java. L'outil Javap est un bien irremplaçable pour tout programmeur en herbe et il l'aidera sûrement à améliorer rapidement ses compétences en programmation. De plus, son large éventail de fonctionnalités aide les codeurs à affiner leurs pratiques, leur donnant ainsi un avantage concurrentiel lors de la création d'applications Java.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer
Article précédent:Java et C#Article suivant:Java et C#