java class 文件解析
参考下面两个文章对一个class文件进行解析:
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.6
http://blog.163.com/hfut_quyouhu/blog/static/7847183520127214559314/
java的源代码如下:
package test.java.abs; public class Abs { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub int a = -2;
System.out.println(abs(a));
} public static int abs(int i){
return ~i+1;
} }
编译之后对class 文件的分析结果如下:
CA FE BA BE ----> magic
00 00 -----> minor version
00 32 -----> major version
00 27 -----> constant pool count(39)
-------------------constant pool 1---------------
07 ----->CONSTANT_Class
00 02
-------------------constant pool 2---------------
01
00 11
74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62
73
test/java/abs/Abs
-------------------constant pool 3---------------
07
00 04
-------------------constant pool 4---------------
01
00 10
6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74
java/lang/Object
-------------------constant pool 5---------------
01
00 06
3C 69 6E 69 74 3E
<init>
-------------------constant pool 6---------------
01
00 03
28 29 56
()V
-------------------constant pool 7---------------
01
00 04
43 6F 64 65
Code
-------------------constant pool 8---------------
0A
00 03
00 09
-------------------constant pool 9---------------
0C
00 05
00 06
-------------------constant pool 10---------------
01
00 0F
4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65
LineNumberTable
-------------------constant pool 11---------------
01
00 12
4C 6F 63 61 6C 56 61 72 69 61 62 6C 65 54 61 62 6C 65
LocalVariableTable
-------------------constant pool 12---------------
01
00 04
74 68 69 73
this
-------------------constant pool 13---------------
01
00 13
4C 74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62 73 3B
Ltest/java/abs/Abs;
-------------------constant pool 14---------------
01
00 04
6D 61 69 6E
main
-------------------constant pool 15---------------
01
00 16
28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56
([Ljava/lang/String;)V
-------------------constant pool 16---------------
09
00 11
00 13
-------------------constant pool 17---------------
07
00 12
-------------------constant pool 18---------------
01
00 10
6A 61 76 61 2F 6C 61 6E 67 2F 53 79 73 74 65 6D
java/lang/System
-------------------constant pool 19---------------
0C
00 14
00 15
-------------------constant pool 20---------------
01
00 03
6F 75 74
out
-------------------constant pool 21---------------
01
00 15
4C 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 3B
Ljava/io/PrintStream;
-------------------constant pool 22---------------
0A
00 01
00 17
-------------------constant pool 23---------------
0C
00 18
00 19
-------------------constant pool 24---------------
01
00 03
61 62 73
abs
-------------------constant pool 25---------------
01
00 04
28 49 29 49
(I)I
-------------------constant pool 26---------------
0A
00 1B
00 1D
-------------------constant pool 27---------------
07
00 1C
-------------------constant pool 28---------------
01
00 13
6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D
java/io/PrintStream
-------------------constant pool 29---------------
0C
00 1E
00 1F
-------------------constant pool 30---------------
01
00 07
70 72 69 6E 74 6C 6E
println
-------------------constant pool 31---------------
01
00 04
28 49 29 56
(I)V
-------------------constant pool 32---------------
01
00 04
61 72 67 73
args
-------------------constant pool 33---------------
01
00 13
5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B
[Ljava/lang/String;
-------------------constant pool 34---------------
01
00 01
61
a
-------------------constant pool 35---------------
01
00 01
49
I
-------------------constant pool 36---------------
01
00 01
69
i
-------------------constant pool 37---------------
01
00 0A
53 6F 75 72 63 65 46 69 6C 65
SourceFile
-------------------constant pool 38---------------
01
00 08
41 62 73 2E 6A 61 76 61
Abs.java
00 21 ------> ACC_PUBLIC,ACC_SUPER
00 01 ------> constant pool index 1
00 03 ------> super class
00 00 ------> interface count
00 00 ------> field count
00 03 ------> method count
-----------------method 1----------------------
00 01 ------> ACC_PUBLIC
00 05 ------> name_index <init>
00 06 ------> descriptor_index ()V
00 01 ------> attributes_count
----------------attribute 1 of method 1 --------------
00 07 ------> attribute_name_index Code
00 00 00 2F ------> attribute_length 47
00 01 ------> max_stack
00 01 ------> max_locals
00 00 00 05 ------> code_length
2A B7 00 08 B1
-------------------------instructions----------------------
2A aload_0 this
B7 invokespecial 00 08 Object.init
B1 return
00 00 ------> exception_table_length
00 02 ------> attributes_count
---------------------sub attribute 1-------------
00 0A ------> attribute_name_index LineNumberTable
00 00 00 06 ------> attribute_length
00 01 ------> line_number_table_length
00 00 ------> start_pc
00 03 ------> line_number
---------------------sub attribute 2-------------
00 0B ------> attribute_name_index LocalVariableTable
00 00 00 0C ------> attribute_length
00 01 ------> local_variable_table_length
00 00 ------> start_pc
00 05 ------> length
00 0C ------> name_index this
00 0D ------> descriptor_index Ltest/java/abs/Abs;
00 00 ------> index
-----------------method 2----------------------
00 09 -------> ACC_PUBLIC,ACC_STATIC
00 0E -------> main
00 0F -------> ([Ljava/lang/String;)V
00 01 -------> attributes_count
--------------------attribute 1 of method 2 -----------------------
00 07 ------> attribute_name_index Code
00 00 00 4A ------> attribute_length 74
00 02 ------> max_stack
00 02 ------> max_locals
00 00 00 0E ------> code_length
10 FE 3C B2 00 10 1B B8 00 16 B6 00 1A B1
-------------------------instructions----------------------
10 bipush FE(-2)
3C istore_1
B2 getstatic 00 10 System.out
1B iload_1
B8 invokestatic 00 16 abs()
B6 invokevirtual 00 1A println()
B1 return
00 00 ------> exception_table_length
00 02 ------> attributes_count
----------------------sub attribute 1 -----------------------
00 0A ----------> attribute_name_index LineNumberTable
00 00 00 0E ----------> attribute_length
00 03 ----------> line_number_table_length
00 00 ----------> start_pc
00 0B ----------> line_number
00 03 ----------> start_pc
00 0C ----------> line_number
00 0D ----------> start_pc
00 0D ----------> line_number
----------------------sub attribute 2 -----------------------
00 0B ----------> attribute_name_index LocalVariableTable
00 00 00 16 ----------> attribute_length
00 02 ----------> local_variable_table_length
00 00 ----------> start_pc
00 0E ----------> length
00 20 ----------> name_index args
00 21 ----------> descriptor_index [Ljava/lang/String;
00 00 ----------> index 0
00 03 ----------> start_pc
00 0B ----------> length
00 22 ----------> name_index a
00 23 ----------> descriptor_index I
00 01 ----------> index 1
-----------------method 3----------------------
00 09 -------> ACC_PUBLIC,ACC_STATIC
00 18 -------> abs
00 19 -------> (I)I
00 01 -------> attribute_count
-------------------------attribute 1 of method 3 --------------------
00 07 ------> attribute_name_index Code
00 00 00 30 ------> attribute_lenght 48
00 02 ------> max_stack
00 01 ------> max_locals
00 00 00 06 ------> code_length
1A 02 82 04 60 AC
-------------------------instructions----------------------
1A iload_0
02 iconst_m1
82 ixor
04 iconst_1
60 iadd
AC ireturn
00 00 ------> exception_table_length
00 02 ------> attributes_count
------------------------sub attribute 1-----------------------
00 0A ----------> attribute_name_index LineNumberTable
00 00 00 06 ----------> attribute_length
00 01 ----------> line_number_table_length
00 00 ----------> start_pc
00 10 ----------> line_number
------------------------sub attribute 2-----------------------
00 0B ----------> attribute_name_index LocalVariableTable
00 00 00 0C ----------> attribute_length
00 01 ----------> local_varialbe_table_length
00 00 ----------> start_pc
00 06 ----------> length
00 24 ----------> name_index i
00 23 ----------> descriptor_index I
00 00 ----------> index
------------------------ class file attribute ----------------------------------
00 01 ---------> attribute count
00 25 ---------> attribute_name_index SourceFile
00 00 00 02 ---------> attribute_length
00 26 ---------> sourcefile_index Abs.java
java class 文件解析的更多相关文章
- javap -- Java 类文件解析器
参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...
- java 读文件 解析
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
- Java XML文件解析
四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例) 蓝色字体内容由上一篇博文中补充进来的,写作风格比较好,大家有兴趣可以去查看原文 众所周知,现在解析XML的方法越来越多,但主流的方法也就四 ...
- java 写文件解析
import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- java jar包解析:打包文件,引入文件
java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...
- 《热血传奇2》wix、wil文件解析Java实现
在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ...
- java使用POI进行 Excel文件解析
package com.timevale.esign.vip.util; import java.io.File; import java.io.FileInputStream; import jav ...
- Atitit。Tree文件解析器的原理流程与设计实现 java c# php js
Atitit.Tree文件解析器的原理流程与设计实现 java c# php js 1. 解析原理与流程1 1.1. 判断目录 ,表示服 dirFlagChar = "└├─&quo ...
随机推荐
- tcpdump dump 网络流量
tcpdump dump 网络流量 描述: Tcpdump 打印一个包的内容的描述在一个网络接口 匹配布尔值表达式 它也可以运行使用-w flag, 这样会保存包的数据到一个文件用于后面分析, 使用- ...
- 生成Excel錯誤 遠端程序呼叫失敗。 (發生例外狀況於 HRESULT: 0x800706BE)
错误信息:详细信息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005.网上找到 ...
- c# post文字图片至服务器
最近由于项目需要实现c#提交文字及数据至服务器,因此研究了一下c# php数据传送: 下面用一个示例来演示,c# post文字+图片 ,php端接收: post提交数据核心代码(post数据提交) ? ...
- AHOI2009最小割
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1072 Solved: 446[Submit] ...
- 【转】基本数据持久性(一) 使用plist保存和读取数据
原文网址:http://www.it165.net/pro/html/201309/7170.html 想保存成绩.记录得分.保存账号密码等等?数据持久性可以做到这一点!这篇文章通过简单的程序,来分享 ...
- 让ASP.NET MVC页面返回不同类型的内容
在ASP.NET MVC的controller中大部分方法返回的都是ActionResult,更确切的是ViewResult.它返回了一个View,一般情况下是一个HTML页面.但是在某些情况下我们可 ...
- Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法
一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...
- [原]H264帧内预测
帧内预测模块大小 说明 4x4(亮度) 预测方式9种 8x8(亮度) 预测方式9种.只有high profile才有 16x16(亮度) 预测方式4种,只依赖左,上数据. 8x8(色度) 预测方式4种 ...
- android用ImageView显示网络图片
1.权限配置 <</SPAN>uses-permission android:name="android.permission.INTERNET"/> .c ...
- Mac IDEA快捷键积累
切来切去:Ctrl+Tab 在打开的不同类中切换来切换去 完美代码结构:Alt+Command+L 不同编辑点跳转:Alt+Command+↔️ 快速重写:Ctrl+N 快速选择代码:Alt+Shif ...