how to use javap command
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:
class OutterClass{
public int var1;
private int var2; public int getVar2(){return var2;}; class InnerClass{
public int ivar1;
private int ivar2; public int getSum(){return var1+var2+ivar1+ivar2;} }; };
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();
}
how to use javap command的更多相关文章
- How to decompile class file in Java and Eclipse - Javap command example(转)
Ability to decompile a Java class file is quite helpful for any Java developer who wants to look int ...
- javap反编译解释外部类直接使用内部类private字段的原理
2016-07-04 15:56:39 我们都知道: 1.内部类可以直接访问外部类的private字段和方法: 2.非静态内部类持有外部类的引用: 3.外部类可以直接访问内部类的private字段和方 ...
- 无限递归的构造器和javap使用指南
无限递归的构造器和javap使用指南 public class ConstructorRecursion { ConstructorRecursion rc; { rc = newConstructo ...
- Beginning Scala study note(9) Scala and Java Interoperability
1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...
- java JNI 的实现(2)-java和C/C++的相互调用.
目录 概述 一,java代码 二,稍微注意通过javah生成的'C/C++'.h头文件和源java代码的关系 三,在C/C++中实现java的native方法(完整C/C++) 1,修改age,即Ja ...
- JVM(五) class类文件的结构
概述 class类文件的结构可见下面这样图(出处见参考资料),可以参照下面的例子,对应十六进制码,找出找出相应的信息. 其中u2 , u4 表示的意思是占用两个字节和占用四个字节,下面我们将会各项说明 ...
- Eclipse中使用javap运行配置详解
javap是sun提供的对class文件进行反编译的工具 1.配置Run---external tools---external tools configurations 选择Program 新建ja ...
- 在eclipse中使用javap工具反汇编
1.配置 Run---external tools---external tools configurations 选择Program 新建javap运行方式 设置location.workspace ...
- JDK的命令行工具系列 (二) javap、jinfo、jmap
javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...
随机推荐
- SQLServer 2012之AlwaysOn —— 指定数据同步链路,消除网络抖动导致的提交延迟问题
事件起因:近期有研发反应,某数据库从08切换到12环境后,不定期出现写操作提交延迟的问题: 事件分析:在排除了系统资源争用等问题后,初步分析可能由于网络抖动导致同步模式alwayson节点经常出现会话 ...
- 跟我一起学WCF(9)——WCF回调操作的实现
一.引言 在上一篇文章中介绍了WCF对Session的支持,在这篇文章中将详细介绍WCF支持的操作.在WCF中,除了支持经典的请求/应答模式外,还提供了对单向操作.双向回调操作模式的支持,此外还有流操 ...
- C#设计模式(13)——代理模式(Proxy Pattern)
一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...
- C#设计模式(8)——桥接模式(Bridge Pattern)
一.引言 这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视 ...
- [ALM]一步一步搭建MS ALM环境 - 安装域服务器
描述: 搭建并配置域服务器,先安装操作系统,配置网络,安装组件,配置域帐号 步骤: 1,打开Hyper-V Manager,参考[Hyper-V]使用操作系统模板创建新的虚拟机,先完成操作系统的安装, ...
- Android开发学习总结——Android开发的一些相关概念
一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 3G指的是第三代移 ...
- p4 是否能自动merge
总结: 1)如果在copy merge(-at)/auto merge(-am)后修改source branch,则可以自动被copy merge: 2)如果在manual merge后修改sou ...
- jQuery的XX如何实现?——2.show与链式调用
往期回顾: jQuery的XX如何实现?——1.框架 -------------------------- 源码链接:内附实例代码 jQuery使用许久了,但是有一些API的实现实在想不通.于是抽空看 ...
- paip.不同目录结构哈的文件批量比较
paip.不同目录结构哈的文件批量比较 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- Linux中ctrl-c, ctrl-z, ctrl-d 区别
在Linux中: ctrl-c: ( kill foreground process ) 发送 SIGINT 信号给前台进程组中的所有进程,强制终止程序的执行: ctrl-z: ( suspend ...