how to use javap command

8051 ワード

SYNOPSIS
javap [options] classes
DESCRIPTION
The javap command is called "disassembler"because it takes apart class files and tells you what's inside them. We won't use it often but sometimes it is very useful to find out how a particular java statement works out, especially for inner classes and so on.
OPTIONS
-l: Prints out line and local variable tables.
-b: Ensures backward compatibility with javap in JDK 1.1.
-public: Shows only public classes and members.
-protected: Shows only protected and public classes and members.
-package: Shows only packages, protected, and public classes and members. This is the default.
-private: Shows all classes and members.
-J flag: Pass flag directly to the runtime system.
-s: Prints internal type signatures.
-c: Prints out disassembled code, i.e., the instrctions that comprise the Java bytecodes, for each of the methods in the class.
-classpath path: Specifies the path javap uses to look up classes. 
-h: Generate code which can be put into a C language header file.
-verbose: Prints stack size, number of locals and args for methods.
-verify: Performs a partial verification of the class file.
-version: Prints out the javap version string.
EXAMPLES
This is a java class with an inner class:
 1 class OutterClass{
 2     public int var1;
 3     private int var2;
 4 
 5     public int getVar2(){return var2;};
 6 
 7     class InnerClass{
 8         public int ivar1;
 9         private int ivar2;
10 
11         public int getSum(){return var1+var2+ivar1+ivar2;}
12 
13     };
14 
15 
16 };

Then compile it to the class file:
javac test.java

Unfortunately, it doesn't work since I didn't set the local environment of path and home. So I set path-jdk/bin and home-jdk.
So I got two bytetype class files OutterClass.class and OutterClass$InnerClass.class.
The output of javap OutterClass.class yields:
Compiled from "test.java"
class OutterClass {
  public int var1;
  OutterClass();
  public int getVar2();
  static int access$000(OutterClass);
}

The output of javap OutterClass$InnerClass.class yields:
Compiled from "test.java"
class OutterClass$InnerClass {
  public int ivar1;
  final OutterClass this$0;
  OutterClass$InnerClass(OutterClass);
  public int getSum();
}

 
 In this way, we can understand how inner class works. Inner classes can access data in the scope in which they are defined, even if the fileds that would otherwise be private. In inner classes, we get the reference of the outer class in this way we can access their data. The outer class reference is set in the constructor. The compiler modifies all inner class constructors, adding a parameter for the outer class reference.
Inner classes are a phenomenon of the compiler, not the virtual machine. Inner classes are translated into regulat class files with & delimiting outer and inner class names, and the virtual mathine does not have any special knowledge about them.
However, the reference of the outer class can only access data that are declared public. How can inner classes access private data? We can find that there is a get method static int access$000(OutterClass);  generated by the compiler which would be used for the inner class to access their private data. 
But there is a great security risk in it. If an inner class accesses a private data field, then it is possible to access that data field through other classes that are added to the package. But to do so requires skill and determination. A programmer can not accidentally obtain accesss but must intentionally build or modify a class file for that purpose.
Now let's have a look at the private parameter of javap.
The output of javap -private OutterClass.class yields:
Compiled from "test.java"
class OutterClass {
  public int var1;
  private int var2;
  OutterClass();
  public int getVar2();
  static int access$000(OutterClass);
}

The output of javap -private OutterClass$InnerClass.class yields:
Compiled from "test.java"
class OutterClass$InnerClass {
  public int ivar1;
  private int ivar2;
  final OutterClass this$0;
  OutterClass$InnerClass(OutterClass);
  public int getSum();
}