参考下面两个文章对一个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的源代码如下:

  1. package test.java.abs;
  2.  
  3. public class Abs {
  4.  
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10.  
  11. int a = -2;
  12. System.out.println(abs(a));
  13. }
  14.  
  15. public static int abs(int i){
  16. return ~i+1;
  17. }
  18.  
  19. }

编译之后对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 文件解析的更多相关文章

  1. javap -- Java 类文件解析器

    参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...

  2. java 读文件 解析

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

  3. Java XML文件解析

    四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例) 蓝色字体内容由上一篇博文中补充进来的,写作风格比较好,大家有兴趣可以去查看原文 众所周知,现在解析XML的方法越来越多,但主流的方法也就四 ...

  4. java 写文件解析

    import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...

  5. JAVA使用SAX解析XML文件

    在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...

  6. java jar包解析:打包文件,引入文件

    java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...

  7. 《热血传奇2》wix、wil文件解析Java实现

    在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ...

  8. java使用POI进行 Excel文件解析

    package com.timevale.esign.vip.util; import java.io.File; import java.io.FileInputStream; import jav ...

  9. Atitit。Tree文件解析器的原理流程与设计实现  java  c# php js

    Atitit.Tree文件解析器的原理流程与设计实现  java  c# php js 1. 解析原理与流程1 1.1. 判断目录  ,表示服  dirFlagChar = "└├─&quo ...

随机推荐

  1. 手势识别官方教程(4)在挑划或拖动手势后view的滚动用ScrollView和 HorizontalScrollView,自定义用Scroller或OverScroller

    简单滚动用ScrollView和 HorizontalScrollView就够.自定义view时可能要自定义滚动效果,可以使用 Scroller或 OverScroller Animating a S ...

  2. centos6.5安装vbox

    cd /etc/yum.repos.d wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo 下载跟CENTO ...

  3. SQL语句优化方法

    1.1 注释使用 在语句中多写注释,注释不影响SQL语句的执行效率.增加代码的可读性. 1.2 对于事务的使用 尽量使事务处理达到最短,如果事务太长最好按功能将事务分开执行(如:可以让用户在界面上多几 ...

  4. BZOJ_1096_[ZJOI2007]_仓库建设_(斜率优化动态规划+单调队列+特殊的前缀和技巧)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1096 有\(n\)个工厂,给出第\(i\)个工厂的到1号工厂的距离\(x[i]\),货物数量\ ...

  5. WordPress OptimizePress插件任意文件上传漏洞

    漏洞版本: WordPress OptimizePress Plugin 1.x 漏洞描述: WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设 ...

  6. powerdesigner导出SQL时大写转换

    打开物理模型,点击菜单“Tools->Execute Commands -> Edit/Run Script...",或者快捷键(Ctrl+Shift+X)执行下面vbscrip ...

  7. LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树

    分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...

  8. oracle 的 regexp_substr()的用法

    将字符串'1-2' 截取出来成1 .2,然后通过这两个值查出数据张三.李四  ,拼接起来成'张三-李四' oracle 语法 SELECT replace(WMSYS.WM_CONCAT(wrxm), ...

  9. jps 显示process information unavailable解决方法

    jps 显示process information unavailable解决办法jps时出现如下信息: 4791 -- process information unavailable 解决办法: 进 ...

  10. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...