科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Class文件详解

Class文件详解

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

  我们都知道,Java编译器负责将.java文件编译成.class文件,class文件存储的是java字节码,与.java文件无关(只要你愿意写一个编译器,也可以将别的语言写的源代码编译成.class文件)。

作者:中国IT实验室 来源:中国IT实验室 2007年9月26日

关键字: 编程 java

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)


¡¡¡¡ÎÒÃǶ¼ÖªµÀ£¬Java±àÒëÆ÷¸ºÔð½«.javaÎļþ±àÒë³É.classÎļþ£¬classÎļþ´æ´¢µÄÊÇjava×Ö½ÚÂ룬Óë.javaÎļþÎ޹أ¨Ö»ÒªÄãÔ¸Òâдһ¸ö±àÒëÆ÷£¬Ò²¿ÉÒÔ½«±ðµÄÓïÑÔдµÄÔ´´úÂë±àÒë³É.classÎļþ£©£¬±¾ÎÄ×¼±¸Ïêϸ½âÆÊclassÎļþµÄÄÚ²¿½á¹¹£¬²¢ÇÒ°ÑclassÎļþ½á¹¹¶ÁÈ¡²¢ÏÔʾ³öÀ´¡£
¡¡¡¡
¡¡¡¡ClassÎļþµÄ¸ñʽÓÉJVM¹æ·¶¹æ¶¨£¬Ò»¹²ÓÐÒÔϲ¿·Ö£º
¡¡¡¡
¡¡¡¡1. magic number£¬±ØÐëÊÇ0xCAFEBABE£¬ÓÃÓÚ¿ìËÙʶ±ðÊÇ·ñÊÇÒ»¸öclassÎļþ¡£
¡¡¡¡
¡¡¡¡2. version£¬°üÀ¨majorºÍminor£¬Èç¹û°æ±¾ºÅ³¬¹ýÁËJVMµÄʶ±ð·¶Î§£¬JVM½«¾Ü¾øÖ´ÐС£
¡¡¡¡
¡¡¡¡3. constant pool£¬³£Á¿³Ø£¬´æ·ÅËùÓÐÓõ½µÄ³£Á¿¡£
¡¡¡¡
¡¡¡¡4. access flag£¬¶¨ÒåÀàµÄ·ÃÎÊȨÏÞ¡£
¡¡¡¡
¡¡¡¡5. this classºÍsuper class£¬Ö¸Ê¾ÈçºÎÕÒµ½this classºÍsuper class¡£
¡¡¡¡
¡¡¡¡6. interfaces£¬´æ·ÅËùÓÐinterfaces¡£
¡¡¡¡
¡¡¡¡7. fields£¬´æ·ÅËùÓÐfields¡£
¡¡¡¡
¡¡¡¡8. methods£¬´æ·ÅËùÓÐmethods¡£
¡¡¡¡
¡¡¡¡9. attributes£¬´æ·ÅËùÓÐattributes¡£
¡¡¡¡
¡¡¡¡ÏÈдһ¸öTest.java£º
¡¡¡¡
¡¡¡¡package example.test;
¡¡¡¡
¡¡¡¡public final class TestClass {
¡¡¡¡public int id = 1234567;
¡¡¡¡public void test() {}
¡¡¡¡}
¡¡¡¡
¡¡¡¡È»ºó±àÒ룬·ÅÔÚC:\example\test\Test.class¡£
¡¡¡¡
¡¡¡¡ÎÒÃÇÓÃJavaÀ´¶ÁÈ¡ºÍ·ÖÎöclass£¬ClassAnalyzerµÄ¹¦ÄܱãÊǶÁÈ¡Test.class£¬·ÖÎö½á¹¹£¬È»ºóÏÔʾ³öÀ´£º
¡¡¡¡
¡¡¡¡package classfile.format;
¡¡¡¡
¡¡¡¡import java.io.*;
¡¡¡¡
¡¡¡¡public class ClassAnalyzer {
¡¡¡¡
¡¡¡¡public static void main(String[] args) {
¡¡¡¡DataInputStream input = null;
¡¡¡¡try {
¡¡¡¡input = new DataInputStream(new BufferedInputStream(new FileInputStream(
¡¡¡¡"C:\\example\\test\\TestClass.class"
¡¡¡¡)));
¡¡¡¡analyze(input);
¡¡¡¡}
¡¡¡¡catch(Exception e) {
¡¡¡¡System.out.println("Analyze failed!");
¡¡¡¡}
¡¡¡¡finally {
¡¡¡¡try { input.close(); } catch(Exception e) {}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡
¡¡¡¡public static void analyze(DataInputStream input) throws IOException {
¡¡¡¡// read magic number:
¡¡¡¡int magic = input.readInt();
¡¡¡¡if(magic==0xCAFEBABE)
¡¡¡¡System.out.println("magic number = 0xCAFEBABE");
¡¡¡¡else
¡¡¡¡throw new RuntimeException("Invalid magic number!");
¡¡¡¡// read minor version and major version:
¡¡¡¡short minor_ver = input.readShort();
¡¡¡¡short major_ver = input.readShort();
¡¡¡¡System.out.println("Version = " + major_ver + "." + minor_ver);
¡¡¡¡// read constant pool:
¡¡¡¡short const_pool_count = input.readShort();
¡¡¡¡System.out.println("constant pool size = " + const_pool_count);
¡¡¡¡// read each constant:
¡¡¡¡for(int i=1; i¡¡¡¡analyzeConstant(input, i);
¡¡¡¡}
¡¡¡¡}
¡¡¡¡
¡¡¡¡public static void analyzeConstant(DataInputStream input, int index) throws IOException {
¡¡¡¡byte flag = input.readByte();
¡¡¡¡// for read:
¡¡¡¡byte n8;
¡¡¡¡short n16;
¡¡¡¡int n32;
¡¡¡¡long n64;
¡¡¡¡float f;
¡¡¡¡double d;
¡¡¡¡byte[] buffer;
¡¡¡¡System.out.println("\nconst index = " + index + ", flag = " + (int)flag);
¡¡¡¡switch(flag) {
¡¡¡¡case 1: // utf-8 string
¡¡¡¡System.out.println(" const type = Utf8");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("¡¡¡¡ length = " + n16);
¡¡¡¡buffer = new byte[n16];
¡¡¡¡input.readFully(buffer);
¡¡¡¡System.out.println("¡¡¡¡¡¡value = " + new String(buffer));
¡¡¡¡break;
¡¡¡¡case 3: // integer
¡¡¡¡System.out.println(" const type = Integer");
¡¡¡¡n32 = input.readInt();
¡¡¡¡System.out.println("¡¡¡¡¡¡value = " + n32);
¡¡¡¡break;
¡¡¡¡case 4: // float
¡¡¡¡System.out.println(" const type = Float");
¡¡¡¡f = input.readFloat();
¡¡¡¡System.out.println("¡¡¡¡¡¡value = " + f);
¡¡¡¡break;
¡¡¡¡case 5: // long
¡¡¡¡System.out.println(" const type = Long");
¡¡¡¡n64 = input.readLong();
¡¡¡¡System.out.println("¡¡¡¡¡¡value = " + n64);
¡¡¡¡break;
¡¡¡¡case 6: // double
¡¡¡¡System.out.println(" const type = Double");
¡¡¡¡d = input.readDouble();
¡¡¡¡System.out.println("¡¡¡¡¡¡value = " + d);
¡¡¡¡break;
¡¡¡¡case 7: // class or interface reference
¡¡¡¡System.out.println(" const type = Class");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("¡¡¡¡¡¡index = " + n16 + " (where to find the class name)");
¡¡¡¡break;
¡¡¡¡case 8: // string
¡¡¡¡System.out.println(" const type = String");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("¡¡¡¡¡¡index = " + n16);
¡¡¡¡break;
¡¡¡¡case 9: // field reference
¡¡¡¡System.out.println(" const type = Fieldref");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("class index = " + n16 + " (where to find the class)");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
¡¡¡¡break;
¡¡¡¡case 10: // method reference
¡¡¡¡System.out.println(" const type = Methodref");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("class index = " + n16 + " (where to find the class)");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
¡¡¡¡break;
¡¡¡¡case 11: // interface method reference
¡¡¡¡System.out.println(" const type = InterfaceMethodref");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("class index = " + n16 + " (where to find the interface)");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
¡¡¡¡break;
¡¡¡¡case 12: // name and type reference
¡¡¡¡System.out.println(" const type = NameAndType");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println(" name index = " + n16 + " (where to find the name)");
¡¡¡¡n16 = input.readShort();
¡¡¡¡System.out.println(" descripter = " + n16 + " (where to find the descriptor)");
¡¡¡¡break;
¡¡¡¡default:
¡¡¡¡throw new RuntimeException("Invalid constant pool flag: " + flag);
¡¡¡¡}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡
¡¡¡¡Êä³ö½á¹ûΪ£º
¡¡¡¡
¡¡¡¡magic number = 0xCAFEBABE
¡¡¡¡Version = 48.0
¡¡¡¡constant pool size = 22
¡¡¡¡
¡¡¡¡const index = 1, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 22
¡¡¡¡value = example/test/TestClass
¡¡¡¡
¡¡¡¡const index = 2, flag = 7
¡¡¡¡const type = Class
¡¡¡¡index = 1 (where to find the class name)
¡¡¡¡
¡¡¡¡const index = 3, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 16
¡¡¡¡value = java/lang/Object
¡¡¡¡
¡¡¡¡const index = 4, flag = 7
¡¡¡¡const type = Class
¡¡¡¡index = 3 (where to find the class name)
¡¡¡¡
¡¡¡¡const index = 5, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 2
¡¡¡¡value = id
¡¡¡¡
¡¡¡¡const index = 6, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 1
¡¡¡¡value = I
¡¡¡¡
¡¡¡¡const index = 7, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 6
¡¡¡¡value =
¡¡¡¡
¡¡¡¡const index = 8, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 3
¡¡¡¡value = ()V
¡¡¡¡
¡¡¡¡const index = 9, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 4
¡¡¡¡value = Code
¡¡¡¡
¡¡¡¡const index = 10, flag = 12
¡¡¡¡const type = NameAndType
¡¡¡¡name index = 7 (where to find the name)
¡¡¡¡descripter = 8 (where to find the descriptor)
¡¡¡¡
¡¡¡¡const index = 11, flag = 10
¡¡¡¡const type = Methodref
¡¡¡¡class index = 4 (where to find the class)
¡¡¡¡nameAndType = 10 (where to find the NameAndType)
¡¡¡¡
¡¡¡¡const index = 12, flag = 3
¡¡¡¡const type = Integer
¡¡¡¡value = 1234567
¡¡¡¡
¡¡¡¡const index = 13, flag = 12
¡¡¡¡const type = NameAndType
¡¡¡¡name index = 5 (where to find the name)
¡¡¡¡descripter = 6 (where to find the descriptor)
¡¡¡¡
¡¡¡¡const index = 14, flag = 9
¡¡¡¡const type = Fieldref
¡¡¡¡class index = 2 (where to find the class)
¡¡¡¡nameAndType = 13 (where to find the NameAndType)
¡¡¡¡
¡¡¡¡const index = 15, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 15
¡¡¡¡value = LineNumberTable
¡¡¡¡
¡¡¡¡const index = 16, flag = 1
¡¡¡¡const type = Utf8
¡¡¡¡length = 18
¡¡¡¡value = LocalVariableTable

²é¿´±¾ÎÄÀ´Ô´

    • 评论
    • 分享微博
    • 分享邮件
    闂傚倸鍊风欢锟犲矗鎼淬劌绐楅柡鍥╁亹閺嬪酣鏌曡箛瀣仾濠殿垰銈搁弻鏇$疀鐎n亖鍋撻弽顓ㄧ稏闁跨噦鎷�

    婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掆偓缂嶅﹪骞冨Ο璇茬窞闁归偊鍓涢悾娲⒑闂堟单鍫ュ疾濠婂嫭鍙忔繝濠傜墛閸嬨劍銇勯弽銊с€掗柟钘夊暣閺岀喖鎮滈埡鍌涚彋閻庤娲樺畝绋跨暦閸洖鐓涢柛灞剧矋濞堟悂姊绘担绛嬪殐闁搞劋鍗冲畷銏ゅ冀椤愩儱小闂佹寧绋戠€氼參宕伴崱妯镐簻闁靛牆鎳庢慨顒€鈹戦埥鍡椾簼婵犮垺锚铻炴俊銈呮噺閸嬪倹绻涢崱妯诲碍閻庢艾顦甸弻宥堫檨闁告挾鍠庨锝夘敆娓氬﹦鐭楁繛鎾村焹閸嬫捇鏌e☉娆愬磳闁哄本绋戦埞鎴﹀川椤曞懏鈻婄紓鍌欑劍椤ㄥ懘鎯岄崒鐐靛祦閹兼番鍔岄悞鍨亜閹烘垵顏╅悗姘槹閵囧嫰寮介妸褎鍣ョ紓浣筋嚙濡繈寮婚悢纰辨晣鐟滃秹鎮橀懠顒傜<閺夊牄鍔庣粻鐐烘煛鐏炶姤鍠橀柡浣瑰姍瀹曠喖顢橀悩铏钒闂備浇宕垫慨鎶芥⒔瀹ュ鍨傞柦妯猴級閿濆绀嬫い鏍ㄧ☉濞堟粓姊虹涵鍛【妞ゎ偅娲熼崺鈧い鎺嗗亾闁挎洩濡囧Σ鎰板籍閸繄顓洪梺缁樺姇瀵剙螖閸涱喚鍘搁梺鍓插亽閸嬪嫰鎮橀敃鍌涚厱閻庯綆鍋嗘晶顒傜磼閸屾稑绗ч柟鐟板閹煎湱鎲撮崟闈涙櫏闂傚倷绀侀幖顐も偓姘卞厴瀹曞綊鏌嗗鍛紱閻庡箍鍎遍ˇ浼村磿瀹ュ鐓曢柡鍥ュ妼婢ь垰霉閻樿秮顏堟箒闂佹寧绻傚Λ妤呭煝閺囥垺鐓冪憸婊堝礈濮樿泛钃熼柕濞у嫷鍋ㄩ梺缁樺姇椤曨參鍩㈤弴銏″€甸柨婵嗗€瑰▍鍥ㄣ亜韫囨稐鎲鹃柡灞炬礋瀹曢亶顢橀悢濂変紦

    重磅专题
    往期文章
    最新文章